Do while

Trying to work this example out of my textbook and don't understand why it functions correctly with inputs of a=3 b=7 but not with a=0 b=3. Am I missing something? It seems to compile and run properly with whatever input I throw at it. Maybe just a glitch in the e-book? I'm using a chegg copy of Big C++ Late Objects 3rd edition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;

int main()
{
   int a, b;

   // Keep prompting the user until the input is correct
   do {
   cout << "Enter two positive integers, the first smaller than the second." 
      << endl;
   cout << "First: " << endl;
   cin >> a;
   cout << "Second: " << endl;
   cin >> b;
   }
   while(a > b);
   // Only print this when the input is correct
   cout << "You entered " << a << " and " << b << endl;
}
Last edited on
It works when I compile and run the code on my machine and in the C++ Shell available through the "Edit & Run" Button.

Edit: although something happens when a == b that you may not like.
Last edited on
So... just change the conditions required to stop the loop, like this?

 
while (a > b || a == b);


EDIT: Tried it that way in the Chegg textbook. No change. Part of the problem is that it doesn't give you the initial inputs unless the code executes properly.
Last edited on
That's a bit verbose (you could use a >= b instead), but yes. My main point was that code generally seems to work as desired (including the case where a == 0, b == 3
I'm not sure then. Maybe you could try putting return 0; at the end of your code. I've had strange behavior when working with automatic graders which provide the input.
Hello dakotad8218,

I have tested the program in the shell and in VS 2017 and come to the conclusion that it works, but maybe it you not understanding something.

The comments here should cover what is happening. If not let me know.
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
#include <iostream>

using namespace std;

int main()
{
	int a, b;

	// Keep prompting the user until the input is correct
	do
	{
		cout << "\n Enter two positive integers, the first smaller than the second.\n"
			<< endl;

		cout << " First: ";
		cin >> a;

		cout << " Second: ";
		cin >> b;

	} while (a > b);  // <--- Back to the top of the loop to enter numbers correctly. [a < b or a == b] will cause the condition to fail.

	// Only print this when the input is correct. Which is when a < b or a == b.
	cout << "\n You entered " << a << " and " << b << endl;

	return 0;  // <--- Not required, but makes a good break point.
}


A couple of suggestions. When using {}s be consistent with their use. https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements
Out of all the styles I think the "Allman" style is the easiest to read.

Also a few blank lines break up the code and make it easier to follow.

Lastly try to avoid single letter variable names. Something that describes what it is or what it is used for helps. It is mostly for your benefit.

Andy
Topic archived. No new replies allowed.