Thread: vectors and user input

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    vectors and user input

    Hey everyone,
    Today's question is sponsored by the topic of vectors and the letter h for "help"...
    So, I'm using a vector of strings to accept user input. My code takes the users input and inserts it into the vector using the string variable with the user input as the argument passed into push_back():

    Code:
    string gameChoice;
    //...some if else statements and a while statement follow...
    
    cout << "\nEnter the name of your favorite game: ";
    cin  >> gameChoice;
    gameList.push_back(gameChoice);
    Well what happens is any single word input is used properly and everyone's happy but once it goes to multiple words it prints the 1st word and sends a blank field to the next iteration of the loop the whole thing is in (indicating the problem is with my cin statement...hwoever cin.get only accepts chars it seems...so any help would be awesome...)-Chap

    By the way...did anyone else have any problems getting onto this website? I've been trying for the past 2 days but I was just getting an eternally loading homepage...just curious if anyone else was having trouble...
    Last edited by Chaplin27; 01-17-2005 at 08:37 AM. Reason: extra info

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You can't use cin for strings containing multiple words. cin will stop at the first blank, that is what you are running into. You need to use getline with strings. Here is an example below that will probably help you out:

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main(void) {
    
    	vector<string> myString;
    	vector<string>::iterator itString;
    	string input;
    
    	for(int x = 0; x<5; x++) {
    
    		cout<<"Enter your favorite game: ";
    		getline(cin, input);
    		myString.push_back(input);
    	}
    	for(itString = myString.begin(); itString != myString.end(); itString++) {
    
    		cout << *itString;
    		cout<<endl;
    	}
    
    	return 0;
    }
    Happy Coding!!
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    so what exactly is the cin argument in getline?

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    The first argument to string::getline is an istream object, in this case we are using cin for the object. Any istream object would suffice. Obviously the second argument is the string.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I know this isn't a very big deal, but...aww screw it I'll do it anyways! This is America!
    Code:
    For C++
    
    
    The following are acceptable uses:
    
    
    int main ( int argc, char *argv[] )
    int main ()
    
    
    The first option follows similar conventions to those used by C99.
    
    The second option is used when you do not require access to the command
    line arguments, and is equivalent to the int main(void) option used by C99
    From CProgramming.com FAQ
    The code you posted used int main(void), that's a C way of saying no arguements. In C++, we should use int main(). I hate myself now :(

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    yes, and I do know the standard. I've read it a few times. Yet, I do not like it so I'll stick with C99 approach. I am sure I will get stoned for it by somebody around here but I like to live dangerously.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    We live in a dangerous world! lol
    Last edited by Kleid-0; 01-17-2005 at 11:00 AM. Reason: Bad smiley bad!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM
  5. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM