Computing.Net > Forums > Programming > Help witth this C++ code!! (simple)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help witth this C++ code!! (simple)

Reply to Message Icon

Name: mdude
Date: December 7, 2008 at 02:39:37 Pacific
OS: xp
CPU/Ram: 512
Product: Dell / 12
Comment:

Hello im just starting C++ and i dont get why this isnt working. I hope you can see what i want it to do. I want to cout something IF the cin was a specific input. Here's the code.... #include using namespace std; int main()
{ char Answer[4];
cout<<"please say yes: ";
cin>>Answer;
cin.ignore();
if (Answer[4] == "yes")
{
cout<<"You said yes";
}
cin.get();
}

Thanks alot.



Sponsored Link
Ads by Google

Response Number 1
Name: dave19
Date: December 7, 2008 at 05:43:04 Pacific
Reply:

I think you will need to compare each element of an array string like Answer[0] == 'y'
Answer[1] == 'e'
so on..
or you can do this simply by including <string> header file..
int main( )
{
string answer("yes");
char ans[4];
cout<<"Enter your answer: "; cin>>ans;
string get(ans);
if (answer.compare(get) == 0)
cout<<"you said yes"<<endl;
else
{cout<<"you said "<<get<<endl;}
}
or you can use char Answer;
and get the input as 'y' or 'n' and simply compare it using IF.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help witth this C++ code!! (simple)

!!! help to Debug C code programmin www.computing.net/answers/programming/-help-to-debug-c-code-programmin/7888.html

Help with debugging C++ code www.computing.net/answers/programming/help-with-debugging-c-code/13083.html

I need Help explaining a C program www.computing.net/answers/programming/i-need-help-explaining-a-c-program/17576.html