Im making a game

Pages: 12
i just posted them in the lounge thanks.
and thanks for all the help on this "game" here as well . i was lost in the darkness haha
would you use a while loop or a for loop to repeat this 10x ?
Last edited on
I'd use a for loop. Also, make sure you declare a couple of variables BEFORE the for loop like
1
2
int numCorrectGuesses = 0;
int numWrongGuesses = 0;

and then update them accordingly in the loop. Like, next to the output statement that tells them their answer is wrong is the appropriate place to add 1 to the numWrongGuesses counter. Same for correct guesses, you gotta figure out where in the code it is appropriate to add 1 to numCorrectGuesses.
Last edited on
ok thanks so much.
i put
1
2
int numRightGuesses = 0;
int numWrongGuesses = 0;


at the top of the code , and i put them again at the bottom but whenever i try to increase them with ++ it just errors.

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
66
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

using namespace std;

int main()
{
    cout << " Hello, " << endl;
    cout << " I am going to show you a number between 1-100. " << endl;
    cout << " I would like you to tell me if it is a Perfect, Abundant, or Deficient. " << endl;
    cout << " Press Enter to Continue " << endl;
    cin.ignore(100, '\n');
    int numRightGuesses = 0;
    int numWrongGuesses = 0;
    
    srand(time(0));//Starts random number generator
    int number = rand() % 99 + 1;//Using Modulus operator to isolate values  99+1
    cout << number;
    cin.ignore(100, '\n');
    cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
    cout << " Make sure you use capitol letters when selecting!" << endl;
    cout << " Press A if you think it's a PERFECT NUMBER" << endl;
    cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
    cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
    char Guess;
    cin >> Guess;

    int rand,one = 1, sum = 0;
    while (one < number)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }

    if(sum == number && Guess == 'A')  //added another condition
    {

        cout << "You are correct! This is a Perfect Number!";
        int numRightGuesses;// i wanted to increase it here but its not working
    }
    else if (sum > number && Guess == 'B')  //added another condition
    {

        cout << "You are correct! This is an Abundant Number!";
        int numRightGuesses;// and increase here
    }
    else if (sum < number && Guess == 'C')  //added another condition
    {

        cout << "You are correct! This is a Deficient Number!";
        int numRightGuesses;// and increase here
    }
    else
    {
        cout << "Sorry, you are incorrect!";
        int numWrongGuesses;// and again here , but obv for wrong answers
    }
    {
        cout << numWrongGuesses << " Wrong" << endl;
        cout << numRightGuesses << "  Right" << endl;
    }
    return 0;
}
my code looks like this now , and its hanging... im trying to keep score and loop the whole thing 10x
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
66
67
68
69
70
71
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

using namespace std;


int main()
{
    cout << " Hello, " << endl;
    cout << " I am going to show you a number between 1-100. " << endl;
    cout << " I would like you to tell me if it is a Perfect, Abundant, or Deficient. " << endl;
    cout << " Press Enter to Continue " << endl;
    cin.ignore(100, '\n');

    int numRightGuesses = 0, numWrongGuesses = 0, pos = 0, neg = 0, score = 0;
    {
        pos+= numRightGuesses;
        neg+= numWrongGuesses;
        score+=numRightGuesses,numWrongGuesses;
    }
    srand(time(0));//Starts random number generator
    int number = rand() % 99 + 1;//Using Modulus operator to isolate values  99+1
    cout << number;
    cin.ignore(100, '\n');
    cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
    cout << " Make sure you use capitol letters when selecting!" << endl;
    cout << " Press A if you think it's a PERFECT NUMBER" << endl;
    cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
    cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
    char Guess;
    cin >> Guess;

    int rand,one = 1, sum = 0;
    while (score < 10)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }

    if(sum == number && Guess == 'A')  //added another condition
    {

        cout << "You are correct! This is a Perfect Number!";
        int numRightGuesses;
    }
    else if (sum > number && Guess == 'B')  //added another condition
    {

        cout << "You are correct! This is an Abundant Number!";
        int numRightGuesses;
    }
    else if (sum < number && Guess == 'C')  //added another condition
    {

        cout << "You are correct! This is a Deficient Number!";
        int numRightGuesses;
    }
    else
    {
        cout << "Sorry, you are incorrect!";
        int numWrongGuesses;
    }
    {
        cout << numWrongGuesses << " Right" << endl;
        cout << numRightGuesses << " Wrong" << endl;
    }
    return 0;
}
For one thing, all this does nothing, 0 + 0 = 0.
1
2
3
4
5
6
int numRightGuesses = 0, numWrongGuesses = 0, pos = 0, neg = 0, score = 0;
    {
        pos+= numRightGuesses;
        neg+= numWrongGuesses;
        score+=numRightGuesses,numWrongGuesses;
    }


