While Statements

I am having some problems with the program I am working on.
Thanks for your help!
Sarah

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
/* This program will ask the user for two numbers between 0 and 100.  The second number must be
larger than the first.  Then, the program will compute the sum of all the numbers between and including 
the numbers the user chose.  If the user includes an invalid number, the program will ask the user
to reread the instructions.

*/
#include <iostream>
using namespace std;
int main ()
{ 
	int num1, num2, total, counter=1;
	while (num1>=0 && num1<=100) 
	{
		cout << "Please enter an integer between 0 and 100: ";
		cin >> num1;
	}
	while (num2>=0 && num2<=100)
	while (num1<=num2)
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
	}	
	while (counter=num2)
	{
		total = num1+counter;
		counter++;
		cout << "the total of the integers between " << num1 << " and " << num2 << "is: " << total << endl;
	}
	if (num1!>=0 && num1 !<=100) or if (num2!>=0 && num2!<=100 && num2!>=num1)
	{
		cout << "Please read and follow the directions!" << endl; 
	}
}


And what might those problems be?
I assume counter=num2 should've been counter==num2, so that might be one of your problems.
A small suggestion:

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

    do
    {
        do
        {
            //get num1 here...

        } while (!(num1>=0 && num1<=100));

        do
        {
            //get num2 here...

        } while (!(num2>=0 && num2<=100));

    } while (!(num1<=num2));

    //calculate sum...

    return 0;
}
I made some changes on this program but am still having problems:
There are no errors but the program isn't running as it should be. The first question is generated but the second question, where the number asked for should be between num1 and 100 is only asking for the number to be between 0 and 100.

Secondly, the program gets stuck asking the first two questions again and again without moving on to adding the sum of all the numbers including and between num1 and num2.

Additionally, if the input is out of the 0 to 100 range, it is still accepted.


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
/* This program will ask the user for two numbers between 0 and 100.  The second number must be
larger than the first.  Then, the program will compute the sum of all the numbers between and including 
the numbers the user chose.  If the user includes an invalid number, the program will ask the user
to reread the instructions.

*/
#include <iostream>
using namespace std;
int main ()
{ 
	int num1, num2, total, counter=1;
	do
	{
		cout << "Please enter an integer between 0 and 100: ";
		cin >> num1;
	} while ((num1>=0) && (num1<=100));
	do
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
	} while ((num2>=0) && (num2<=100));
	while (num1<=num2);
	do
	{
		total = num1+counter;
		counter++;
		cout << "the total of the integers between " << num1 << " and " << num2 << "is: " << total << endl;
	} while (counter==num2);
	return 0;
	if (!(num1>=0) && !(num1<=100))
	if (!(num2>=0) && !(num2<=100) && !(num2>=num1))
	{
		cout << "Please read and follow the directions!" << endl; 
	}
}


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
#include <iostream>
using namespace std;
int main ()
{ 
	int num1, num2, total, counter=1;

	do
	{
		cout << "Please enter an integer between 0 and 100: ";
		cin >> num1;
	} while ((num1>=0) && (num1<=100));

	do
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
//	} while ((num2>=0) && (num2<=100));

	// I think that should probably be
	} while ((num2>=num1) && (num2<=100));

	// I don't know what this is for, but it will either do nothing or loop forever
	// while (num1<=num2); // the semicolon completes this tiny loop

	do
	{
		total = num1+counter;
		counter++;
		cout << "the total of the integers between " << num1 << " and " << num2 << "is: " << total << endl;
	} while (counter==num2);
	return 0;
	if (!(num1>=0) && !(num1<=100)) // Not sure what this is for?
	if (!(num2>=0) && !(num2<=100) && !(num2>=num1))
	{
		cout << "Please read and follow the directions!" << endl; 
	}
}


