while if problem ..

hi, i am a new beginner,i don know what wrong in my code?actually i want print out a=5, b=5, c=5.but still cant work.

#include <iostream>
using namespace std;

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
int main(){
	int ,row,a=0,b=0,c=0;
	
	cout<<"the row number ";
	cin>>row;
	while(row!=0){
		
		if(row<=10){
			a++;
		}
		 if(row>=11 && row<=25){
			b++;
		}
		 if (row >=26 && row <=30){
			 c++;}
	}
			
	
	
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
	cout<<"c="<<c<<endl;
	
	 

	
	system ("pause");
	return 0;
}
Last edited on
What input value do you enter (or would like to enter) for row?

How is this loop supposed to terminate? It will either not execute at all (row==0) or repeat infinitely.
 
    while(row!=0){
i would like to enter 2 13 27, it will print out a=1, b=1, c=1.
Well, the code as it stands has just one cin statement, which allows just a single value to be entered.

It seems you should add another cin >> row inside the body of the loop. That would allow more than one value to be entered, and also would make it possible (by typing 0) to exit from the loop.
Last edited on
Hello somefight,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Chervil has made two good points.

Using what you have you might try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
	while (row !=0)
	{
		if (row <= 10)
		{
			a++;
			//if (a == 5) break;
			if (a == 5)
			{
				b = 5;
				c = 5;
				break;
			}
		}


You may need to rethink the if statements because depending on the number entered only one if statement can be used for the number entered. I.E., if row = 2 only the first if statement is used. If row = 13 only the second if statement is used.

This may give "a", "b" and "c" = to 5, but I do not think that is what you want.

Hope that helps,

Andy

P.S. what I think should be line 5 int ,row,a=0,b=0,c=0; you have an extra comma at the beginning.
Last edited on
thanks for help!!
Topic archived. No new replies allowed.