Loop question

Ive been trying to get this loop to work for over an hour and cant figure out why its behaving so weird. If i use a For or While loop the program does not add/multiply the first two numbers of many being input. If i use a Do While loop only the first number is not being added/multiplied. I know that sometimes working with loops can get you a "1 off" problem. But i really dont get why im getting 2 off in For and while loops
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
void b() {// sum and product of numbers being input
	unsigned k, sum = 0, product = 1;
	cout << "Input unsigned, followed by doubles, non # to quit " << endl;
	cin >> k || die("input failure");
	if (k == 0) {
		cout << "Sum is " << sum << " Product is " << product << endl;
	}
	else if (cin >> k) {
		double k;
		cin >> k;
		do {
			sum+=k; product*=k;
		} while (cin >> k);
/*for (cin >> k; !cin.fail(); cin >> k)
			sum += k;
		product *= k;*/
		/*if (cin >> k) {
			cin >> k || die("input failure");
			double k;
			while (cin >> k) {
				sum = sum + k;
				product = product*k;
			}*/
		cout << "Sum is " << sum << " Product is " << product << endl;				
	}
}


I commented out the loops that were giving me 2off results, thought id include them anyway. Code first needs an unsigned type to jump onto the loop, and afterwards all numbers being input have to be double type and added and multiplied and output. Can someone maybe point me in the right direction? tutorials or any type of learning tool would be appreciated
Last edited on
Hi,

cin >> k || die("input failure"); I never encountered this syntax ever, could you tell us what are you exactly trying to do?

also
else if (cin >> k), this is faulty

PS: Welcome to cplusplus.com
thanks for the welcome...

die("input failure") is a user defined function i use to display error messages. If a user does not input a number the die function would output "Fatal Error: input failure" and exits.

if i wanted to say that unsigned k has to be positive and a user inputs a negative i can simply switch "input failure" to "No negatives allowed" and the resulting output would be "Fatal Error: No negatives allowed" and the program would simply exit.

sorry for the faulty code, im still learning and thats the only way i seem to be able to get it to loop. I suppose if(cin) would work as well, but i need the loop to skip when k is not being input
If a user does not input a number the die function would output "Fatal Error: input failure" and exits.


for that we got the cin.fail() function


if i wanted to say that unsigned k has to be positive and a user inputs a negative i can simply switch "input failure" to "No negatives allowed" and the resulting output would be "Fatal Error: No negatives allowed" and the program would simply exit.


if you take the data type as unsigned and use cin.fail(), you can tackle your negative integer problem

I suppose if(cin) would work as well, but i need the loop to skip when k is not being input
cin works but not as a evaluator for if statements

HOPE IT HELPS :)

Last edited on
Topic archived. No new replies allowed.