for loops are being ignored

My for loops are being ignored. I think I need an if else statement somewhere, but entirely sure. Any ideas? The program is supposed to display something like this:

Enter value:5
Enter value:1
Enter value:3
Enter value:1
Enter value:3
Enter value:5
Enter value:5
Enter value:3
Enter value:0
11

333

555
PROGRAM ENDS



#include <iostream>
using namespace std;

int main()
{

int i, value, count1 = 0, count2 = 0, count3 = 0, count4 =0, count5 = 0;

// Program will prompt user for integer.

do
{
cout << "Enter value:";
cin >> value;

if (value == 0)
break;
}
while (value != 0);

// Integers will be stored.

for (i = 0; i < count1; i++)
cout << "1";
cout << endl;

for (i = 0; i < count2; i++)
cout << "2";
cout << endl;

for (i = 0; i < count3; i++)
cout << "3";
cout << endl;

for (i = 0; i < count4; i++)
cout << "4";
cout << endl;

for (i = 0; i < count5; i++)
cout << "5";
cout << endl;

// Program will display numbers in a histogram and then end.

cout << "PROGRAM ENDS" << endl;

return 0;
}
All your count variables are 0 (what's up with these anyway? Use an array.)
your loops will be ignored coz theres no value in your count something something
we haven't learned arrays yet, so we can't use them. We can use loops, switches, and if/else
Well for one, none of your for loops have curly braces, so they won't run. And as for the output, if you input for example three "3's", should it display all three of them or just the count?

EDIT: Also, I don't believe anything is being stored after you do while loop ends. The only variable that will be stored is the last '0' you entered.
Last edited on
I attached curly braces for all the for loops, but I'm still getting the same thing. I agree with nothing being stored except for the last 0. If i type in just a 0, it gives me five blank lines, which is correct. It doesn't do anything with numbers 1-5. I'm just not sure how to fix it. It's probably simple, but I just don't see it.
Well I've found your problem. I could post part of it for you, but only if you feel you can't get figure it out.
Maybe point me in the right direction first. I really do wanna learn this stuff. If that doesn't work, then post what you can. Thanks.
Ok look at this code first, its part of your problem.

1
2
3
4
5
6
7
8
9
10
11
do
{
    cout << "Enter value:";
    cin >> value;

    if (value == 0)
    {
    break;
    }
} while (value != 0);


The only thing this loop is doing is waiting for you to input zero. It has no way of storing the integers 1 - 5. You need to find a way to increment count1, count2, etc.

I'll be on for a while if you need any more help.
I'm getting there. I have them printing out the same amount I entered, but not all 1s are on the same line for example. And now when I enter 0, it no longer prints five empty lines.
My result:

Enter value:3
Enter value:2
Enter value:4
Enter value:3
Enter value:2
Enter value:1
Enter value:0
1
2
2
3
3
4




19 do
20 {
21 cout << "Enter value:";
22 cin >> value;
23
24 if (value == 0)
25 break;
26 else if (value == 1) count1 ++;
27 else if (value == 2) count2 ++;
28 else if (value == 3) count3 ++;
29 else if (value == 4) count4 ++;
30 else if (value == 5) count5 ++;
31 }
32 while (value !=0);
33
34 // Integers will be stored.
35
36 for (i = 0; i < count1; i++)
37 {
38 cout << "1";
39 cout << endl;
40 }
41 for (i = 0; i < count2; i++)
42 {
43 cout << "2";
44 cout << endl;
45 }
46 for (i = 0; i < count3; i++)
47 {
48 cout << "3";
49 cout << endl;
50 }
51 for (i = 0; i < count4; i++)
52 {
53 cout << "4";
54 cout << endl;
55 }
56 for (i = 0; i < count5; i++)
57 {
58 cout << "5";
59 cout << endl;
60 }
61
62 // Program will display numbers in a histogram and then end.
63
64 cout << "PROGRAM ENDS" << endl;
65
66 return 0;
Take each "endl" out of the for loop and put it after the last curly brace like so

1
2
3
4
5
for (i = 0; i < count1; i++)
    {
    cout << "1";
    }
cout << endl << endl;     // the extra endl will make the space between the output 
Thanks for all your help. I think I got it.
closed account (D80DSL3A)
Can I point out one thing here?
You only need curly braces in a for loop if you need to include more than one statement. Ironically, the form he originally had would work great here:
1
2
3
for (i = 0; i < count1; i++)
    cout << "1";// this will be executed in the for loop
cout << endl << endl;// this will execute after the loop 

Just sayin'. Please continue.
Irregardless, either way will work.
Yeah I got rid of the curly braces. Not needed. I also got rid of one of the endl, because we aren't supposed to any spaces between each line.
Example:

Enter value:3
Enter value:2
Enter value:4
Enter value:3
Enter value:2
Enter value:1
Enter value:0
1
22
33
4

PROGRAM ENDS

It does exactly that now. Thanks everyone for your help.
Topic archived. No new replies allowed.