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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <cstdarg>
using namespace std;
double add(double a, ...);
int main()
{
int x;
double a, b, c, d, e, f, g, h;
cout<<"\tTo add:";
cout<<"\n\n1. Two numbers."<<endl;
cout<<"2. Three numbers."<<endl;
cout<<"3. Four numbers."<<endl;
cout<<"4. Five numbers."<<endl;
cout<<"5. Six numbers."<<endl;
cout<<"6. Seven numbers."<<endl;
cout<<"7. Eight numbers."<<endl;
cout<<"\n\nSelection(type number): ";
cin>>x;
switch(x) {
case 1:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b;
add(2, a, b);
cin.get();
break;
case 2:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b>>c;
add(3, a, b, c);
cin.get();
break;
case 3:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b>>c>>d;
add(4, a, b, c, d);
cin.get();
break;
case 4:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b>>c>>d>>e;
add(5, a, b, c, d, e);
cin.get();
break;
case 5:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b>>c>>d>>e>>f;
add(6, a, b, c, d, e, f);
cin.get();
break;
case 6:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b>>c>>d>>e>>f>>g;
add(7, a, b, c, d, e, f, g);
cin.get();
break;
case 7:
cout<<"\n\nEnter the numbers: ";
cin>>a>>b>>c>>d>>e>>f>>g>>h;
add(8, a, b, c, d, e, f, g, h);
cin.get();
break;
default:
cout<<"Wrong input. Exiting...";
cin.get();
break;
}
cin.get();
}
double add(double a, ...)
{
va_list args;
double sum;
va_start ( args, a );
for ( int y = 0; y < a; y++ ) {
sum += va_arg ( args, double );
}
va_end ( args );
cout<<"\n\nAnswer: "<<sum;
cin.get();
}
|