Basic C++ Help

Started by
6 comments, last by mpfeif101 20 years ago
Here is the code I am using for a calcuator I am trying to make. It doesn''t work. Can someone please show me what I am doing wrong? #include <iostream> #include <stdlib.h> using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int subtraction (int a, int b) { int r; r=a-b; return (r); } int multiplication (int a, int b) { int r; r=a*b; return (r); } int division (int a, int b) { int r; r=a/b; return (r); } int main () { int z; cout << "Enter the first number: "; cin >> z; string o; cout << "Enter the operator: "; cin >> o; int y; cout << "Enter the second number: "; cin >> y; if (o == "+") { int x; x = addition (z,y); } elseif (o == "-") { int x; x = subtraction (z,y); } elseif (o == "*") { int x; x = multiplication (z,y); } elseif (o == "/") { int x; x = division (z,y); } cout << "The result is " << x << "\n"; system("pause"); return 0; }
Advertisement
{
int x;
x = division (z,y);
}

x only lives within that block, the variable is erased on the last line there. You need to introduce x in the outer scope. Remove all the "int x;"s from inside the if-else structure and put one "int x;" before that thing, like after "cin >> y;". Could be other flaws there too..
as an example of the above post:

int x = 2;if ( someCondition ){    int x;    x = 4;}cout << "x = " << x << "\n";//the above will print x = 2int x = 2;if ( someCondition ){    x = 4;}cout << "x = " << x << "\n";//the above will print x = 4


-me
Thanks for your help. It works now (i found another error, it was supposed to be else if, not elseif).

Once you do one problem though, it says "Press any key to continue". Instead is there a way to say like "Press y to do another problem, x to exit"?

#include <iostream>
#include <stdlib.h>

using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}

int multiplication (int a, int b)
{
int r;
r=a*b;
return (r);
}

int division (int a, int b)
{
int r;
r=a/b;
return (r);
}

int main ()
{
int z;
cout << "Enter the first number: ";
cin >> z;
string o;
cout << "Enter the operator: ";
cin >> o;
int y;
cout << "Enter the second number: ";
cin >> y;

int x;
if (o == "+")
{
x = addition (z,y);
}
else if (o == "-")
{
x = subtraction (z,y);
}
else if (o == "*")
{
x = multiplication (z,y);
}
else if (o == "/")
{
x = division (z,y);
}


cout << "The result is " << x << "\n";
system("pause");
return 0;
}


To keep repeating you could just go into a simple do-while loop (do-while instead of while, since you want to run the program at least once). Keep repeating the loop until a certain key is pressed.

do {  // get user input  // and print the result  char cont;  cout << "Do you want to continue? y/n";  cin >> cont;  } while (cont != "Y" && cont != "y") 


[edited by - shyam on April 12, 2004 6:50:38 PM]
"The results are interesting, if true, but they are certainly not going to help me find oil."- response of Chevron Oil VP after a scientist working for him discovered 2^216,091 - 1 was a prime number.
Did that and i got an error... said cont wasn''t defined.

heres the code.

#include <iostream>#include <stdlib.h>using namespace std;int addition (int a, int b){  int r;  r=a+b;  return (r);}int subtraction (int a, int b){  int r;  r=a-b;  return (r);}int multiplication (int a, int b){  int r;  r=a*b;  return (r);}int division (int a, int b){  int r;  r=a/b;  return (r);}int main (){do {  int z;  cout << "Enter the first number: ";  cin >> z;  string o;  cout << "Enter the operator: ";  cin >> o;  int y;  cout << "Enter the second number: ";  cin >> y;    int x;  if (o == "+")  {  x = addition (z,y);  }  else if (o == "-")  {  x = subtraction (z,y);  }  else if (o == "*")  {  x = multiplication (z,y);  }  else if (o == "/")  {  x = division (z,y);  }      cout << "The result is " << x << "\n\n\n";  char cont;  cout << "Do you want to continue? y/n";   cin >> cont;  } while (cont != "Y" && cont != "y")  return 0;}
nevermind i got it to work. Thanks for your help people!!! now for adding more operators like square root, lol.

quote:

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}



it would be better to write:

int addition (int a, int b)
{
return (a+b);
}

..

retrun(a*b);

...

AND:

int division(int a, int b)
{
if (b != 0) return (a/b); // for b = 0
return 0;
}

--------------------------All 0==]=====> BrainStorm--------------------------If it''s not lame it isn''tfrom mine--------------------------

This topic is closed to new replies.

Advertisement