Code runs without errors, but doesnt make sense (basic calculator)

Can someone please point out the errors
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
// bismillah.cpp : main project file.

#include "stdafx.h"
#include <iostream>

int main()
{
    using namespace std;
	cout << "Welcome to calculator/n";
	start:
	cout << "What would you like to do (Please enter 1 for multiply , 2 for addition, 3 for subtraction, 4 for division)...";
	int x;
	cin >> x;
	cout << "Enter the numbers";

	int number1;
	int number2;
	cin >> number1;
	cout << endl;
	cin >> number2;

	if (1>x>4)
	{cout << "enter a number between 1 and 4" << endl; goto start;}
	if (x==1)
		cout << number1*number2;
	else if (x==2)
		cout << number1+number2;
	else if (x==3)
		cout << number1-number2;
	else if (x==4)
		cout << number1/number2;
	cin >> x;
	return 0;
}
What exactly are you not understanding? cout(c out) outputs whatever is in quotes. cin( c in) takes input and stores it in the specified variable. it first prompts the user to enter a number (1,2,3,4)( which is stored in int x) and than performs the according operation according to what the user entered for number 1 and number 2. entering 1 multiplies number 1 by number 2. entering 2 adds them, entering 3 subtracts them. if the number(stored in int x) is less than one or greater than 4 it outputs "enter a number between 1 and 4" because there arent any conditions for those numbers
I made this up myself, so I know what it's supposed to do and I'm glad it's good enough to understand, but can you please try running it? The console windows is not acting like it's supposed to
1. Why do you need lines 3 and 32?

2. Who did tell you something about labels and goto? Forget goto as soon as possible. It was a very old control structure sometimes used in very old software. You should NEVER EVER use it because you'll definitely lose control over your program flow.

3.
The console windows is not acting like it's supposed to
It would help us to know what you think the console window should do.
Once it s filled the screen flickering with one of the cout sentences
3 is there because it's Visual C++, 32 is there so I can look at my program before it closes (otherwise the answer's displayed for a few milliseconds)
I'd appreciate if you'd tell me more about this "goto" and any newer alternatives, and also about how to make the program stay infinitely(if that's not possible then atleast make it ask for something like quit or exit used in cmd or terminal)
Line 22 needs to be fixed. Also, there is a long multi-page thread discussing how to stop the console from closing down that is sticky-ed on this forum.
I'd appreciate if you'd tell me more about this "goto" and any newer alternatives

Have a look at http://www.cplusplus.com/doc/tutorial/control/. Those control structures discussed there are more or less common to most programming languages (since at least 35 years).
fg109, that's what i came up with using high school math, what would be the appropriate c++ syntax? I assume you understand my intention
The problem is, C/C++ is not type safe nor does it really assist on expressing structured programs.

Say x is 2, then
1 > x > 4 ==> (1 > x) > 4 ==> true > 4
Then depending on the internal representation of true you could get due to this horrible implicit type conversion
255 > 4 or 1 > 4 or MAX_INT > 4 or whatever your compiler thinks to represent the value true.

So you may rewrite it as f.e. (1 > x) || (x > 4).
What tcs said, except you probably wanted 1 < x < 4 which means you need to change it to (1 < x ) && (x < 4).
@fg109: Your proposal checks a valid user input. But salh needs the oppsite.
You're right, my mistake.
tcs thanks, i guess i have a lot of catching up to do
@fg109 thanls for that even though it wrong, i learned something new which i might need in the future
Topic archived. No new replies allowed.