Sloppy Calculator

It works without my loop but when I try to incorporate the loop it wont run.

Anyone know where I'm messing up?

I mainly want to type in 2 numbers and have it spit out the sum. Then I want it to reset and start over so I can do 2 more new numbers.

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

int a, b, sum;

using namespace std;

int main()

{

	for (i = 1; i < 1000; i++)
	{

	cin >> a;
	cin >> b;

	sum = a + b;
	cout << sum;
	cout << "\n\n\n\n"
	}
}
closed account (jh5jz8AR)
Hey Irhcsa,

I looked your code over and I noticed two things.

Your loop control variable (i) is undefined.

for (int i = 1; i < 1000; i++)

The second mistake I noticed, is you are missing a semi-colon after your last cout statement.

cout << "\n\n\n\n";


I hope this helps you out, Irhcsa.

Happy coding
Last edited on
your code is fine, but you use a variable for iteration that hasn't been declared yet.

for (i = 1; i < 1000; i++) this should be for ( int i = 1; i < 1000; i++ )
Haha thanks usdblades and Lorence30.

I derped up.
Alright I have another hick up.

My program runs and I can do addition and subtraction just fine but the multiplication and division don't seem to be wanting to work. I have searched online and am sure that the correct characters are * and / Here's what I have so far.

The program runs, but when entering a division problem for example, the program does some weird beeping and a ton of stuff flashes and it crashes.

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
#include <iostream>
#include <Windows.h>

int a, b, sum, difference, multiply, divide;

using namespace std;

int main()

{
	cout << "Enter your problem with this format: 5+2 or 20-6 then press enter.\n";

	for (int i = 1; i < 1000; i++)
	{
		
		cin >> a >> b;

		difference = a - b;
		sum = a + b;
		multiply = a * b;
		divide = a / b;

		if (a + b)
		{
			cout << sum;
			cout << "\n___________\n";
		}

		else if (a - b)
		{
			cout << difference;
			cout << "\n___________\n";
		}

		else if (a * b)
		{
			cout << multiply;
			cout << "\n___________\n";
		}

		else if (a / b)
		{
			cout << divide;
			cout << "\n___________\n";
		}
	}
}
Last edited on
> Enter your problem with this format: 5+2 or 20-6 then press enter
> cin >> a >> b;
¿where are you reading the operator?
In the if statement. It works for addition and subtraction.
Topic archived. No new replies allowed.