how to end the loop by input a string to double

This is my homework. I am asked to write a program that let user input several numbers and then calculate the average of them. I finished most of it, but i don't know how to end the loop by let user enter "done". Can anyone tell me how solve it?
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
 #include <iostream>
#include <string>
#include <stdio.h>

using namespace std;


int main()
{
	double nums[3];
	int total;
	double sum=0;
	double average;
	int i=0;
	while(nums[i]!='done'){
	for(total=0,i=0;total<=2;total++,i++){
			cout <<"Please enter a number: ";
			cin>>nums[i];
			cout<<"You entered "<<nums[i]<<" and you entered "<<total+1<<" numbers "<<endl;
	sum= sum+nums[i];
    average=sum/(total+1);
	}
	 cout<<"Your average is "<<average<<endl;
	 sum=0;
	}
		string exit;
		cin>>exit;



	return 0;
}
Last edited on
You're making things way more complicated than it needs to be.
The only variables you need are:
1
2
3
int num = 0;        //number user enters
int sum = 0;        //accumulative sum of numbers
int count = 0;      //count of numbers entered 


Pseudo-code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//declare variables, as show above

infinite loop
        ask user to enter a number or enter -1 to stop
        store input in num

        if num is -1
                break out of loop        //we don't want to increment count and add to sum when user inputs -1 to stop

        add num to sum

        increment count

print out average       //average = sum/count; make sure to cast as float/double, since averages can have decimals 
Last edited on
Topic archived. No new replies allowed.