-2

Alright, so i am in an intermediate programming class and we are using C++. But for the intro class, we learned python. That being said, i do not know much C++ at all. I have done a lot of searching and tried my best to set up this program but i just cannot get it to work correctly. It seems as though i have gotten down to just "identifier "" is undefined" errors and i dont know how to fix them. The program is supposed to take an entered email and check that it is formatted correctly.

My teacher suggested doing it this way: "To validate, check into the string variable defined in c++. Look at the methods that allow you to search a string looking for characters. Also look at the string methods that create string. You should be able to search a string for the @ and create substring of the parts. Then validate the parts."

Please help!

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class emailverify
{
public:             // global variables/functions
    std::string test;
    std::string email;
    std::string at = "@";
    std::string period = ".";
};




int main()
{
    std::string test = "Y";
        while (test == "Y" || test == "y") // while loop to keep asking for more emails if the user wishes to test another
        {
            cout << "Please enter a valid Email address.\n"; // asks for email
            cin >> email;
                if (email.find(at != std::string::npos))
                {
                    if (email.find_last_of(period != std::string::npos))
                    {
                        cout << "This email is valid. Would you like to try another? (Y/N)\n"; // email passed both tests, so is valid, seeing if while loops continues or not
                        cin >> test;
                    }
                    else
                    {
                        cout << "This email is invalid, would you like to try another? (Y/N)\n"; // failed second test, so is not valid, seeing if while loop continues or not
                        cin >> test;
                    }
                }
                else
                {
                    cout << "This email is invalid, would you like to try another? (Y/N)\n"; // failed first test, so is not valid, seeing if while loop continues or not
                    cin >> test;
                }
        } // while loop ends
    return 0;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
A.j. Schenker
  • 23
  • 1
  • 4

2 Answers2

2

You seem to be confused about how variables work in C++.

Compiling your program with GCC it says:

test.cpp: In function ‘int main()’:
a.cpp:23:20: error: ‘email’ was not declared in this scope
             cin >> email;

That means that there is no such variable named email. You declared a member variable with that name inside your emailverify class, but that will only be used if you define a variable of type emailverify, and you did not do that.

For now, I'd suggest you get rid of the emailverify class and declare the variables you need directly as local variables in main (you could declare them as global, but it is better if you keep them local):

int main()
{
    std::string test;
    std::string email;
    std::string at = "@";
    std::string period = ".";

Then there are a bunch of other errors, such as email.find(at != std::string::npos) instead of email.find(at) != std::string::npos, but you'll get to those eventually.

PS: I know some programming teachers like writing code such as std::string at = "@"; but IMHO that is just silly. Writing email.find("@") is perfectly ok, the extra variable buys you nothing.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Thanks so much! I only tried to use the class because our teacher suggested that we use it. – A.j. Schenker Sep 03 '17 at 21:44
  • @A.j.Schenker: Ah, I fail to see how a class can be useful here. But if you want to try, you have to define a variable of that class `int main() { emailverify e;` and then refer to the member variables as `e.test`, `e.email`, etc. But beware of that local `test` variable you declared, that will be different of `e.test`. – rodrigo Sep 03 '17 at 21:47
0

Your problem is in part of code:

class emailverify
{
public:             // global variables/functions
    std::string test;
    std::string email;
    std::string at = "@";
    std::string period = ".";
};

it does not define global variables or functions but declares class. There is no email or test variable in your main function neither defined or declared.

If you want to stick to globals things you can do is either create global object of type emailverify and use its members via . or make all of them static and access via :: (emailverify::test) or change class into namespace, but it would require defining them outside of class as well (Defining static members in C++).

But, you could just use them as locals and don't worry about all of this right now.

R2RT
  • 2,061
  • 15
  • 25
  • I just tried to do that and dont understand it well enough to be able to implement the global variables. :/ thanks though! – A.j. Schenker Sep 03 '17 at 21:50