And here, score is always < 10 since it is never incremented and stays at 0.
1
2
3
4
5
6
while (score < 10)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }


yes .... i changed this...
1
2
3
4
5
6
while (score < 10)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }


back to this

1
2
3
4
5
6
    while (one < number)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }


it compliles again and dosnt hang anywhere but like you said i have a lot of garbage code that i added

1
2
3
4
5
6
int numRightGuesses = 0, numWrongGuesses = 0, pos = 0, neg = 0, score = 0;
    {
        pos+= numRightGuesses;
        neg+= numWrongGuesses;
        score+=numRightGuesses,numWrongGuesses;
    }


i thought that 0 was supposed to act as a place holder like an empty bucket if you will....
Last edited on
It never hurts to initialize a variable to value, but if you substitute the values for the variables you are just adding 0 to 0. Also the brace serve no purpose that I can tell just like here.
1
2
3
4
    {
        cout << numWrongGuesses << " Right" << endl;
        cout << numRightGuesses << " Wrong" << endl;
    }


Which unless I'm missing something would output
0 Right
0 Wrong


i wanted it to output 0 right, 0 wrong but i want it to count for you. so out of 10 it could say 6 right 4 wrong
that is what i have been stuck on trying to do today, have it count or keep score kinda deal and loop itself 10x
That is what I figured. All you need to do is basically put most of it inside a do/while loop. Obviously the results wont go inside, but you can't just put everything else inside. You don't want to be resetting numRightGuesses back to 0 each time for example or any other counting variables. I'd suggest cleaning up all the unused stuff before going any farther though so your code is clearer to read.
i had it in a while loop and it wouldnt compile so i tried a for loop and it would compile and start hanging/not finish running throughh the code. now im sure it wasnt comppiling in the while loop because of something that i did , im too green at coding .

ive taken out alot of the code that did nothing , it now looks like this agan (which kinda sucks cause this is what it looked like last night ..saaad face )

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
66
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

using namespace std;

