A messed up loop...?

closed account (o9ypX9L8)
I'm just starting to teach myself C++ from the tutorial on cprogramming.com, and I made a little command line program that asks you some questions and tells you if you win or fail. The problem is, if you enter a letter instead of a number, the program ends instead of asking you to try again. I've been trying to fix this myself for 2 hours and just got fed up with it. As the title suggests, I think it's a problem with the loop I used to allow it to restart.

I'm also curious to know if I'm writing good code, or if I should be doing things differently or something. You guys are the only feedback I have, since I'm not taking a class and none of my friends even know how to write a webpage.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;

int main() // VERY VERY VERY IMPORTANT TO REMEMBER THIS PART!
{

  int correct = 1; // Declaring the variables
  int re = 0;

  do
  {
      int a = 0;
      int b = 1;
      re = 0;

      cout<<"Enter 2 numbers that are the same...\n"; //Display the challenge
      cin>> a; // User's first imput becomes a
      cin>> b; // User's second input becomes b
      if ( a == b ) // Check that a and b are equal
      {
          cout<<"That's right!\nWhat is 4+3?\n"; // Display the success message and ask the next question
          cin>> a; // variable a is now being used for this question
          if ( a == 7 ) // check for the right answer
          {
              cout<<"You're good.\nWhat comes after 3857?\n"; // Lots of success
              cin>> a; // variable a is now being used for this question. You get the idea.
              cin.ignore();
              if ( a == 3858 ) // again, checking if the answer is right
              {
                  correct = 1;
              }
              else
              {
                  correct = 0; // telling the program that the answer is not correct
              }
          }
          else
          {
              correct = 0;
          }
          }
      else
      {
          correct = 0;
      }
      if ( correct == 1 )
      {
          cout<<"\nVICTORY SCREAM.\n"; // you win and the program ends
          re = 0;
      }
      else
      {
           cout<<"FAILURE!\n";
           cout<<"Enter 1 to try again\n";
           cin>> re;
           if ( re != 1 )
           {
               re = 0; // if you enter something other than 1, the loop will break
           }
      }
  }
  while ( re == 1 );
  cin.get();
}
Last edited on
You can check for cin.good() after input to see if the user entered a number
A way to validate input: http://www.cplusplus.com/forum/articles/6046/
Some functions to fully validate any kind of input: http://www.cplusplus.com/forum/beginner/13044/#msg62827
closed account (o9ypX9L8)
thanks so much. Sorry for such a generic question
I'm not sure if this will help you, but to make your code more maintainable, try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

bool add_qs(int a, int b); //declaration, allows for use w/o putting main() at bottom

int main(){
    bool correct;

    correct = add_qs(3, 4);

    if (correct)
    cout << "right" << endl;
    else //not correct
    cout << "wrong" << endl;

    return 0;}

bool add_qs(int a, int b){ //define our function here
    int ans; //temp variable local to function

    cout << "What is " << a << " + " << b << " ?" << endl;
    cin >> ans;

    if (ans==a+b)
    return true; //'correct' gets the return value, in this case 'true' is returned

    else //this else isn't even necessary, but this may make it more readable
    return false;}


The use of multiple functions allows for much easier changes to a program. This way, if you wanted to ask the user more questions, the amount of code needed to be added to main() would be minimal. You could also add the messages "wrong" and "right" right inside of the secondary function.
Last edited on
closed account (o9ypX9L8)
That's more advanced than what I can figure out right now. I can only do inputs, outputs, if's, and loops. But thanks for the help anyway; I will probably use it as I keep learning more. From what I can make out, that's exactly the kind of thing I would have trouble getting used to because I'm so used to writing html, which is annoyingly static.
Look into other data types like bool and double. I see you like int a lot, but bool could be used in this program for your first two variables, and it will make it look a little cleaner.
closed account (o9ypX9L8)
The tutorial I'm learning from hadn't told me about those data types yet, only int, so that's what I used. I've moved on beyond this stage in the program though and pretty much solved my problem. Thanks for the help!
Topic archived. No new replies allowed.