not declared in scope...function problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <cstdlib>

void askForName(string& name);
//Request for username

void validateUserName(string& name);
//Validates that the user enters characters only in their name.


using namespace std;

int main()
{
    string userName;
    askForName(userName);
    validateUserName(userName);
    return 0;
}

void askForName(string& name)
{
    cout << "What's your name?\n";
    getline(cin,name);
}

void validateUserName(string& name)
{
    bool beta = false;
    int nameLength = name.length();
    for(int i = 0;i < nameLength;i++)
    {
        if(isalpha(name[i])||isspace(name[i]))
        {

        }
        else
        {
            cout << "First instance of a non char is at index "
                 << name.find_first_not_of("abcdefghijklmnopqrstuvwxyz",0) << ".\n";
            beta = true;
        }
    }
    if (beta == true)
    {
        cout << "Enter a name with characters only.\n";
        getline(cin,name);
    }

}

Am I missing something here? Not sure what's going on. I'm using Qt Creator as my compiler.
You are missing the std:: prefix for everything, e.g. std::string

http://stackoverflow.com/q/1452721/1959975
I included std by using namespace std;
Edit: fixed it by moving using namespace std; before my function declarations. Thanks for pointing it out though LB!
Last edited on
You should not use using namespace std; - it is a bad habit: http://stackoverflow.com/q/1452721/1959975
Last edited on
Hi,

Having using namespace std; is a bad idea - Google to see why. Best use the format that LB mentioned.
I'll read into it. This is the way I've been doing it in school for my C++ class. Don't know how else to do it. Also a lot of the way I've seen code written on here is a lot different then the way I write in class. Is there a name for the different styles. For example I don't see anyone using cout << and see a lot of ::. Which way of writing c++ is better not only technically, but for the work world too?
Topic archived. No new replies allowed.