Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 11-23-2006, 04:14 AM   #1 (permalink)
 
Junior Techie

Join Date: Nov 2005

Posts: 76

sum1nowhere is on a distinguished road

Question Noob needs C++ help

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?
sum1nowhere is offline  
Old 11-23-2006, 04:32 AM   #2 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

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
__________________
baronvongogo is offline  
Old 11-23-2006, 04:39 AM   #3 (permalink)
 
Junior Techie

Join Date: Nov 2005

Posts: 76

sum1nowhere is on a distinguished road

Default

Oh it wasn't the caps I was forgetting it was the quotes....thank you.
sum1nowhere is offline  
Old 11-23-2006, 04:43 AM   #4 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

No problem I also threw in system("pause") just so you can see the welcome or invalid imput.
__________________
baronvongogo is offline  
Old 11-24-2006, 07:07 PM   #5 (permalink)
 
Newb Techie

Join Date: Nov 2006

Posts: 18

InvestorJeff

Send a message via AIM to InvestorJeff
Default

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 is offline  
Old 11-24-2006, 08:03 PM   #6 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

Quote:
Originally posted by InvestorJeff
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
__________________
baronvongogo is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On