I'm abit of a newbie at C++ programming.
I'm trying to create a simple program, that can just multiply x and y, that you enter.
But when I run the program, everything is fine. Then, when I've entered the two numbers, the only result that shows up is 1, which must mean that it doesn't actually retrieve the result, but just says TRUE.
If anyone would be able to help me with this problem, I'd appreciate it.
Code:
#include <conio.h>
#include <iostream>
#pragma hdrstop
using namespace std;
int multiply (int, int);
void showResult(int);
int main(int argc, char **argv);
int main (int, char**)
{
int x, y, result;
cout<<"Enter the first value:";
cin>> x;
cout<<"Enter the second value: ";
cin>> y;
result = multiply(x, y);
showResult(result);
cout<< "Press any key to continue...";
getch();
return 0;
}
int multiply(int x, int y)
{
return x * y;
}
void showResult(int res)
{
cout<<"The result is "<<showResult << endl;
Just a little question...
Is it possible to make a "Back to start" thing, where you have to press enter? (So that you can start over, without having to close and then open it again)If yes, how can I do this?
You put the thing where you continue around your code. Replace //your code in the example provided by firedraco with your actual code, and then your all set!
Thank you.
But when I put in the code I get heaps of errors, for example a "X undeclared" and atleast 20 others.
I'm not sure that I am inserting the loop correct.
Could you by any chance, if you have time, put it into this code (I updated my code alot), so I could see where it's to be inserted?
It seems like it cannot take a { after the main().
It should seem really simple to put it into the code, but I'm not sure whether it is over the includes, after using namespace std; and so on.
But if you can, then would you please insert it into this code?:
int addition(int x, int y);
#include <conio.h>
#include <iostream>
#pragma hdrstop
usingnamespace std;
int addition (int, int);
void showResult(int);
int addition(int x, int y) {
return x + y; //change to + - * or /.
}
int subtract(int x, int y) {
return x - y; //change to + - * or /.
}
int multiplication(int x, int y) {
return x * y; //change to + - * or /.
}
int dividation(int x, int y) {
return x / y; //change to + - * or /.
}
void showResult(int res) {
cout<<"The result is "<< res << endl;
}
int main(int argc, char **argv, int res) {
int x, y, result;
char calcer;
cout << endl <<"Enter the first value:";
cin >> x;
cout << endl <<"Enter your calctype\n + for Addition, - for Substraction\n* for Multiplication and / for Division:";
cin >> calcer;
cout <<"Enter the second value: ";
cin >> y;
if (calcer == '+') {
result = addition(x, y);
showResult(result);
} elseif (calcer == '-') {
result = subtract(x, y);
showResult(result);
} elseif (calcer == '*') {
result = multiplication(x, y);
showResult(result);
} elseif (calcer == '/') {
result = dividation(x, y);
showResult(result);
} else {
cout<<"Incorrect calculation type selected. Please restart the program.\n";
}
cout << endl << endl << "Press any key to continue...";
getch();
return 0;
}
I know it might be demanding abit much, but it'll help me very much!