Thread: Whole words

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Whole words

    Ive been having a problem, ive gone through the frst few basic lessons in c++ here at the site, and can get my tiny programs to accept a single character or number, but it will not accept any whole words. How do I get it to do so?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    	
    	string word;
    	
    	cout<<"enter a word: ";
    	cin>>word;
    
    	cout<<"you entered: "<<word<<endl;
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    thanks! i know im a n00b but it really helps anyway

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    okay, i have this now... it wont do squat, and i know i have absolutely nothing good here, but maybe you can see what im trying to do, and understand why im an idiot, btw, my cout is just random crap, so um, iforget my point then

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    
        string name;
        string choicea = 'leave' ;
            cout<<"Welcome to Texst adventure! Please enter your name:" ;
        cin>> name;
        cin.ignore();
        cout<< " your name is: "<< name <<"!\n\n";
        cin.get();
        
        cout<<"welcome to the adventure,"<< name <<"!\n";
        cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n";
        cin>> choicea;
        cin.ignore();
        if (choicea == 1){
                    cout<<" You exit your house, now what?\n";}
                    else (
                    cout<<" I do not understand your command, please try again. \n";}
                    while ( choicea != 1 ){
                          }
        
        
    }

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    string choicea = "leave";  // Double quotes
    Also you're comparing a string to an int in your if statement.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Quote Originally Posted by futurama140
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    
        string name;
        string choicea = 'leave' ;
            cout<<"Welcome to Texst adventure! Please enter your name:" ;
        cin>> name;
        cin.ignore();
        cout<< " your name is: "<< name <<"!\n\n";
        cin.get();
        
        cout<<"welcome to the adventure,"<< name <<"!\n";
        cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n";
        cin>> choicea;
        cin.ignore();
        if (choicea == 1){
                    cout<<" You exit your house, now what?\n";}
                    else (
                    cout<<" I do not understand your command, please try again. \n";}
                    while ( choicea != 1 ){
                          }
        
        
    }

    That is there for seemingly no apparent reason you might want to take it out.
    My computer is awesome.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    so how do i compile the string into a string into the if statement?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Also, when i type in "leave" when running the program, after the
    Code:
    cin>> choicea;
    the program exits, does anyone get what im trying to do? i want it to display the one cout after i type in leave, and if i dont type in "leave" i want it to say the other cout.

    Help please?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    So, if choicea is equal to "leave", you want it to run the first cout? Then code: if choicea is equal to "leave".

    Your current code says: if choicea is equal to 1.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    no, ichanged it earlier, before i made the post with the question, my current code is:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    
        string name;
        string choicea = "leave" ;
            cout<<"Welcome to Texst adventure! Please enter your name:" ;
        cin>> name;
        cin.ignore();
        cout<< " your name is: "<< name <<"!\n\n";
        cin.get();
        
        cout<<"welcome to the adventure,"<< name <<"!\n";
        cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n";
        cin>> choicea;
        
        if (choicea == "leave"){
                    cout<<" You exit your house, now what?\n";}
                    else {
                    cout<<" I do not understand your command, please try again. \n";}
                    while ( choicea != "leave" ){
                          }
        
        
    }

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    OK, so I got that one problem fixed, but now how do i loop it back the the first choice if someone inputs an unrecognised command? i have:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    
        string name;
        string choicea = "leave" ;
            cout<<"Welcome to Texst adventure! Please enter your name:" ;
        cin>> name;
        cin.ignore();
        cout<< " your name is: "<< name <<"!\n\n";
        cin.get();
        
        cout<<"welcome to the adventure,"<< name <<"!\n";
        cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n";
        cin>> choicea;
        
        if (choicea == "leave"){
                    cout<<" You exit your house, now what?\n";
                    cin.get();}
                    
                    else {
                    cout<<" I do not understand your command, please try again. \n";}
                    while ( choicea != "leave" ){
                          }
        cin.get();
        
    }
    notice it says while choicea does not equal "leave" then its blank, i dont know what to say to get it to loop back to the choice.

    edit: also, it now wont display the 2nd cout if i input the wrong text. it ggoes to the first cout anyway
    Last edited by futurama140; 12-08-2005 at 01:05 AM.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    something like this
    Code:
        cin>> choicea;
        while (choicea != "leave"){
            cout<<" I do not understand your command, please try again. \n";
            cin>> choicea;
        }
        cout<<" You exit your house, now what?\n";
    Kurt

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    yea, thats not cutting it, you try using that in my code and see what happens, and its not pretty

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, that looks like it would fit nicely. Maybe you didn't use it correctly. Post your code and your problems if your imlpementation of the suggestion isn't working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM