Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mpfeif101

Basic C++ Help

This topic is 5335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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 = 2



int x = 2;
if ( someCondition )
{
x = 4;
}
cout << "x = " << x << "\n";

//the above will print x = 4




-me

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!