begginer question ! (calculator)

closed account (N7LhAqkS)
Hello all im working on my first console program ( calculator )

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
#include <iostream>

using namespace std;

int main()
{
    int iOne;
    int iTow;
    int iThree;
    int iFour;
    iOne = 1;
    iTow = 2;
    iThree = 3;
    iFour = 4;
    int iNumbers;
    
    cout << "Mathematic Calculator" << endl;
    cout << endl;
    cout << "1. ( + )" << endl;
    cout << "2. ( - )" << endl;
    cout << "3. ( X )" << endl;
    cout << "4. ( / )" << endl;
    cout << endl << "Pick One Option :" << endl;
    cin >> iNumbers;
    
    if (iNumbers == iOne) {
                  cout << endl << "Write Tow Numbers :";
                  cin >> iNumbers;
                  cout << " + ";
                  cin >> iNumbers;
                  cout << " = ";
                  cout << iNumbers + iNumbers << endl;
                 } else {
                               cout << endl << "Write Another Number !" << endl;
                               }
                 
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


its not completed but i have questions like how make in the if statemant
that it will be so cin = 50 + 50 = 100
i cannot make it like this becuase every cin have auto endl;
so i get someting like this
i write 50 and in next line it write + i write number 50 and then it show down = 100

i want it in 1 line 50 + 50 = 100

and other question how make other operations like - is - / * is * and / is / ??
i tryed use - but - no work
i tryed 50 - 20 and my = was 0 :D it is becuase i have int ??? it no need be other data type ?


and my last question why it show me press any key to continue and my console exit ? i wanna make new operations with this code i make 1 operation or i make 1 Wrong Number ! and it show me press any key to contine and i press and console close how i can make that i can do more operations .... ?
and how i can put in my code an END tab example press E to end calculator ?

Thank,s for read it :) hope u will help me with my iusses. See ya ...
Question 1: you can't read input without newline, you'll need an external library for that (eg: curses)
Question 2: You are reading the input on the same variable for both the operands:
28
29
30
31
                  cin >> iNumbers;
                  cout << " + ";
                  cin >> iNumbers; // overwites iNumbers
                  cout << " = ";

Last question:
and my last question why it show me press any key to continue and my console exit ?
Because you put system("PAUSE");
i wanna make new operations with this code i make 1 operation or i make 1 Wrong Number ! and it show me press any key to contine and i press and console close how i can make that i can do more operations .... ?
Wrap the code from line 17 to line 36 in a loop
and how i can put in my code an END tab example press E to end calculator ?
add it as an option after the operations
Last edited on
Sorry, but wtf is with your if statement indentation?

You can't do it that way, not with cin>>. You'll have to look in the reference section of this site to figure out how to read a different way.

I don't understand what you are asking about the operations...the problem with your algorithm is that you read the first number, then read the second number overwriting the first, so when you add, subtract, etc. you are always using the same number twice.

Finally:
http://www.cplusplus.com/forum/articles/7312/
closed account (N7LhAqkS)
hmm ok i done the loop it work but i dont understand the overwriting what u talk and its not good ? in if have cin and cout ?

Question 1: you can't read input without newline, you'll need an external library for that (eg: curses) i have saw curses in http://www.cplusplus.com/forum/articles/7312/ but there i dont see any cin and cout so dont know how make it 50 + 50 = 100 in 1 line.

hmm ok i done the loop it work but i dont understand the overwriting what u talk and its not good ?
1
2
3
4
5
cin >> iNumbers;
cout << " + ";
cin >> iNumbers;
cout << " = ";
cout << iNumbers + iNumbers << endl;
User enters 123 => iNumbers = 123

User enters 456 => iNumbers = 456

iNumbers + iNumbers = 456 + 456, not 123 + 456


i have saw curses in http://www.cplusplus.com/forum/articles/7312/ but there i dont see any cin and cout

If you use curses you won't use cin or cout
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/scanw.html
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/printw.html
you can use cin, but you code will have to work around the bounds of cin. or like bazzy suggested use curses.
basically the pseudocode of this solution is as follows, (using cin)
1. ask the user which type of math function they would like to use;
2. store in a variable;
3. get user to input 2 numbers, store in 2 different variables;
4. do the math calculation store the answer in another variable;
5. output the result.

as an example to read in 2 numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int num1;
int num2;

cout << "Enter 2 numbers followed by newline";
cin >> num1 >> num2;

// do you math on num1 +*/- and num2 =
// output result

// or 

cout << "Enter first number followed by newline";
cin >> num1;
cout << "Enter second number followed by newline";
cin >> num2;


cout << num1 << " + " << num2 << " = " << answer << endl;
Last edited on
closed account (N7LhAqkS)
okey thanks mutch a last question how i can make the e = exit console ?

i tryed someting like this char cFinish;
if (cFinish == 'e') {
exit;
}

but it no work and why it must be allways char cFinish or int iFinish what is this i or c ? and why it need be there i tryed without it and its the same i dont understand why programators use it :/

