tags:

views:

68

answers:

2

how to do? User type some numbers and then another function return result of addition of these numbers? (C/C++)

for example, user type in 3 4 7 and then he see printed on screen 14 another one, user type 5 6 and gets 11

I'm a beginner in programming C, so I ask you to help me, please

+3  A: 

At a high level:

  • take the input string
  • split it by white space
  • parse each part to an integer
  • add the integers
  • return the result

This is as simple as it can get without actually posting code, which I won't do as I suspect this is a homework assignment.

James Gaunt
+1 for giving clear instructions while not giving code.
matias
+2  A: 

because the number of arguments is not specified i think you need va_list but as you said you are new in c++ so you can write it like this

#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    int sum=0;
    int t;
    while ((cin>>t)!=EOF)//in windows EOF is  ctrl+z
    {
        sum+=t;
    }

    cout<<sum<<endl;

    return 0;
}
Don't test for EOF. There are other error conditions. Use `while (cin >> t)` Thus the loop will work while there is valid input and stop when somthing becomes invalid. In your version it will stop working when a non integer is types but the loop will not end.
Martin York