Actually m4ster r0shi's solution works but not quite optimal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main ()
{
    int num1, num2;

    do
    {
        //get num1 here...

    } while (num1 < 0 || num1> 100);

    do
    {
        //get num2 here...

    } while (num2 <= num1 || num2 > 100); // make sure bigger than previous

    //calculate sum...

    return 0;
}
Last edited on
I tried making those changes but it still wasn't working properly. Here's what I have now. I'm having some problems with "total" not being initiated. In addition to this, I am still having difficulties getting the "Please read and follow the directions!" to display if the inputs are invalid.

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
/* This program will ask the user for two numbers between 0 and 100.  The second number must be
larger than the first.  Then, the program will compute the sum of all the numbers between and including 
the numbers the user chose.  If the user includes an invalid number, the program will ask the user
to reread the instructions.

*/
#include <iostream>
using namespace std;
int main ()
{ 
	int num1, num2, counter=1;
	int total=0+counter;
	cout << "Please enter an integer between 0 and 100: ";
	cin >> num1;
	while (num1 >=0 || num1 <=100)
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
		if (num2 >= num1 || num2 <= 100)
		{
			while(counter==num2)
			total = num1+counter;
			counter++;
			cout << "the total of the integers between " << num1 << " and " << num2 << "is: " << total << endl;
			break;
		}
		else 
		{
			cout << "Please read and follow the directions!" << endl; 
		}
	}
}
slg5094 wrote:
I tried making those changes but it still wasn't working properly.


Honestly my last code structure works correctly. I recommend studying it and trying again with it:

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

int main ()
{
    int num1, num2;

    do
    {
        // get num1 using something like std::cin >> num1;

    } while (num1 < 0 || num1> 100);

    do
    {
    	 // get num2 using something like std::cin >> num2;

    } while (num2 <= num1 || num2 > 100); // make sure bigger than previous

    //calculate sum...

    return 0;
}
Okay, almost everything is working now. Thanks again for your help. Just one more issue: when the first number entered is invalid, the "Please read and follow the directions!" response pops up twice. How can I stop the code from continuing if the first number entered is invalid?

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
/* This program will ask the user for two numbers between 0 and 100.  The second number must be
larger than the first.  Then, the program will compute the sum of all the numbers between and including 
the numbers the user chose.  If the user includes an invalid number, the program will ask the user
to reread the instructions.

*/
#include <iostream>
using namespace std;
int main ()
{ 
	int num1, num2, total=0, counter;
	cout << "Please enter an integer between 0 and 100: ";
	cin >> num1;
	if (num1 >=0 && num1 <=100)
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
	}
	else 
	{
		cout << "Please read and follow the directions!";
	}
	if (num2>=num1 && num2 <=100)
	{
			for (counter=num1; counter<=num2; counter++)
			total+=counter;
			cout << "The total of the integers between " << num1 << " and " << num2 << " is " << total << endl;
	}
	else
	{
		cout << "Please read and follow the directions!" <<endl;
	}
}
The only way you can do that is with a loop.

Looking at your requirements again you need to do something like this:

1) Ask for input
2) Is input valid?
3) [NO] Tell to follow directions
4) Ask for input again
5) loop to (2)
6) [YES] comtinue....


1
2
3
4
5
6
7
8
	cout << "Please enter an integer between 0 and 100: "; // (1) Ask for input
	cin >> num1;
	while(num1 < 0 || num1 > 100) //  (2) Is input valid?
	{	// [NO]
		cout << "Please read and follow the directions!" << endl; // (3) Tell to follow directions
		cout << "Please enter an integer between 0 and 100: "; // (4) Ask for input again
		cin >> num1;
	} // (5) loop to 2 
Last edited on
Here, I got this to work as it should. I believe one problem was you were using the wrong conditions in the while's, e.g. while (num1 >= 0 && num1 <= 100) instead of while (num1 < 0 || num1 > 100). That meant "while num1 is greater than 0 and less than 100, *Prompt the user*. It should only re-prompt when that's NOT the case. Also not the || i used, meaning OR. You could use 'or' as well, it looks more intuitive, but it doesn't feel right to me. But it does the same thing, same with '&&' and 'and'.

