why wont this work??

This is a discussion on why wont this work?? within the C++ Programming forums, part of the General Programming Boards category; Ok so its going through the loop- but its not calculating correctly. I am using redirection for this and my ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question 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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And where in the for loop does it read anything from your file?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (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. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ammochck21 View Post
    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. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ammochck21 View Post


    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. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ammochck21 View Post
    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. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by tabstop View Post
    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.)
    Quote Originally Posted by ammochck21 View Post
    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. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    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.....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21