help in looping

help pls im newbie every time i type quit it will turn to infinite loop :(
did i miss something ?
question:
Write a program that continuously accepts and computes the sum of a number. If the user enters quit the looping stops

my code:
1
2
3
4
5
6
7
8
9
10
11
int main()
{	string z;
	int x,sum;
	do{
	
		cout<<"enter a number: ";
		cin>>x;
		cout<<"sum: "<<(sum=sum+x)<<endl;}
	while(z!="quit"); 
	return 0;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{ 
string z;
int x, sum = 0;
do{
cout<<"enter a number: ";
cin>>x;
cout<<"sum: "<<(sum=sum+x)<<endl;
cin >> z;
}
while(z!="quit");
return 0;
}



Use code tags and proper indentation please.
Last edited on
You need to read the number as a string. If it doesn't equal "quit" then convert the string to a number and increment the sum.

BTW, you need to initialize the sum to zero.
closed account (48T7M4Gy)
You need to read the number as a string.


Z is a string, and x is an int according to the OP - I think he changed it from the very first version of the post. (maybe)
Topic archived. No new replies allowed.