Also, note the use of "Do...While" instead of just While. That way it will prompt first one time, before checking the condition and prompting more if it is true.

I will mark what I changed with comments later, if need be. Otherwise if you can figure it out yourself, that'd be great.

Well, I'll mark a couple things I can think of right off the bat...

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
/*
 * This program will ask the user for two numbers between 0 and 100.
 * The second number must be larger than the first.
 * Then, the program will compute the sum of all the numbers between and
 * including the numbers the user chose. If the user includes an invalid number,
 * the program will ask the user to reread the instructions.
 */
#include <iostream>
using namespace std;

int num1, num2, total, counter; // I changed 'total = 0' to 'total', because it is initialized as 0 by default.

int main() {
	do { //using do, while
		cout << "Please enter an integer between 0 and 100: ";
		cin >> num1;
	} while (num1 < 0 || num1 > 100);

	do {
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
	} while (num2 < num1 || num2 > 100);
	for (counter = num1; counter <= num2; counter++)
		total += counter;
	cout << "The total of the integers between " << num1 << " and " << num2
			<< " is " << total << endl;
}
// got rid of 'directions', it now just keeps asking, you can fix that if you want
// or make it say "error: invalid integer" and then prompt again. 
Last edited on
Ryan,
Thanks for your help. I understand everything you did. I have one question though: how/where can I add the read the directions response if the integer is invalid?
Thanks,
Sarah
Sorry, hang on, I'll put that back where it should be.

Well, I guess you can either use do...while, without the directions, or just while and put the directions and a new prompt if the int is invalid.

OK, this should do it:

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
/*
 * This program will ask the user for two numbers between 0 and 100.
 * The second number must be larger than the first.
 * Then, the program will compute the sum of all the numbers between and
 * including the numbers the user chose. If the user includes an invalid number,
 * the program will ask the user to reread the instructions.
 */
#include <iostream>
using namespace std;

int num1, num2, total, counter;

int main() {
  cout << "Please enter an integer between 0 and 100: ";
  cin >> num1;
  while (num1 < 0 || num1 > 100) {
    cerr << "Error: Invalid integer. Please follow the directions!\n" << endl;
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
  }
  cout << "Please enter an integer between " << num1 << " and 100: ";
  cin >> num2;
  while (num2 < num1 || num2 > 100) {
    cerr << "Error: Invalid integer. Please follow the directions!" << endl;
    cout << "Please enter an integer between " << num1 << " and 100: ";
    cin >> num2;
  }
  for (counter = num1; counter <= num2; counter++)
    total += counter;
  cout << "The total of the integers between " << num1 << " and " << num2
       << " is " << total << endl;
  return 0;
}


By the way, it is good practice to keep the code width less than 80 characters, my IDE does it automatically, but you can wrap lines like you see with the last cout, as long as the semicolon (;) is at the end of the statement, not the end of the line, the compiler will ignore the fact that it's on separate lines.

Oh yeah, don't forget to include return 0; at the end of the program. That's another good practice thing, in this case, but often times you will need it, or your program won't function correctly. Not to mention you are declaring a function (main) of type int without having it return an integral value.
Last edited on
Great-- it's working perfectly now. Thanks for your help and for the tips at the end of your post!
Can somebody tell me what these lines of code are saying?

1
2
3
4
5
6
7
  }
  for (counter = num1; counter <= num2; counter++)
    total += counter;
  cout << "The total of the integers between " << num1 << " and " << num2
       << " is " << total << endl;
  return 0;
}


xcrossmyheartx wrote:
Can somebody tell me what these lines of code are saying?

http://www.cplusplus.com/doc/tutorial/control/

Look up "the for loop".
Last edited on
Topic archived. No new replies allowed.