Help with loops

Hi there! Im a bit struggling with the loop conditions.

I need to write a basic program where the user is requested to enter a digit between 1-30 up to 5 times and display the same amount of * for the amount entered. Not counting the times they enter a incorrect number i.e. Above 30 or below 0.

The code below is what I have so far. The whole program does repeat 5 times and thats it.
How would I write it so that the program repeats 5 times only when correct numbers are entered and repeat unlimited or up to 50 times if incorrect numbers keep being put in.

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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main () {

  int digit;


cout <<"Enter 5 digits: " <<endl;

for (int j=0; j<5; j++){

    cout <<"\n" <<"-> ";
    cin >> digit;

            if(digit>30){
              cout <<"Please enter a digit between 1-30.";
              continue;
            }
            if(digit<0){
              cout <<"Please enter a digit between 1-30.";
              continue;
            }


            for(int i = 0; i<digit; i++){
              cout <<"*";
            }
}

  cin.sync();
  cin.get();
  return(0);
}




Any help or guidance would be really appreciated.
Last edited on
Hello wirelesskill,

I thought yo might have used a while loop, but the for loop works.

Right now you will input five numbers right or wrong.

I offer the following code for two reasons:

To show you the "j--" that will only count correct answers.

And to show hoe when the {}s line up in the same column it is easier to read and much easier to find a missing closing brace.

I changed the for condition so it would only work until "j" becomes less then zero causing the for loop to end because of to many incorrect choices. Otherwise it will loop until five correct numbers are added.

I will see what I can come up with using a 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
	constexpr int MAXTRIES{ 5 };

	int digit;

	cout << "Enter 5 digits: " << endl;

	for (int j = 0; j < MAXTRIES && j >= 0; j++)
	{

		cout << "\n" << "-> ";
		cin >> digit;

		if (digit > 30) 
		{
			cout << "Please enter a digit between 1-30.";
			j--;  // <--- Added.
			continue;
		}
		if (digit < 0)
		{
			cout << "Please enter a digit between 1-30.";
			j--;  // <--- Added.
			continue;
		}

		for (int i = 0; i < digit; i++)
		{
			cout << "*";
		}
	}

	cin.sync();
	cin.get();

	return(0);
}


Hope that helps,

Andy
I see where I went wrong.

Thank you so much Andy!
Hello wirelesskill,

You are welcome.

I was working with your code and have some suggestions:

Line 23 is a little misleading. It might work better as "Please enter a digit between 1-30 inclusive." and then the second if statement would need to be "< 1" to work properly. Also I would copy line 23 and paste it at line 14. It gives a better start to the output.

Your header files "iomanip' and "cmath" are not used at this time unless you have a use for them later.

In your if statements the numbers "30" and "0" I would do something like I did with "MAXTRIES", so they can easily be changed later if needed.

I found lines 40 and 41 did not work well when I first used your program. Generally I use this to pause the program before it ends:

1
2
3
4
5
6
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();


Once you understand it the comments do not have to be there.

Just for fun I changed the for loop to a while loop. Give it a look and see what you think.

Any questions just ask.

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 <string>
//#include <iomanip>  // <--- Not used right now.
//#include <cmath>  // <--- Not used right now.

using namespace std;

int main()
{
	constexpr int MAXTRIES{ 5 };
	const std::string TRIES[]{ "first", "second", "third", "forth", "fifth" };
	int digit{}, count{};

	cout << "Enter 5 digits: " << endl;
        cout << "Please enter a digit between 1-30 inclusive." << std::endl;

	while (count < MAXTRIES)
	{
		cout << "\n" << TRIES[count] << " number -> ";
		cin >> digit;

		// <--- Stays in loop until a correct number is entered.
		while (digit < 1 && digit>30)
		{
			cout << "Please enter a digit between 1-30 inclusive.";
			cout << "\n" << TRIES[count] << " number -> ";
			cin >> digit;
		}

		for (int i = 0; i < digit; i++)
		{
			cout << "*";
		}

		count++;  // <--- Only counts correct answers.

		std::cout << std::endl;
	}


	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	//cin.sync();
	//cin.get();

	return(0);
}


Have fun coding,

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.