trying to get my program to repeat itself

I am trying to get my program to repeat itself only if the user wants it to
this is what i have come up with so far but it doesnt work it gets to the first input for total then just says press any key to continue then it closes

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){

    int total, again, n1, y, n;
   
    cout << "Welcome to Hell. Wanna play?" << endl;
    cout << " Pick a number any number I can handle it." << endl;
    cin >> total;

    while (total < 1) {
    cout << " need a number greater then 0" << endl;
    cin >> total;
    }
      while ( again == y){
    while(total > 0) {
        if ((total % 3) == 2){
                total = total - 2;
                cout << "I'll take 2 whether you like it or not. =P" << endl;
        } else {
                total--;
                cout << " I'm taking one mo fucka." << endl;
        }
        cout << " Only " << total << " left. Can you even count that high?" << endl;
        if (total == 0) {
                cout << " HAHAHA I WIN I WIN I WIN !!!!!!!!!!!!!!!!" << endl;
                     break;
   
   }
   
   cout << "Pick a 1 or 2 I promise it doesn't matter though I'll still win  ";
   cin >> n1;
   while (n1 < 1 || n1 > 2){
    cout << "Hey I thought I told you 1 or 2 try again numskull " << endl;
    cin >> n1;
    }
    total = total - n1;
    cout << " Okay we are at " << total << " Good job you made me do the math for you now lets move on" << endl;
        if (total == 0) {
            cout << " Wow whats a shocker you won. Good job you beat something that can't even think on its own" << endl;
            break;
            }
        }
 cout << " wanna go again? y/n" <<endl;
 cin >> again; 
 }
  system ("PAUSE");	
  return 0;
}
Your asking the user to enter a letter as an input to a variable of type integer. You might change again

to

char again='y';

and while(again=='y')

And remember that your variables

int total, again, n1, y, n;

are not initialized. They each are for storing values of a certain type. again, n1, y, and n are just names for the variables.
Last edited on
what do u mean they are not initialized?
i got it to work this is what i came up with but im still getting [warning] In function 'int:
and [warning] unused on line 8

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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){

    int total, n1, n;
   char again = 'y';
     while ( again == 'y'){
    cout << "Welcome to Hell. Wanna play?" << endl;
    cout << " Pick a number any number I can handle it." << endl;
    cin >> total;

    while (total < 1) {
    cout << " need a number greater then 0" << endl;
    cin >> total;
    }
    
    while(total > 0) {
        if ((total % 3) == 2){
                total = total - 2;
                cout << "I'll take 2 whether you like it or not. =P" << endl;
        } else {
                total--;
                cout << " I'm taking one mo fucka." << endl;
        }
        cout << " Only " << total << " left. Can you even count that high?" << endl;
        if (total == 0) {
                cout << " HAHAHA I WIN I WIN I WIN !!!!!!!!!!!!!!!!" << endl;
                     break;
   
   }
   
   cout << "Pick a 1 or 2 I promise it doesn't matter though I'll still win  ";
   cin >> n1;
   while (n1 < 1 || n1 > 2){
    cout << "Hey I thought I told you 1 or 2 try again numskull " << endl;
    cin >> n1;
    }
    total = total - n1;
    cout << " Okay we are at " << total << " Good job you made me do the math for you now lets move on" << endl;
        if (total == 0) {
            cout << " Wow whats a shocker you won. Good job you beat something that can't even think on its own" << endl;
            break;
            }
        }
 cout << " wanna go again? y/n" <<endl;
 cin >> again; 
 }
  system ("PAUSE");	
  return 0;
}
You declared them, but did not assign them an initial value, where you wrote,
while (again==y), the compiler was checking if the values held by the variables were equal. You had not assigned either a value, until at the end cin >> again. But you asked for a y or n, to be stored in a variable which can only hold numbers.

By initialize I just mean.

1
2
3
4
5
6
int y;
y=1;

or int y=1;

or int y=1,x=2;


The values of again, and y, were probably 0 by default.
Last edited on
i didnt want them to hold a value until it was input by the user
is there a better way to do that? and is char the variable if i want to use a letter?
If you don't want them to hold a value, then you shouldn't check the value they hold prior to a user entering a value for them.

Yes to char.

And please see http://www.parashift.com/c++-faq-lite/input-output.html for the problem you are likely to have after you make the change..


wow umm some of that stuff seems a bit out of my reach atm im just starting do u think my book will go over something like that later or should it have done it when i first started working on this project?
Topic archived. No new replies allowed.