# Thread: why wont this work??

1. ## why wont this work??

Ok so its going through the loop- but its not calculating correctly. I am using redirection for this and my input file looks like this:

123
P 3 M M S <--its not adding all of them together- only the first one....
T 2.5
Q
456
S 5 12 3 10 7 16
R 4
P 1 L
Q

I havent written the other codes yet, just want to find out why my calculations for P arent working. This will read in the job number, then read in a code. If its P, I want it to read in the number of sizes there are going to be. Thats where my for loop comes in. This is whats NOT working. SO then it reads the M, and should add 100, then reads in another M and should add 100 to the 100 already in total1....and on and on. Thanks

Code:
```{
cin >> code;
while(code!='Q')
{
if (code == 'P')
{
cin >> num;
cin >> size;
for (int i=1; i <= num; i++);
{
if (size == 'S')
{
total1 = total1 + S;
sum = sum + S;
}
else if (size == 'M')
{
total1 = total2 + M;
sum = sum + M;
}
else if (size == 'L')
{
total1 = total3 + L;
sum = sum + L;
}
}
}
cin >> code;
}

cout << setw(7) << jobnum << "\$" << right << setw(7) << total1 << "   \$" << setw(7) << total2 << "   \$" << setw(7) << total3 << "   \$" << setw(7) << total4 << "   \$" << setw(7)\
<< total1 << endl;

cin >> jobnum;
}
cout <<  "TOTAL" << setw (54) << sum <<  endl;
}```

2. And where in the for loop does it read anything from your file?

3. well it doesnt right now- which I forgot to put back in...but I tried to put cin >> size at the end of the for loop so it will read in thenext size and go through again...but that gives me an infite loop.....

4. Why don't you try one cin >> size at the start of the for loop (and not before the loop)?

The additional one in the end of the loop would otherwise read what should go into code after the for loop.

5. (1) Doing it the way you're doing it will read in one too many letters (one at the top, and three in the loop) -- read at the top of the loop.
(2) No, it's not an infinite loop.
(3) Spaces are characters too, so check that your size isn't a space. (Does >> discard them? I don't think so.)

6. ok well ive been trying things and its still not working....this is what its outputting:
Tree Stump Total
Job# Planting Removal Trimming Removal Billed
123 \$ 100 \$ 0 \$ 0 \$ 0 \$ 100
456 \$ 500 \$ 0 \$ 0 \$ 0 \$ 500
TOTAL 600

here is the updated code (I olnly added only thing):

Code:
```{
cin >> code;
while(code!='Q')
{
if (code == 'P')
{
cin >> num;
cin >> size;
for (int i=1; i <= num; i++);
{
if (size == 'S')
{
total1 = total1 + S;
sum = sum + S;
}
else if (size == 'M')
{
total1 = total2 + M;
sum = sum + M;
}
else if (size == 'L')
{
total1 = total3 + L;
sum = sum + L;
}
}
}
cin >> code;
}

cout << setw(7) << jobnum << "\$" << right << setw(7) << total1 << "   \$" << setw(7) << total2 << "   \$" << setw(7) << total3 << "   \$" << setw(7) << total4 << "   \$" << setw(7)\
<< total1 << endl;

cin >> jobnum;
}
cout <<  "TOTAL" << setw (54) << sum <<  endl;
}```

7. sorry i put the same one in....i took out the cin >> size before the loop and inserted it in before if(size == 'S')
......

8. Originally Posted by ammochck21
ok well ive been trying things and its still not working....this is what its outputting:
Tree Stump Total
Job# Planting Removal Trimming Removal Billed
123 \$ 100 \$ 0 \$ 0 \$ 0 \$ 100
456 \$ 500 \$ 0 \$ 0 \$ 0 \$ 500
TOTAL 600

here is the updated code (I olnly added only thing):

