sup, i am practicing my skills with C++ and i want to know why it doesnt take the second option. It is not finished yet but i tried before i finish it and it only takes the add option why ?? well here is the code:
//calculator
#include <iostream.h>
#include <stdio.h>
int main()
{
int num1, num2;
int total;
char opt, add, subs, mult, divi;
num1 = 0;
num2 = 0;
add = 1;
subs = 2;
mult = 3;
divi = 4;
total = 0;
opt = 0;
cout << " Hello, select a basic mathematic operation\n\n";
cout << "\n 1.- Add \n\n 2.- Subtraction \n\n 3.- Multiplication \n\n 4.- Division \n\n";
cin >> opt;
if ( opt == 1 )
{
cout << " Enter two numbers to Add " << endl;
cin >> num1;
cin >> num2;
total = num1 + num2;
cout << "The total is " << total << endl;
return main();
}
if ( opt == 2 )
{
cout << " Enter two numbers to Substract " << endl;
cin >> num1;
cin >> num2;
total = num1 - num2;
cout << " The total is " << total << endl;
return main();
}
return 0;
}
Whats wrong ???
Whats wrong ???
When everything Fails, FORMAT C: solves it all....
A few things.
First, opt is of type char, so unless you type in a <soh> or <stx> (both of which are non-printable characters with ASCII codes 1 and 2) those if statements will never be true--because you are comparing the character to an integer. Change " if(opt == 1) " to " if(opt == '1') ", etc. and it will evaluate correctly.
Next, you should "return 0" not "main()".
Finally, the ".h" in the #include is not necessary for iostream, and since stdio.h is a C library it should be extern'ed in or prefixed with a "c". Like this:
or
First, opt is of type char, so unless you type in a <soh> or <stx> (both of which are non-printable characters with ASCII codes 1 and 2) those if statements will never be true--because you are comparing the character to an integer. Change " if(opt == 1) " to " if(opt == '1') ", etc. and it will evaluate correctly.
Next, you should "return 0" not "main()".
Finally, the ".h" in the #include is not necessary for iostream, and since stdio.h is a C library it should be extern'ed in or prefixed with a "c". Like this:
Code: Select all
#include <iostream>
extern "C" {
#include <stdio.h>
};
Code: Select all
#include <iostream>
#include <cstdio>