Noob needs C++ help

Status
Not open for further replies.

sum1nowhere

Baseband Member
Messages
85
How do I set a variable equal to a word? Like as in a user name? So that it only accepts one user name? I have tried doing "
cout << "Name:" << endl;
string user;
cin >> user;
if (user == Bob)
{
cout << "Welcome" << endl;
}
else
{
cout << "The name you entered is not a valid." << endl;
}
"
I know that is not how it is supposed to be done, but I am just a newbie teaching myself what I can before college starts in January. So can any one tell me how to make it so it only accepts certain inputs?
 
include <iostream>
#include <string>

using namespace std;

void main(){

cout << "Name:" << endl;
string user;
cin >> user;
if (user == "Bob")
{
cout << "Welcome" << endl;
system("pause");
}
else
{
cout << "The name you entered is not a valid." << endl;
system("pause");
}

}

Its case sensative so bob must be Bob
 
Yea, if you are worrying about case sensitive, i believe you can just go...

if (user == "Bob" || user == "bob")

**Another Solution**

Just use the toupper() and/or tolower() function that way whatever they enter will work.

Thats my 2 cents...
 
InvestorJeff said:
Yea, if you are worrying about case sensitive, i believe you can just go...

if (user == "Bob" || user == "bob")

**Another Solution**

Just use the toupper() and/or tolower() function that way whatever they enter will work.

Thats my 2 cents...

toupper and tolower are for use with Chars not strings
 
Status
Not open for further replies.
Back
Top Bottom