Very useful addition tool.

I managed to make an addition tool with the aid of variable arguments.

Download link: http://www.mediafire.com/?mn45w8u8pe2fql9

Code:
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();
}
If you simply use a vector, you just need to handle one case instead of 7 (or 50, or 1000).
I dont know vectors yet.
How about this?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
	int n;
	double a, sum=0;
	std::cout<<"Enter number of arguments:"<<std::endl;
	std::cin>>n;
	for (int i=0; i<n; i++) std::cin>>a, sum+=a;
	std::cout<<sum;
        }
I dont know vectors yet.

In what kind of place/book do they cover var args before vectors? O_O
you can get args in C++? so much I do not know.
What do you mean by that o_O
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double add(double a, ...) //infinitium+1
{
	va_list args; //init
	double sum;
	
	va_start ( args, a ); //init2?
	for ( int y = 0; y < a; y++ ) {//loopydydoopdydoop
	 sum += va_arg ( args, double );
	}
	va_end ( args ); //daend
	cout<<"\n\nAnswer: "<<sum;
	
	cin.get();
}


That's my l33t explanation of what I meant by that
var agrgs are useless in this case, as it can be done in a simple loop!
Topic archived. No new replies allowed.