problem in returning a value

i think my code is correct but it's output is not. please help me with this..

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
#include <cstdlib>
#include <iostream>

using namespace std;

int add(int x, int y) 
{ 
    int result = x + y;
    return x + y;
} 
    
int main() 
{ 
  int num1, num2; 
  int result; 
  int choice;
  
  cout <<"\nEnter two numbers separated by a space: " << endl; 
  cin >> num1 >> num2;
  cout <<"\nnum1 is " << num1 << ", num2 is " << num2 << endl;
  cout<<endl;
  cout<<"0 - close"<<endl<<"1 - add"<<endl<<"2 - swap"<<endl<<"3 - increment"<<endl<<"4 - decrement"<<endl;
  cout<<"Enter your choice: ";
  cin>>choice;

  switch(choice)
  {
  case 0:       
    system("PAUSE");
    return EXIT_SUCCESS;

  case 1:
   result = add(num1, num2);
   cout << "\nresult is " << result << endl;
   break;
 


  } 
}



Last edited on
Wrong section...

Anyway, what exactly is the output you are getting?
umm in my system the result is just fine.. i did 2 4 and gave choice 1 so addition result is 6...
well i have a quiestion is int result =x+y on line 8 is required??? wat i think is
33:: result = add(num1, num2); & 9:: return (x+y); does the job..

please guide me if i m wrong

regards
cd4
@CD4: Yes, that is a better solution.
when i enter a 2 numbers and choose 1 it only close the program..i dont get the answer..

i think the problem is on my system(i guest) because our teacher said that some systems don't show the right answer but some like your system shows.


thanks ^^

ooppss sorry for the wrong section..i'm just new..sorry
because our teacher said that some systems don't show the right answer but some like your system shows.


I question your teacher's C++ experience. Separate systems will/should not display different calculated results. It sounds like you're just having a problem keeping the console open. You have a call to system("PAUSE"); in case 0 that keeps it open, but nothing in case 1 which will result in the console window closing immediately. Read through this post for the appropriate solution http://www.cplusplus.com/forum/beginner/1988/. Also, you should always put a default case in your switch statement to catch the exceptions.
Topic archived. No new replies allowed.