cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : problem with code
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  problem with code

dominoeffekt (11)
I get this result from my code. Whats wrong?
:S
i dont get it

Please insert the first number you would like to use
54
And the second number u would like to use
90
What would u like to do? +(1) *(2) eller -(3) ?: 2
your answer is:-1076632888your answer is:4860your answer is:4860dominoeffekt@ubuntu:~/codes$

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
#include <iostream>
using namespace std;

int add(int firstnumber,int secondnumber)
{
  return(firstnumber+secondnumber);
} 


int multi(int firstnumber,int secondnumber)
{
  return(firstnumber*secondnumber);
} 



int sub(int firstnumber,int secondnumber)
{
  return(firstnumber-secondnumber);
} 


int main()
{
int a,b,c;
 int what;
cout << "Please insert the first number you would like to use" << endl;
cin >> a;

cout << "And the second number u would like to use" << endl;
cin >> b;

 cout << "What would u like to do? +(1) *(2) eller -(3) ?";
cin >> what;

if (what == 1)
  c=add(a,b);
  cout << "your answer is:" << c;

if (what == 2)
c=multi(a,b);
 cout << "your answer is:" << c;

if (what == 3)
 c=sub(a,b);
 cout << "your answer is:" << c;

  return (0);
}
| Last edited on
Mitsakos (343)
You use
1
2
3
if (what == 1)
  c=add(a,b);
  cout << "your answer is:" << c;

Only the c=add(a,b) is part of your if. You should use bracets "{}" if you want to execute more than one comand in the if.
1
2
3
4
if (what == 1){
  c=add(a,b);
  cout << "your answer is:" << c;
}


Do the same to the other ifs and you will be fine

Hope that helps.
| Last edited on
dominoeffekt (11)
thx mate! all worked fine now:D
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us