Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 08-27-2005, 02:45 AM   #1 (permalink)
 
Newb Techie

Join Date: Jul 2005

Posts: 48

010001100101010

Question If statements(c++)

i was trying to use a if statement so that when you enter yes or no diferent strings come up. i was wondering if you can do that. my code looks like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;

string hi;
cout << "hello im Cbuddy, what is your name?";
cin >> name;

string answer;
string yes;

cout << "oh hello "; cout << name; cout << " have we met before?" << endl;
cin >> answer;
if(answer==yes){cout<<"oh now i remember you"<<endl};
else{cout<<"oh well its nice to meet you"<<endl};

system ("pause");
return 0;
}
the lines in red are the ones that my debugger found were wrong, although i think the real problem is how i defined answer and yes.
then again im pretty new to c++ so i could be totaly wrong on all this stuff.
__________________
http://i12.photobucket.com/albums/a2...cew00t/bla.bmp
Daisy, Daisy, give me your answer do. I\'m half crazy, all for the love of you
010001100101010 is offline  
Old 08-27-2005, 08:36 AM   #2 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

you have the right idea although == wont work when comparing strings.

My C++ is a bit rusty but i belive you could use strcmp() or stricmp().

Code:
if(strcmp(answer, "yes")==0){
     ***
}else{
     ***
}
strcmp returns 0 if they match and something else if they dont, i think 1 or -1 but i'm not sure. I assume it is implemented in C++ it is there in C anyway
fitzjj is offline  
Old 08-27-2005, 12:27 PM   #3 (permalink)
 
Newb Techie

Join Date: Jul 2005

Posts: 48

010001100101010

Default

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;

string hi;
cout << "hello im Cbuddy, what is your name?";
cin >> name;

string answer;
string yes;

cout << "oh hello "; cout << name; cout << " have we met before?" << endl;
cin >> answer;

if(strcmp(answer, "yes")==0){
cout << "oh now i remember you" << endl;
}else{
cout << "oh well nice to meet you" << endl;
}

system ("pause");
return 0;
}
im not sure if i did what you said right but now theres an error in the red line and my compiler sais that it cannot convert.
__________________
http://i12.photobucket.com/albums/a2...cew00t/bla.bmp
Daisy, Daisy, give me your answer do. I\'m half crazy, all for the love of you
010001100101010 is offline  
Old 08-27-2005, 12:58 PM   #4 (permalink)
 
Super Techie

Join Date: Feb 2004

Posts: 416

chog

Send a message via AIM to chog
Default

i would try your original way just with yes in quotes("yes").
chog is offline  
Old 08-30-2005, 09:34 AM   #5 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

my bad, do as chog says and stick the yes in quotes. strcmp will compare char arrays which are the original way to deal with strings in C/C++. Since you are using the <string> library you can use == to compare.

if you need to user strcmp, you also need to #include <stdio.h>
fitzjj is offline  
Old 08-31-2005, 11:27 AM   #6 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

Hi,

The code bellow should work fine, it does for me in Dev-C++:

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
    
    string name;
    string hi;
    
    cout << "hello im Cbuddy, what is your name?";
    cin >> name;
    
    string answer;
    string yes;
    
    cout << "oh hello " << name << " have we met before?" << endl;
    cin >> answer;
         if (answer == "yes"){
                cout << "oh now i remember you" << endl;
                } else {
                     cout << "oh well its nice to meet you" << endl;
                     }

system ("pause");
return 0;
}
EDIT: Can anyone tell me how to make a code box on this forum please.
Cache is offline  
Old 08-31-2005, 11:38 AM   #7 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

[coed] code goes here [/coed]

but obviously with code spelt correctly!
fitzjj is offline  
Old 08-31-2005, 11:40 AM   #8 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

Quote:
Originally posted by fitzjj
[coed] code goes here [/coed]

but obviously with code spelt correctly!
Ok, thanks alot.
Cache is offline  
Old 08-31-2005, 12:44 PM   #9 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

I just noticed the "string yes;" part does nothing and is not needed. I think perhaps you wanted to use that string to compair with the string "answer", right? which is why you never used quotation marks in "if (answer == "yes")". To do that you should have given the string "yes" the value "yes"
Code:
string yes = "yes";
EDIT: The line bellow is also useless:
Code:
string hi;

Cache 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