Why isnt it looping?

I tried to loop it but it doesn't let the user type after the first go
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
#include <iostream>

using namespace std;

int main()

{

    int i;
  cout << "Please type 2816" << endl;
  cin >> i;

    if (i > 2816)
    {
        cout << "Too big" << endl;
    }
    else if (i < 2816)
    {
        cout << "Too small" << endl;
    }
    else if (i = 2816)
    {
        cout << "Thanks" << endl;
    }
    while (i != 2816);
    return 0;
}
What kind of loop are you trying to do?
The while-loop, do-while loop, or the for loop. My assumption is the do-while loop.

Example of the while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This program demonstrates a simple while loop.
#include <iostream>
using namespace std;

int main()
{
   int number = 0;

   while (number < 5)
   {
      cout << "Hello\n";
      number++;
   }
   cout << "That's all!\n";
   return 0;
}


Example of the do-while loop:
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
// This program averages 3 test scores. It repeats as
// many times as the user wishes.
#include <iostream>
using namespace std;

int main()
{
   int score1, score2, score3; // Three scores
   double average;             // Average score
   char again;                 // To hold Y or N input

   do
   {
      // Get three scores.
      cout << "Enter 3 scores and I will average them: ";
      cin >> score1 >> score2 >> score3;
      
      // Calculate and display the average.
      average = (score1 + score2 + score3) / 3.0;
      cout << "The average is " << average << ".\n";
      
      // Does the user want to average another set?
      cout << "Do you want to average another set? (Y/N) ";
      cin >> again;
   } while (again == 'Y' || again == 'y');
   return 0;
}
Last edited on
Maybe you could use the do-while loop like:

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;

int main()

{

    int i;
    do
    {
       cout << "Please type 2816" << endl;
       cin >> i;

	if (i > 2816)
	{
            cout << "Too big" << endl;
	}
	else if (i < 2816)
	{
	     cout << "Too small" << endl;
	}
	else if /*(i = 2816)*/ (i == 2816)
	{
             cout << "Thanks" << endl;
	}
    } while (i != 2816);
    return 0;
}


PS: @TheIdeasMan. Thanks for the correction.
Last edited on
Tried that it didnt work :/
typo on line 23 :+)

Compiler told me that.
Topic archived. No new replies allowed.