// bismillah.cpp : main project file.
#include "stdafx.h"
#include <iostream>
int main()
{
usingnamespace 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;
elseif (x==2)
cout << number1+number2;
elseif (x==3)
cout << number1-number2;
elseif (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
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.
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.
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