Adblock breaks this site

Loop + cin help

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

  1. 88jayto

    88jayto Apprentice
    Banned

    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0
    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?
     
  2. wackywamba

    wackywamba Guru

    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1
    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.
     
  3. 88jayto

    88jayto Apprentice
    Banned

    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0
    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?
     
  4. wackywamba

    wackywamba Guru

    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1
  5. 88jayto

    88jayto Apprentice
    Banned

    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  6. 88jayto

    88jayto Apprentice
    Banned

    Joined:
    Oct 16, 2008
    Posts:
    751
    Referrals:
    0
    Sythe Gold:
    0
    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 = "";
    }
    }
     
  7. wackywamba

    wackywamba Guru

    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1
    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 >


 
 
Adblock breaks this site