int main()
{
    cout << " Hello, " << endl;
    cout << " I am going to show you a number between 1-100. " << endl;
    cout << " I would like you to tell me if it is a Perfect, Abundant, or Deficient. " << endl;
    cout << " Press Enter to Continue " << endl;
    cin.ignore(100, '\n');
    int numRightGuesses = 0;
    int numWrongGuesses = 0;

    srand(time(0));//Starts random number generator
    int number = rand() % 99 + 1;//Using Modulus operator to isolate values  99+1
    cout << number;
    cin.ignore(100, '\n');
    cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
    cout << " Make sure you use capitol letters when selecting!" << endl;
    cout << " Press A if you think it's a PERFECT NUMBER" << endl;
    cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
    cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
    char Guess;
    cin >> Guess;

    int rand,one = 1, sum = 0;
    while (one < number)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }

    if(sum == number && Guess == 'A')  //added another condition
    {

        cout << "You are correct! This is a Perfect Number!";
        int numRightGuesses;
    }
    else if (sum > number && Guess == 'B')  //added another condition
    {

        cout << "You are correct! This is an Abundant Number!";
        int numRightGuesses;
    }
    else if (sum < number && Guess == 'C')  //added another condition
    {

        cout << "You are correct! This is a Deficient Number!";
        int numRightGuesses;
    }
    else
    {
        cout << "Sorry, you are incorrect!";
        int numWrongGuesses;
    }
    {
        cout << numWrongGuesses << " Wrong" << endl;
        cout << numRightGuesses << " Right" << endl;
    }
    return 0;
}
What did you have in the while loop? Just putting this part inside compiles fine, but doesn't give the right results.
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
while (one < number) // will loop as many times as the value of the random number
    {
        if(number % one == 0)
            sum = sum + one;
        one++;

		if(sum == number && Guess == 'A')  //added another condition
		{

			cout << "You are correct! This is a Perfect Number!";
			int numRightGuesses;
		}
		else if (sum > number && Guess == 'B')  //added another condition
		{

			cout << "You are correct! This is an Abundant Number!";
			int numRightGuesses;
		}
		else if (sum < number && Guess == 'C')  //added another condition
		{

			cout << "You are correct! This is a Deficient Number!";
			int numRightGuesses;
		}
		else
		{
			cout << "Sorry, you are incorrect!";
			int numWrongGuesses;
		}
	}

int rand,one = 1, sum = 0;
rand is never used and it's not a good idea to name a variable the same as a function either. I don't understand what one and sum are supposed to do exactly? I don't know number theory so I'd need to take a quick "dummy" lesson to figure out how to determine if your logic is right in picking a right or wrong answer, but I think you are trying to use a single variable to do two different things.
what do you sudgest i do ?
Since your trouble is with the loop and you have a bit of a mess, start a new project and get the loop working, 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
        int number;
	int numRight = 0;
	int numWrong = 0;
	char guess;
	bool odd = true;
	int tries = 0;

	do
	{
		number = rand() % 99 + 1;
		cout << "Tell me if the following number is odd or even (O/E): " << number << '\n';
		cin>> guess;

		if(number % 2 == 0)
		{
			odd = false;
		}

		if((odd && (guess == 'o' || guess == 'O')) || (!odd && (guess == 'e' || guess == 'E')))
		{
			cout << "You are correct." << '\n';
			numRight++;
		}
		else 
		{
			cout << "You are incorrect." << '\n';
			numWrong++;
		}
		tries++;
		odd = true;	// reset odd
	} while (tries < 10);

	cout << "You had " << numRight << " correct guesses." << '\n';
	cout << "You had " << numWrong << " incorrect guesses." << '\n';

	return 0;
}

Then just replace the questions and answers.
thanks but ive done this now.....

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
66
67
68
69
70
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

using namespace std;
int number, Guess, numRightGuesses = 0, numWrongGuesses = 0, count = count +1;

int main()
{
    cout << " Hello, " << endl;
    cout << " I am going to show you a number between 1-100. " << endl;
    cout << " I would like you to tell me if it is a Perfect, Abundant, or Deficient. " << endl;
    cout << " Press Enter to Continue " << endl;
    cin.ignore(100, '\n');

{
while(count <= 10)

{
    srand(time(0));//Starts random number generator
    int number = rand() % 99 + 1;//Using Modulus operator to isolate values  99+1
    cout << number;
    cin.ignore(100, '\n');
    cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
    cout << " Make sure you use capitol letters when selecting!" << endl;
    cout << " Press A if you think it's a PERFECT NUMBER" << endl;
    cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
    cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
    char Guess;
    cin >> Guess;
}
    int rand,one = 1, sum = 0;
    while (one < number)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }

    if(sum == number && Guess == 'A')  //added another condition
    {

        cout << "You are correct! This is a Perfect Number!";
        int numRightGuesses;
    }
    else if (sum > number && Guess == 'B')  //added another condition
    {

        cout << "You are correct! This is an Abundant Number!";
        int numRightGuesses;
    }
    else if (sum < number && Guess == 'C')  //added another condition
    {

        cout << "You are correct! This is a Deficient Number!";
        int numRightGuesses;
    }
    else
    {
        cout << "Sorry, you are incorrect!";
        int numWrongGuesses;
    }
    {
        cout << numWrongGuesses << " Wrong" << endl;
        cout << numRightGuesses << " Right" << endl;
    }
    return 0;
}
}


