I'm having difficulty wrapping my head around loop style statements. Resolved with multiple conditions for do while statement.

I'm working on a simple arithmetic program in VS 2013 and have most of it done. I'm just having issues understanding loop statements. I want the final line to be a classic "Start over? Y/N" type thing, and have it start back at the top, allowing the user to enter new variables.

If someone could explain loops to me, and the kind that would be better suited for this one, it'd be appreciated. I'm not really looking for the answer, just guidance. The code is included for the purpose of assistance. Everything works thus far.

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

int main()
{
	cout << "Enter an integer. " << endl;
	int x = 0;
	cin >> x;
	cout << "Enter another integer. " << endl;
	int y = 0;
	cin >> y;
	cout << "You entered " << x << " and " << y << "." << endl;
	cout << "The sum of these two numbers is " << x + y << "." << endl;
	cout << "The difference of these two numbers is " << x - y << "." << endl;
	cout << "The product of these two numbers is " << x*y << "." << endl;
	cout << "The quotient of these two numbers is " << double(x) / double(y) << "." << endl;
	int z = 0;
	cout << "Would you like to begin again? Y/N" << endl;
	return 0;
}


If it looks like homework, it isn't. I'm using LearnCPP.com, and decided to practice by extrapolating the std::cin portion of the page following this sentence into a program.

http://www.learncpp.com/cpp-tutorial/1-3a-a-first-look-at-cout-cin-endl-namespaces-and-using-statements/
Last edited on
https://www.youtube.com/watch?v=JX14ObbbCPI&list=PL2DD6A625AD033D36&ab_channel=CodingMadeEasy

https://www.youtube.com/watch?v=tvC1WCdV1XU&list=PLAE85DE8440AA6B83&ab_channel=thenewboston

Thats two big playlist of 2 people going through all of the basics of c++, all kinds of loops, statements just pretty much everything. There are different types of loops. You probably want a do-while loop. Which looks like this

1
2
3
4
do
{
// do stuff
}while(condition);


Everything inside will keep looping as long as the condition is met. So what you could do is. Ask if the user wants to go again, which you have already done. Store the Y/N in a char variable. And in the condition say, run this loop as long as the users answer is "Y".

This will be more clear after you've checked out more of their videos, they're better at explaining.

Here are some refrences if you want to check them out aswell -

http://www.tutorialspoint.com/cplusplus/cpp_do_while_loop.htm

http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm
ok, I've got the first one buffering right now. While I do that, if I understand you and those two links at the bottom correctly, I nest my code inside of do while, which is nested in main?

So...

1
2
3
4
5
6
7
int main()
{
do
{
//my stuff
}while(char=y)
return 0


Simplified, of course, for the forum.
which is nested in main?

Main is just the main function, no nesting happening.

Your code is not quite right. Since Y is a character, you have to surround it with ' '

So

char answer;

1
2
3
4
do
{
 // your stuff
}while(answer == 'Y'); // double = not just one. 
Huh. Thanks! That'll help a lot, actually.

Almost there. I can now have it loop if I enter a capital Y but not lowercase. I tried doing this:

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
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char ch('Y');
	do
	{
		cout << "Enter an integer. " << endl;
		int x = 0;
		cin >> x;
		cout << "Enter another integer. " << endl;
		int y = 0;
		cin >> y;
		cout << "You entered " << x << " and " << y << "." << endl;
		cout << "The sum of these two numbers is " << x + y << "." << endl;
		cout << "The difference of these two numbers is " << x - y << "." << endl;
		cout << "The product of these two numbers is " << x*y << "." << endl;
		cout << "The quotient of these two numbers is " << double(x) / double(y) << "." << endl;
		int z = 0;
		cout << "Would you like to begin again? Y/N ";
		cin >> ch;
	} while (ch == 'Y' && ch =='y');
	return 0;
}


Going from while(ch =='Y') to the new one kills it. Is there an either/or option?

Doing this gave me the option for either/or:
while (ch == 'Y' || ch == 'y')

Thanks for the help!
Last edited on
1
2
Doing this gave me the option for either/or:
while (ch == 'Y' || ch == 'y')


Isint this what you want? You want the user to either be able to enter Y or y. So why arent you using it? This is correct.

(ch == 'Y' && ch =='y'); This is incorrect because there is not a letter in the world that is both capital Y and lowercase y at the same time.

Switch them out and it'll work fine, I tested it :)
I did do that. I was just posting the solution to my problem in case others had issues with it.
Topic archived. No new replies allowed.