yes you need to include the cstdlib header, and exit needs paramters;

1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
using std::exit;

char cFinish='a';

cin >> cFinish;
if (cFinish == 'e')
{
   cout << "Goodbye";
   exit (0);
}


the exit conditions are same as return conditions that the operating system can use, so zero(0) symbolises program exited successfully, 1-92 (i think 92, around that) symbolises a different error code.
so to use this say your trying to open a file and it failed you could exit with a condition 1, or some other number that symbolises the correct error for that situation.
Last edited on
closed account (N7LhAqkS)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cstdlib>
using std::exit;
using namespace std;
int main()
{
    int iOne;
    int iTow;
    int iThree;
    int iFour;
    iOne = 1;
    iTow = 2;
    iThree = 3;
    iFour = 4;
    int iNumber1;
    int iNumber2;
    char cFinishs='e';

    cout << "Mathematic Calculator" << "\t" << "Press E to Close !" << endl;
    cout << endl;
    cout << "1. ( + )" << endl;
    cout << "2. ( - )" << endl;
    cout << "3. ( X )" << endl;
    cout << "4. ( / )" << endl;
    do {
    cout << endl << "Pick One Operation :" << endl;
    cin >> iNumber1;

    if (iNumber1 == iOne) {
                  cout << endl << "Write Tow Numbers :" << endl;
                  cin >> iNumber1;
                  cin >> iNumber2;
                  cout << iNumber1 <<" + " << iNumber2;
                  cout << " = ";
                  cout << iNumber1 + iNumber2 << endl;
                 } else if (iNumber1 == iTow) {
                  cout << endl << "Write Tow Numbers :" << endl;
                  cin >> iNumber1;
                  cin >> iNumber2;
                  cout << iNumber1 <<" - " << iNumber2;
                  cout << " = ";
                  cout << iNumber1 - iNumber2 << endl;
                 } else if (iNumber1 == iThree) {
                  cout << endl << "Write Tow Numbers :" << endl;
                  cin >> iNumber1;
                  cin >> iNumber2;
                  cout << iNumber1 <<" X " << iNumber2;
                  cout << " = ";
                  cout << iNumber1 * iNumber2 << endl;
                 } else if (iNumber1 == iFour) {
                  cout << endl << "Write Tow Numbers :" << endl;
                  cin >> iNumber1;
                  cin >> iNumber2;
                  cout << iNumber1 <<" / " << iNumber2;
                  cout << " = ";
                  cout << iNumber1 / iNumber2 << endl;
                 } else if (cFinishs =='e') {
                        cin >> cFinishs;
                        exit (0);
                        } else {
                               cout << endl << "Write Another Number !" << endl;
                               }                                          
} while(true);
}


gcampton i used ur code but it work on any other key i wanna that it end only on E button

maybe i have wrong placed this code ? :/ if yes sry im rly new with c++ i watched some tutorials but 1 think is watching tutorial and other think is do it :D
and how i can change some words color ? in console ? u know that = 100 the 100 will have red color maybe. that it will seen nicer and that u can fast see the equals :)

Sorry try using this: it's string compare.

1
2
3
4
5
6
7
8
9
10

char cFinishs='e';
char userInput;

cin >> userInput;

if ( strcmp ( userInput,cFinishs ) != 0)
    exit (0);
// else keep looping.


with this type of programming at this stage you cannot change text color, however later on when you learn gui etc it will be no problem. But with terminals it's hard as there is soooooooo many different types of terms and different operating systems.


EDIT:
} else if (cFinishs =='e') {


don't put this line in as we know it is true.... so it will automatically exit no matter what.
Last edited on
closed account (N7LhAqkS)
it is not like i have need but thanks.....
closed account (y8h7M4Gy)
You have to use the #define operator.
Example:


#define 'e' (Then here write what command you want 'e' to do.)


Put that where your includes are.
closed account (N7LhAqkS)
tryed and it show me error macro names must be identifiers

#define 'e' (exit (0))

closed account (y8h7M4Gy)
Why don't you just let it go to the thing that says "Press any key to continue" then it will just exit by itself.
Here is an example.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main () {
	char cont='y',op;
	int numa,numb;
	cout << "Welcome to my calculator.\nIf you need help, type in h and hit enter, or continue. y/n\n";
	cin >> cont;
	if (cont == 'h') {
		cout << "Instructions of use:\n1: enter a number\n2: enter an operator ( + - / x )\n3: enter a second number\nMade by Edward\nContinue? y/n\n";
		cin >> cont;
	}
	while (cont == 'y') {
		cont = 'n';
		cin >> numa;
		cin >> op;
		cin >> numb;
		cout << "=\n";
		if (op == '+')
			cout << numa+numb << endl;
		else if (op == '-')
			cout << numa-numb << endl;
		else if (op == 'x')
			cout << numa*numb << endl;
		else if (op == '/')
			cout << numa/numb << endl;
		cout << "Continue? y/n \n";
		cin >> cont;
	}
	return 0;
}


Last edited on
Topic archived. No new replies allowed.