Result is always TRUE!

Hello.

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;

}


Thank you, in advance.
closed account (z05DSL3A)
Try:
1
2
3
4
void showResult(int res)
{
    cout<<"The result is "<< res << endl;
}
Last edited on
It worked! Thank you very much!
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?
Yes...you could use gotos if you are lazy, or you can make a big while loop covering the whole program that exits unless they type continue...i.e.:

1
2
3
4
5
6
7
8
int main() {
     string response;
     while(response == "continue") {
          //your code
          cout<<endl<<"Type 'continue' to continue, anything else to exit: ";
          cin>>response;
     }
}
Last edited on
Okay. Thank you so much!
Well, I'm still not pretty good at this, and sometimes I'm abit slow, but where am I going to put this code into my program?
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?:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
int addition(int x, int y);
#include <conio.h>
#include <iostream>
#pragma hdrstop

using namespace 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);
     } else if (calcer == '-') {
           result = subtract(x, y);
           showResult(result);
     } else if (calcer == '*') {
           result = multiplication(x, y);
           showResult(result);
     } else if (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!
Last edited on
Hey if you put that code in you would get a heap of errors. What you need to do is put #include<string> at the very beggining to be able to use string

or u can read the post you posted before about finalizeing program
I did that, but still get alot of errors. :S
Topic archived. No new replies allowed.