CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2008
    Posts
    12

    Second part of my program repeats and...

    Why? I added a function to test things and it's now repeating once and automatically assumes I entered something other than y, or n.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int squared(int s)
    {
     return s * 2;
    }
    
    
    int main()
    {
     top:
     system("CLS");
     
     cout << "What integer do you want squared? ";
     int x;
     cin >> x;
     cout << squared(x) << endl;
    
     string quitKey = "y";
     do
     {
      topOfLoop:
      cout << "Do you wish to quit? y/n ";
      getline(cin, quitKey);
      if (quitKey == "y") return 1;
      if (quitKey == "n") goto top;
      else
          cout << "What? ";
          goto topOfLoop;
     } while (quitKey != "y");
    
     return 0;
    }
    Last edited by Artitatt; June 11th, 2008 at 01:03 AM.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Second part of my program repeats and...

    Um you shouldn't use gotos.

    I don't know why you're using getline when your desired input is only one character? For a quick fix, use cin instead of getline:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int squared(int s)
    {
     return s * 2;
    }
    
    
    int main()
    {
     top:
     system("CLS");
     
     cout << "What integer do you want squared? ";
     int x;
     cin >> x;
     cout << squared(x) << endl;
    
     string quitKey = "y";
     do
     {
      topOfLoop:
      cout << "Do you wish to quit? y/n ";
      cin >> quitKey;
      if (quitKey == "y") return 1;
      if (quitKey == "n") goto top;
      else
          cout << "What? ";
          goto topOfLoop;
     } while (quitKey != "y");
    
     return 0;
    }
    I think your problem has to do with the stream having stuff in it from the first cin statement and it affecting the getline statement but someone else can clarify that.

    Perhaps consider using a char for input if that's all you need.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    May 2008
    Posts
    12

    Re: Second part of my program repeats and...

    I'm using getline because the user might enter something like this: pouh reqh pourfh poieuh prfho (*%RT, heh... and I want to counter that.
    Last edited by Artitatt; June 11th, 2008 at 01:37 AM.

  4. #4
    Join Date
    Feb 2007
    Posts
    112

    Re: Second part of my program repeats and...

    A very crude way is to use two "cin". One for the string (y or n) and other for the "enter" ('/n') character.
    try it and see if it works.
    Code:
      cout << "Do you wish to quit? y/n ";
      cin >> quitKey;
      string extraChar;
      cin>>extraChar;

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Second part of my program repeats and...

    Quote Originally Posted by Mybowlcut
    }[/code]I think your problem has to do with the stream having stuff in it from the first cin statement and it affecting the getline statement but someone else can clarify that.
    Put simply, cin >> var will only read until it encounters whitespace; and those whitespace characters will remain in the stream.

    getline will read until it encounters a \n, and remove characters from the stream up to and including that \n.

    So if you do a cin >> var and then a getline, the getline just grabs the \n left in the stream by the cin, and returns immediately.

  6. #6
    Join Date
    May 2008
    Posts
    12

    Re: Second part of my program repeats and...

    Quote Originally Posted by pipa
    A very crude way is to use two "cin". One for the string (y or n) and other for the "enter" ('/n') character.
    try it and see if it works.
    Code:
      cout << "Do you wish to quit? y/n ";
      cin >> quitKey;
      string extraChar;
      cin>>extraChar;
    I tried your example and it didn't quite work then changed it to:

    string extraChar;
    getline(cin, extraChar);

    but now I have to enter in y or n twice as if to be validated.
    It looks better, but it's not quite fixed. Thanks for the idea. Can you think of anything else?

  7. #7
    Join Date
    May 2008
    Posts
    12

    Re: Second part of my program repeats and...

    I added cin.ignore()

    to

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int squared(int s)
    {
     return s * 2;
    }
    
    
    int main()
    {
     top:
     system("CLS");
     
     cout << "What integer do you want squared? ";
     int x;
     cin >> x;
     cout << squared(x) << endl;
    
    
     cin.ignore(); // newly added
    
    
     string quitKey = "y";
     do
     {
      topOfLoop:
      cout << "Do you wish to quit? y/n ";
      cin >> quitKey;
      if (quitKey == "y") return 1;
      if (quitKey == "n") goto top;
      else
          cout << "What? ";
          goto topOfLoop;
     } while (quitKey != "y");
    
     return 0;
    }
    and it works now. :-)
    Last edited by Artitatt; June 12th, 2008 at 12:33 AM.

  8. #8
    Join Date
    Feb 2007
    Posts
    112

    Re: Second part of my program repeats and...

    Quote Originally Posted by Artitatt
    I tried your example and it didn't quite work then changed it to:

    string extraChar;
    getline(cin, extraChar);

    but now I have to enter in y or n twice as if to be validated.
    It looks better, but it's not quite fixed. Thanks for the idea. Can you think of anything else?
    I did a similar kind of program in C long time back. I remember that I had to do the "reading in" operation twice as I had to compensate for the '/n' character.
    But I like ur cin.ignore() function. thats new to me.
    Good Luck!

  9. #9
    Join Date
    May 2008
    Posts
    12

    Re: Second part of my program repeats and...

    Quote Originally Posted by pipa
    I did a similar kind of program in C long time back. I remember that I had to do the "reading in" operation twice as I had to compensate for the '/n' character.
    But I like ur cin.ignore() function. thats new to me.
    Good Luck!
    Cool. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured