Thread: Noob Question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Noob Question

    I am not very good at programming and am only 14, but my program uses a string variable and says no to whatever you type. Primative, I know, but the programme has a repeat option but when the programme repeats, it doesnt ask for the input again it just says no.

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
     string Question;
     int LoopStop;
     do
     {
     cout<<"Please Ask The Computer A Question:\n"<<flush;
     getline(cin,Question);
     cout<<" \n"<<flush;
     cout<<"NO!\n"<<flush;
     cout<<" \n"<<flush;
     cout<<"Would you like to continue? (0 for yes/ 1 for no)\n"<<flush;
     cin >> LoopStop;
     cout<<" \n"<<flush;
     system ("CLS");
     }
     while ( LoopStop == 0 );
    }
    Any help?
    Last edited by bumfluff; 11-05-2005 at 05:38 PM. Reason: Changes

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
     string Question;
     int LoopStop;
     do
     {
        cout<<"Please Ask The Computer A Question:\n"<<flush;
        getline(cin,Question);
        cout<<" \n" << flush;
        cout<<"NO!\n" <<flush;
        cout<<" \n" << flush;
        cout<<"Would you like to continue? (0 for yes/ 1 for no)\n"<<flush;
        cin >> LoopStop; // This leaves a \n in the stream which then gets input into
                         // your string the next time around.
        cin.ignore();    // This will ignore the next character in the stream.
        cout<< " \n" << flush; // Why the \n when you're about to clear the screen?
        system("CLS");
     } while ( LoopStop == 0 );
     
     return 0;    // Don't forget to return an int
    }
    
    // I'd also recommend not flushing your output so much and grouping your
    // cout statements better. If you want to flush and do new lines, use endl. Also indent.
    If you don't understand this, then read up on how streams/IO buffers work.
    Last edited by SlyMaelstrom; 11-05-2005 at 05:59 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    First, you should always initialize your variables. That means in the same line that you create them, you should assign them a value. If you don't know what value to assign them at that point, then assign them 0:
    Code:
    string Question = "";
    int LoopStop = 0;
    
    int myArray[10] = {0};  //initializes every element with 0
    If you don't initialize your variables, they may contain junk values which could trip you up later in your program.

    Secondly, adding a newline and then flushing the internal buffer to the screen is equivalent to endl, so this line:

    cout<<"\n"<<flush;

    can be written more succinctly:

    cout<<endl;

    Finally, a line like this:

    cin >>num

    waits for the user to enter some input. Then the user can type something in and hit Return. However, when the user hits Return, an invisible \n or "newline" character is entered after the input ('\n' is considered a single character). So, if the user types in 10 and hits Return, the input actually looks like this:

    10\n

    Now, let's examine how that input is handled. The >> operator is programmed to skip all leading whitespace, read in data, and stop reading in data when the first white space character is encountered(e.g. spaces, tabs, and newlines). And, very importantly, the terminating whitespace character is not read in. So, with this input:

    10\n

    1) The >> operator skips any leading whitespace--there's no whitespace in front of 10, so
    2) The >> operator reads in 10, and
    3) The >> operator stops reading when it encounters the whitespace character \n
    4) The >> operator does not read in the \n, and leaves the \n in the input stream.

    Sometime thereafter, if these lines are executed:

    cout << "\nEnter your name: ";
    getline(cin, name);

    that is an instruction to read in more input. Specifically, getline() grabs all the input it can including whitespace until it encounters a newline character '\n'. Very importantly, unlike the >>operator, getline() reads in the terminating \n character. In your case, there is still a \n left in the input stream, and as far as getline() is concerned that is good input, so getline() doesn't need to wait for any user input. Therefore, getline() does it's thing and reads in the \n. Since a \n causes getline() to finish executing, your program continues on its way.

    The lesson is: if you are going to be switching between the >>operator and getline(), you need to remove the '\n' character that is inserted at the end of the input when the user hits Return and which the >>operator leaves in the input stream. You can remove a trailing whitespace character like this:

    cin>>data;
    cin.ignore(1);

    cin.ignore() will remove the designated number of characters from the stream.

    Note that cin.ignore() is not necessary when you do something like this:

    cin>>data;

    and then the user enters:

    10\n

    and then you do something like this:

    cin>>data2;

    Since the >> operator skips leading whitespace, it skips over the \n remaining in the stream and searches for for input, and when it can't find any, it stops and waits for the user to enter something.
    Last edited by 7stud; 11-06-2005 at 04:19 AM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Thanks, I changed my program to what slymaelstrom said, initialized my variables and used endl instead of flush. I am afraid I did not understand all of your explanation, 7stud.

    Thanks

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    cout<<" \n"<<flush;
    cout<<"NO!\n"<<flush;
    cout<<" \n"<<flush;
    Why would you need flush after every cout statement there? Instead of writing a newline to the screen, and then writing NO! + another newline to the screen, and then writing another newline to the screen, why couldn't you write all of it to the internal buffer and then flush all of it to the screen at once? It would be faster. Writing to the screen is relatively slow, so if you write to the internal buffer, which is done automatically for you, then flush everything, the user would see everything faster. The internal buffer is used to speed up output to the screen. It stores up the text you want to output, and then writes the data to the screen in large chunks, which makes it more efficient.

    In addition, I believe whenever you cin>> it will automatically flush cout. So, you don't have to worry about flushing buffers.
    Last edited by 7stud; 11-06-2005 at 04:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM