Hi everyone. I'm new here and I'm also new to programming. I figured that making a calculator might be a good start. I know how about some commands but not all the ones I will need. I use Borland C++ 5.0.2
Hi everyone. I'm new here and I'm also new to programming. I figured that making a calculator might be a good start. I know how about some commands but not all the ones I will need. I use Borland C++ 5.0.2
Im new 2.... plus im only 12 so its hard to uunderstand all of this stuff..... i have made a calulator before here is my code.
Code:#include <iostream.h> int main() { int a, b; cout<<"Entering text (if any)\n"; cout<<"enter a number\n"; cin>> a; cout<<"enter a number to add to it\n"; cin>> b; cout<<"the sum of the number is " << a+b << endl; return a+b; }
You might want to hold off on a calculator program untill your at least intermiddient(sp?), but it's just MHO.
Helbovine, dont return a+b from main. There is no need to, the return statement is used to pass a value back to the OS, sometimes it'll look like
but all this represents is an int valueCode:return EXIT_SUCCESS;
thx im new and i have to get a better book but thx for advise
1 question.. lets say i said this
thnk uCode:#include <iostream.h> int add() { int a, b; cout<<"text"; cin>> a; cout<<"text"; cin>> b; cout<<"The sum is " << a+b << endl; //now here in the add function would i put return a+b; //or return EXIT_SUCCESS } int main() { add /*and here would it be return EXIT_SUCCESS or*/ return 0; }
Code:#include <iostream.h> void add() { int a, b; cout<<"enter first number"; cin>> a; cout<<"enter second number"; cin>> b; cout<<"The sum is " << a+b << endl; } int main() { add(); return 0; }
the return a+b isnt needed.
You could use it if you want to do something like this:
int c = add();
c will then be equal to the return value of add()
in case your function doesn't need to return anything, do like elad did it and use void.
That means that the function doesn't return anything
illegal else without matching if
Code:#include <iostream.h> int add() { int a, b; cout<<"First Number\n"; cin>> a; cout<<"Second Number\n"; cin>> b; cout<<"The sum is " << a+b << endl; return a+b; } int subtract() { int c, d; cout<<"First Number\n"; cin>> c; cout<<"Second Number\n"; cin>> d; cout<<"The Difference Is " << c-d << endl; return c-d; } int main() { char sign; cout<<"Enter Mathamatical Symbol ( + - * / )\n"; cin>> sign; if(sign == '+'); { add(); return 0; } else if(sign == '-'); { subtract(); return 0; } return 0; }
it should be:if(sign == '+');
if(sign == 'x') { //no ; after the if statement
//blablabla
} else if(...) {//no ; here either
//blabla
}
just use a simple switch statment
if you use a 'switch' then it cuts down on the number of functions.Code:#include <iostream.h> #include <conio.h> void main() { double A, B, ans; char sign; clrscr(); cout << "Enter first number: "; cin >> A; cout << endl; cout << "Enter second number: "; cin >> B; cout << endl; cout << "enter operation (+,-,*,/): "; cin >> sign; cout << endl; switch (sign) { case '+': ans = A + B; break; case '-': ans = A - B; break; case '*': ans = A * B; break; case '/': ans = A / B; break; default: cout << "Invalid operation"; } cout << "The answer is: " << ans; getch(); }
Last edited by xlnk; 03-30-2002 at 01:36 PM.
the best things in life are simple.
well i finished it before i even saw ur switch statment there here it is
but lets say if i wanted i wanted to make it say........Code:#include <iostream.h> int add() { long int a, b; cout<<"First Number\n"; cin>> a; cout<<"Second Number\n"; cin>> b; cout<<"The sum is " << a+b << endl; return a+b; } int subtract() { long int c, d; cout<<"First Number\n"; cin>> c; cout<<"Second Number\n"; cin>> d; cout<<"The Difference Is " << c-d << endl; return c-d; } int multiply() { long int e, f; cout<<"First Number\n"; cin>> e; cout<<"Second Number\n"; cin>> f; cout<<"The Product Is " << e*f << endl; return e*f; } int divide() { long int g, h; cout<<"First Number\n"; cin>> g; cout<<"Second Number\n"; cin>> h; cout<<"The Answer Is " << g/h << endl; return g/h; } int main() { char sign; cout<<"Enter Mathamatical Symbol ( + - * / )\n"; cin>> sign; if(sign == '+') { add(); return 0; } else if(sign == '-') { subtract(); return 0; } else if(sign == '*') { multiply(); return 0; } else if(sign == '/') { divide(); return 0; } else if(sign == 'a' || 'b' || 'c' || 'd' || 'e' || 'f' || 'g' || 'h' || 'i' || 'j' || 'k' || 'l' || 'm' || 'n' || 'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' || 'v' || 'w' || 'x' || 'y' || 'z') { cout<<"Not A Legall Statement Try Again\n"; } return 0; }
add, subtract, multiply, or divide again? y,n
how would i get it to loop back up and start main all over again
int main()
{
char sign;
char redo;
int loopQuit = 0;
while(loopQuit == 0) {
cout<<"Enter Mathamatical Symbol ( + - * / )\n";
cin>> sign;
if(sign == '+')
{
add();
return 0;
}
else if(sign == '-')
{
subtract();
return 0;
}
else if(sign == '*')
{
multiply();
return 0;
}
else if(sign == '/')
{
divide();
return 0;
}
else
{
cout<<"Not A Legal Statement Try Again\n";
}
cout << "Redo this?(y or n) ";
cin >> redo;
if(strcmp(redo,"n")==0 || strcmp(redo,"N")==0) {
loopQuit = 1;
}
}
return 0;
}
also, notice the else instead of the else if(blalblablabla)
Code:#include <iostream.h> #include <conio.h> #include <bool.h> void calc() { double A, B; double ans; char sign; clrscr(); cout << "Enter first number: "; cin >> A; cout << endl; cout << "Enter second number: "; cin >> B; cout << endl; cout << "enter operation (+,-,*,/): "; cin >> sign; cout << endl; switch (sign) { case '+': ans = A + B; break; case '-': ans = A - B; break; case '*': ans = A * B; break; case '/': ans = A / B; break; default: cout << "Invalid operation"; } cout << "The answer is: " << ans; getch(); } bool NotFinished() { char Choice; bool Temp; cout << endl; cout << "Would you like to repeat [Y/N]: "; cin >> Choice; switch(Choice) { case 'Y': Temp = true; break; case 'N': Temp = false; break; default : cout << "You must enter Y or N" << endl; } return Temp; } void main() { do { calc(); }while(NotFinished()); }
Last edited by xlnk; 03-30-2002 at 02:51 PM.
the best things in life are simple.