Loop + cin help

Discussion in 'Programming General' started by 88jayto, Mar 29, 2011.

Loop + cin help
  1. Unread #1 - Mar 29, 2011 at 1:41 PM
  2. 88jayto
    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0

    88jayto Apprentice
    Banned

    Loop + cin help

    while(cin >> note_to_play;)
    {
    notes_to_play.push_back(note_to_play);
    }

    This functions like an infinite loops... I am trying to make it keep adding to the vector as long as user inputs something... How do I do this correctly?
     
  3. Unread #2 - Apr 7, 2011 at 3:17 AM
  4. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Loop + cin help

    Okay, there are a number of problems you need to look at.

    Firstly, your condition for your while is saying that as long as note_to_play can be stored as a char[]. This will most likely never be false so you are never giving a condition to leave the while loop.

    Secondly, cin can get convoluted with other IO's, so it's a good idea to usually use a cin.clear() after every input.

    Lastly, to address your first problem, by the looks of things you will need to use an EOF character. If you try to use CTRL+Z or CTRL+C this should close the program. These are the default operating system EOF's.

    You will need to create your own say, "-1" then check when the user enters a -1 and continue on with your code.

    Hope this helped, if not let me know.
     
  5. Unread #3 - Apr 10, 2011 at 7:48 PM
  6. 88jayto
    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0

    88jayto Apprentice
    Banned

    Loop + cin help

    the user inputs commands on one line separated by spaces, I want it to put all the commands the user entered on that line into a single vector. I don't want the user to have to enter a terminator. Is there another way to do it?
     
  7. Unread #4 - Apr 14, 2011 at 3:16 PM
  8. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

  9. Unread #5 - Apr 16, 2011 at 10:03 PM
  10. 88jayto
    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0

    88jayto Apprentice
    Banned

    Loop + cin help

    Sorry, but I don't understand that examples. I am still in my first year of c++ :-/ . Is there a default null terminator for cin? I know u can find the end of a cstring using the null terminator /0.
     
  11. Unread #6 - Apr 16, 2011 at 11:55 PM
  12. 88jayto
    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0

    88jayto Apprentice
    Banned

    Loop + cin help

    I fixed it, I just used getline and just made a for loop go through the entire thing and to make a new cell everytime it finds a space... like so
    vector<string> notes; // The entire song is added to vector before it is played
    string note;
    string user_input;
    cout << "Please Enter Notes or /help: ";
    getline(cin, user_input);
    if(user_input[user_input.size()-1]!=' ')//Makes Null Terminator
    user_input+=" ";
    for(int z = 0; z < user_input.size(); z++)//This loops breaks up the string into notes and puts them into an vector
    {
    if(user_input[z]!=' ')
    note+=user_input[z];
    else
    {
    notes.push_back(note);
    note = "";
    }
    }
     
  13. Unread #7 - Apr 17, 2011 at 3:47 AM
  14. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Loop + cin help

    There you go, I was about to suggest using a getline if those were your intentions.

    Good luck with the rest of it though.
     
< Problem With Program | Looking for someone to make me somthing >

Users viewing this thread
1 guest


 
 
Adblock breaks this site