int in C++ (newbie)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • titounette
    New Member
    • Oct 2006
    • 3

    int in C++ (newbie)

    Help i try to built a function which is a sum of two integers
    The result must be an integer !
    Check the result...
    Any help it 's welcome.
    /*************** *************** *************** ***** ********/
    #include <iostream>

    #include "math.h"
    using namespace std;

    int add_number(int a, int b, int c)
    {
    cout << "enter a" << endl;
    cin >>a;
    cout << "enter b "<< endl;
    cin >> b;
    c = a + b;
    return (a, b, c);
    }

    int main ()
    {
    signed int a, b, c;

    add_number(a, b, c);
    cout << " the sum is :" << c << endl;
    }
  • apusateri
    New Member
    • Oct 2006
    • 27

    #2
    First of all, when you're creating this function, since you're going to ask the user for the inputs, you don't need to include them in your function.

    Also, you don't need to return all three values, only the resulting summation (c in your case)

    i.e.

    Code:
    int addNumber()
    {
       int a, b, c;
    
       cout << "enter a" << endl;
       cin >>a;
       cout << "enter b "<< endl;
       cin >> b;
       c = a + b;
       return c;
    }
    
    int main ()
    {
       int sum;
       sum = addNumber();
    
       cout << " the sum is :" << sum << endl;
    }

    That should work - let me know if you have any questions. Thanks.
    Last edited by apusateri; Oct 10 '06, 08:00 PM. Reason: Typo

    Comment

    • titounette
      New Member
      • Oct 2006
      • 3

      #3
      Many thanks. It' s fine!
      I made some changes for me.


      int addNumber(int a, int b)
      {
      int c;

      cout << "enter a" << endl;
      cin >>a;
      cout << "enter b "<< endl;
      cin >> b;
      c = a + b;
      return c;
      }

      int main ()
      {
      int x, y, sum;
      sum = addNumber(x, y);

      cout << " the sum is :" << sum << endl;

      Comment

      • apusateri
        New Member
        • Oct 2006
        • 27

        #4
        Originally posted by titounette
        Many thanks. It' s fine!
        I made some changes for me.


        int addNumber(int a, int b)
        {
        int c;

        cout << "enter a" << endl;
        cin >>a;
        cout << "enter b "<< endl;
        cin >> b;
        c = a + b;
        return c;
        }

        int main ()
        {
        int x, y, sum;
        sum = addNumber(x, y);

        cout << " the sum is :" << sum << endl;
        Glad that it worked for you.

        FYI, however

        Code:
        nt  x, y, sum;
           sum = addNumber(x, y);
        You don't need to declare x or y, or pass them to the function, as you are obtaining the values for those two numbers within the function itself. Declaring x and y, and then passing them to the function, doesn't actually do anything useful for the program.

        Just a side note, glad I could help!

        Comment

        • dynamicleo
          New Member
          • Oct 2006
          • 14

          #5
          You can used this way:
          Code:
          #include <iostream>
          
          #include "math.h"
          using namespace std;
          
          void add_number(int &a, int &b, int &c)
          {
              cout << "enter a" << endl;
              cin >>a;
              
              cout << "enter b "<< endl;
              cin >> b;
          
              c = a + b;
              // return (a, b, c);
          }
          
          void main ()
          {
              int a, b, c;
          
              add_number(a, b, c);
              
              cout << " the sum is :" << c << endl;
          }

          Comment

          Working...