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.
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.
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.
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'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;
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
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.