loops

what do i need to do to loop this 10 times
i keep trying to put it in a while loop because i dont know enough about a for loop.
when i put it in a while loop this only runs the first chunk of my code and then hangs...
the code i have provided is only the code that i want looped...

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
     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;
}


ive been bashing my head against a wall now for hours, im feeling really stupid
I know using loops are a bit hard!!!!
But you should use a for!!!

for(one = 1; one <= 10; one++)

It should be looping 10 times.
Last edited on
still no luck....
im not sure what i could be doing wrong, I put what you told me right at the top of the code you had provided and it told me one was not declared in the scope. So i then deleted the declared int one = 1; which was beneath the for loop and placed it above. it then compiled ( i got excited) ran thru the program once and it closed , just like every other time ... anyways updated code looks as follows 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
    int one = 1;
    for(one = 1; one <= 10; one++)
    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, 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;
}
}


edit* to my eyes i think i might be using the interger one=1 to do too much , any people out there agree with me ? or am i off base
Last edited on
any help at all would be appreciated
Instead of using return to stop the cmd from disappearing use the
_getch()
at the end of the program...
And u should put the srand() outside, I mean above the for!
Last edited on
but i need it inside to continue to generate a random number ?

and i have never been taught
_getch()

is there any other possible way ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
srand(time(NULL));
	for(nblancer = 1; nblancer <= 1000000; nblancer*= 10)
	{
		resultat1 = 0, resultat2 = 0, resultat3 = 0, resultat4 = 0, resultat5 = 0, resultat6 = 0;
		for (int i = 1; i <= nblancer; i++)
		{
			d = rand() % 6 + 1;
			switch(d)
			{
				case 1: ++resultat1; break;
				case 2: ++resultat2; break;
				case 3: ++resultat3; break;
				case 4: ++resultat4; break;
				case 5: ++resultat5; break;
				case 6: ++resultat6; break;
			}
		}


This is something I did to generate rolling dice for school. It`s only half of it!!!!
Last edited on
Topic archived. No new replies allowed.