Code:
```{
cin >> code;
while(code!='Q')
{
if (code == 'P')
{
cin >> num;
cin >> size;
for (int i=1; i <= num; i++);
{
if (size == 'S')
{
total1 = total1 + S;
sum = sum + S;
}
else if (size == 'M')
{
total1 = total2 + M;
sum = sum + M;
}
else if (size == 'L')
{
total1 = total3 + L;
sum = sum + L;
}
}
}
cin >> code;
}

cout << setw(7) << jobnum << "\$" << right << setw(7) << total1 << "   \$" << setw(7) << total2 << "   \$" << setw(7) << total3 << "   \$" << setw(7) << total4 << "   \$" << setw(7)\
<< total1 << endl;

cin >> jobnum;
}
cout <<  "TOTAL" << setw (54) << sum <<  endl;
}```
Did you want to set total1 in each of them, or maybe sometimes total2 and total3?

9. yeah I am going crazy....sorry here is the code I have...lol...sorry everyone:

Code:
```{
cin >> code;
while(code!='Q')
{
if (code == 'P')
{
cin >> num;
for (int i=1; i <= num; i++);
{
cin >> size;
if (size == 'S')
{
total1 = total1 + S;
sum = sum + S;
}
else if (size == 'M')
{
total1 = total1 + M;
sum = sum + M;
}
else if (size == 'L')
{
total1 = total1 + L;
sum = sum + L;
}
}
}
cin >> code;
}

cout << setw(7) << jobnum << "\$" << right << setw(7) << total1 << "   \$" << setw(7) << total2 << "   \$" << setw(7) << total3 << "   \$" << setw(7) << total4 << "   \$" << setw(7)\
<< total1 << endl;

cin >> jobnum;
}
cout <<  "TOTAL" << setw (54) << sum <<  endl;
}```

this is my output:

Job# Planting Removal Trimming Removal Billed
123 \$ 100 \$ 0 \$ 0 \$ 0 \$ 100
456 \$ 500 \$ 0 \$ 0 \$ 0 \$ 500
TOTAL 0

10. Originally Posted by ammochck21

this is my output:

Job# Planting Removal Trimming Removal Billed
123 \$ 100 \$ 0 \$ 0 \$ 0 \$ 100
456 \$ 500 \$ 0 \$ 0 \$ 0 \$ 500
TOTAL 0
Are you that isn't still a 600 at the end?

And is this what you wanted to get, or not?

11. yes this is the output I am getting right now...

Job# Planting Removal Trimming Removal Billed
123 \$ 100 \$ 0 \$ 0 \$ 0 \$ 100
456 \$ 500 \$ 0 \$ 0 \$ 0 \$ 500
TOTAL

the "100"'s should be \$235 after it goes through the loop 3 times....the total of all of the sizes is 235

12. Originally Posted by ammochck21
yes this is the output I am getting right now...

Job# Planting Removal Trimming Removal Billed
123 \$ 100 \$ 0 \$ 0 \$ 0 \$ 100
456 \$ 500 \$ 0 \$ 0 \$ 0 \$ 500
TOTAL

the "100"'s should be \$235 after it goes through the loop 3 times....the total of all of the sizes is 235
is M = 100? Are you sure you aren't reading ' ', 'M', and ' ' for your sizes? (Print them out right after you read them in and see.)

13. well its reading the first M....but then thats it....its not going back through the loop to get the other sizes...yes S = 35, M=100 and L = 500. so since the first data set is

P 3 M M S it should go through first and add 100, then 100 then 35....but its not looping through...thats my problem

14. Originally Posted by tabstop
is M = 100? Are you sure you aren't reading ' ', 'M', and ' ' for your sizes? (Print them out right after you read them in and see.)
Originally Posted by ammochck21
well its reading the first M....but then thats it....its not going back through the loop to get the other sizes...yes S = 35, M=100 and L = 500. so since the first data set is

P 3 M M S it should go through first and add 100, then 100 then 35....but its not looping through...thats my problem
It's looping -- you can't make a for loop like that not loop. Try reading my answer again.

15. when i read in the size and printed out to see what its reading in...it only prints an M.....the spaces get skipped with what we are doing....so its not really going through the for loop until i = 3.....