and its finnally looping , however its skipping over alot of the code for some reason
Actually it never goes past line 32. Because count is never incremented, its value of 1 never changes. Just initialize it to 0 and have the condition be (count < 10) and increment it at the end of the loop. If you would stop adding useless braces and get rid of those you have; lines 17, 64, 67, and 70 at least, it would be easier to read your code. In order to include all the if/else statements, you need to put those inside the loop also.
I've spotted another problem on lines 51,57, and 62. Where it says "int numRightGuesses" or "int numWrongGuesses", in each case it should either be numRightGuesses++ or numWrongGuesses++ to increment the values. As it is, you are redeclaring existing values, but not assigning any value to them. Why is count initialized to count+1? Shouldn't it merely be initialized to 1?

Also, rand cannot be used as an int variable. Other than that, I think admkrk covered it pretty well when he talked about your formatting. While it makes sense for an if or else statement to be within braces, as well as loops, you seem to be bracing other portions of code that hurt readability (such as lines 64 through 67). There's also an extra brace that does not belong prior to your while loop on line 17, and a redundant brace on line 70.

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <limits>

using namespace std;
int number, Guess, numRightGuesses = 0, numWrongGuesses = 0, count = 1, one=1,sum=0;

int main()
{
    cout << " Hello, " << endl;
    cout << " I am going to show you a number between 1-100. " << endl;
    cout << " I would like you to tell me if it is a Perfect, Abundant, or Deficient. " << endl;
    cout << " Press Enter to Continue " << endl;
    cin.ignore(100, '\n');

while(count <= 10)
{
    srand(time(0));//Starts random number generator
    int number = rand() % 99 + 1;//Using Modulus operator to isolate values  99+1
    cout << number;
    cin.ignore(100, '\n');
    cout << " Is this number a Perfect, Abundant, or Deficient ? " << endl;
    cout << " Make sure you use capitol letters when selecting!" << endl;
    cout << " Press A if you think it's a PERFECT NUMBER" << endl;
    cout << " Press B if you think it's a ABUNDANT NUMBER" << endl;
    cout << " Press C if you think it's a DEFICIENT NUMBER" << endl;
    char Guess;
    cin >> Guess;
    count++;
}//end of first while loop
    while (one < number)
    {
        if(number % one == 0)
            sum = sum + one;
        one++;
    }

    if(sum == number && Guess == 'A')  //added another condition
    {
     cout << "You are correct! This is a Perfect Number!";
     numRightGuesses++;
    }
    else if (sum > number && Guess == 'B')  //added another condition
        {
        cout << "You are correct! This is an Abundant Number!";
        numRightGuesses++;
        }
         else if (sum < number && Guess == 'C')  //added another condition
               {
              cout << "You are correct! This is a Deficient Number!";
              numRightGuesses++;
               }
                   else
                      {
                      cout << "Sorry, you are incorrect!";
                      numWrongGuesses++;
                      }
  
        cout << numWrongGuesses << " Wrong" << endl;
        cout << numRightGuesses << " Right" << endl;
        return 0;
}//end of main 


Let me know if this compiles properly, I implemented the mentioned changes, moved all variable declarations to the top, and tried to demonstrate how formatting and indentation can help readability and avoid confusion with braces.
Last edited on
Topic archived. No new replies allowed.
Pages: 12