counting input values

This is a discussion on counting input values within the C++ Programming forums, part of the General Programming Boards category; I am trying to code(c++) a count and sum off of a menu eg. if I ordered 2-B's, 1-A and ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    counting input values

    I am trying to code(c++) a count and sum off of a menu eg. if I ordered 2-B's, 1-A and 3-C's the amount would be 27. My code below only shows 21.25 which seems that it isn't adding the first input. Any suggestions on how I wrote my code?

    Thanks for the help

    Bill
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main( )
    {
      //declare variables
      char comboType = ' ';
      double comboPrice = 0.0;
      double orderTotal = 0.0;
       
       //input data  
       
        cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
        cin >> comboType;
        comboType = toupper(comboType);     
            while (comboType != 'T')
        {  
              cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
              cin >> comboType;
              orderTotal += comboPrice; 
            switch (comboType)
        {
              case 'A':
                   comboPrice = 4.25;
                   break;
              case 'B':
                   comboPrice = 5.75;
                   break;
              case 'C':
                   comboPrice = 5.25;
                   break;
              case 'D':
                   comboPrice = 3.75;
                   break;
        //end switch           
        }   
        // end while 
        }             
        //display output 
        if (comboType = 'T')
    {  
        cout << "Please pay this amount: " << orderTotal << endl;
    }            
        else
                 cout << "No items ordered!" << endl;
            
    system("pause");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Hello,

    I'm not sure if i'm misunderstanding you, but 31.5 should be correct. You could just round up and change the result from double to int to 32 I think.

    B = 5.75
    A = 4.25
    C = 5.25

    The code adds one after the other in the order they are added.. ( I also moved the case definition up the code a bit)

    Your code currently goes :

    ordertotal(5.75) = ordertotal(0) + comboPrice(5.75)
    ordertotal(11.50) = ordertotal(5.75) + comboPrice(5.75)
    ordertotal(15.75) = ordertotal(11.50) + comboPrice(4.25)
    ordertotal(21.00) = ordertotal(15.75) + comboPrice(5.75)
    ordertotal(26.75) = ordertotal(21.00) + comboPrice(5.75)
    ordertotal(31.50) = ordertotal(26.75) + comboPrice(5.75)

    All done and over with ordertotal == 31.50 !! 31 !! 32 depending on your rounding.


    I believe your total is correct, but any more experienced please feel free to correct me.


    Edit: I'm not sure how you're getting 21.25 as you are using +=comboPrice, i'm sure you know this simple means (ordertotal = ordertotal + comboprice), try defining the switch before you start reading into it, makes things a bit more clear at least the very least
    Last edited by Spazmotic; 03-06-2010 at 09:28 AM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Thanks so much Spazmotic,

    I agree with you, but I screwed up... it's 3 D's and not 3 C's.....but if you add the 2 B's total would be 11.50 and then A would be 4.25 and then 3 D's would be 11.25for a grand total of 27 and my program gets 21.25



    so are you saying to put the switch before the while?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    I don't have the experience to tell you "WHY" but i know "HOW"..

    Follow the directions in my previous post regarding clarity and you will get 27 exactly.

    Good luck mate

    EDIT: (Because of possible homework solutions..i'm trying to make this as vague as possible while helping you..to the experts and moderators..If i went too far..tell me to delete or delete yourself please )

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Actually..I do know why now.


    Look at your code carefully and consider the EXACT order of things that are happening.... remember C++ reads from the top down..open up kwrite or kate or notepad or whatever you have and write in plaintext exactly what you see happening in that loop, and you will understand why you are short.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    I know you moved the case up a bit, just trying to understand location from your

    "try defining the switch before you start reading into it, makes things a bit more clear at least the very least"

    I am sure you now know I am a newbie..

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Thanks so much,,, I figured it out with your help.....

    I just moved
    cout<< "Enter item ordered [A/B/C/D] or T to calculate total: ";
    cin >> comboType;
    orderTotal += comboPrice;
    under the //end switch bracket....

    many thanks for walking me through this!

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Lol, you're making this very difficult on me, because i'm new the forum and know they don't like giving away the direct answers because this forum does not condone cheating on homework and I agree.. so I am going to re-phrase this the best I can.


    Remember C++ reads top down.. as you either did not quite understand how to or did not open up a notepad and type out exactly what you saw....I am going to emulate this...I will not provide any further code instruction because from here on out I believe i WILL give you the answer if i do.. but I will show you the novice diagnosis technique I have been using when i get either get compile errors or segmentation faults 20 times in a row (I'm not even as far as you, I don't use classes yet, this is just math and logic, so you will get it )


    The first segment of [code] here will be your while loop, the second will be my "notepad" illustration


    Code:
    cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
        cin >> comboType;
        comboType = toupper(comboType);     
            while (comboType != 'T')
        {  
              cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
              cin >> comboType;
              orderTotal += comboPrice; 
            switch (comboType)
        {
              case 'A':
                   comboPrice = 4.25;
                   break;
              case 'B':
                   comboPrice = 5.75;
                   break;
              case 'C':
                   comboPrice = 5.25;
                   break;
              case 'D':
                   comboPrice = 3.75;
                   break;
        //end switch           
        }   
        // end while 
        }

    Start -> Run -> Notepad

    Code:
    "Enter item ordered[A/B/C/D] or T to calculate total:"
    Read what was typed in to comboType 
    
            So long as comboType does not equal the letter 'T' do the following...
                    "Enter item ordered[A/B/C/D] or T to caculate total:"
                    Read what was typted in and store it in variable comboType
    
                    ordertotal(0.0) = orderder(0.0) + comboprice(0.0)
                            (or)
                    add comboprice(0.0) to ordertotal(0.0)
    
                    NOW what does A B C D mean?  
    
                    A4.25
                    B5.75
                    C5.25
                    D3.75
    
            did they enter T as the input?
    
            Yeah? Ok, Tell me how much they owe ( Ordertotal)

    The main thing I want you to look at here, is your first calculation, the first time that ordertotal is calculated, think of what it would be missing by you having this calculating this early..and think of the EXACT amount your current program result is short of what it should be.



    .... Gods of this forum..please don't ban me...I know that was too much

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Hey again,

    Good job man, It's all about order , i'm learning that alot, C++ can be both forgiving and a cruel mistress at times, she'll call out your mistakes, but if it's the order or math you got wrong, then it's all downhill

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Spazmotic View Post
    Lol, you're making this very difficult on me, because i'm new the forum and know they don't like giving away the direct answers because this forum does not condone cheating on homework and I agree..
    At the same time, it is a programming resource and sometimes the most helpful thing is example or demonstration code. Not everyone here is doing homework, and the needs and priorities of that community should be balanced with the need to avoid becoming a place where people come to get their homework done for free.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    as I always say,,, " You don't learn a thing if someone gives you the answer, However, a nudge in the right direction can only lead to learning the answer"

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    I agree MK... But I don't wish to be banned before day two..it's just not stylish

    Do you just try to determine whether they're doing it for a class and make a judgement call or actually ask?


    Since from his reply and the explaining quickly the way he did., i believe he did figure this out and I will demonstrate here the proper way and why it was shooting the wrong answer, for google searchs for the resource .

    Before this begins, I would like to make a quick note to harley...i'm going to be changing something in my version..check out what it is..and see if you notice the redundancy removal.

    Original:

    Code:
    //declare variables
      char comboType = ' ';
      double comboPrice = 0.0;
      double orderTotal = 0.0;
       
       //input data  
       
        cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
        cin >> comboType;
        comboType = toupper(comboType);     
            while (comboType != 'T')
        {  
              cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
              cin >> comboType;
              orderTotal += comboPrice; 
            switch (comboType)
        {
              case 'A':
                   comboPrice = 4.25;
                   break;
              case 'B':
                   comboPrice = 5.75;
                   break;
              case 'C':
                   comboPrice = 5.25;
                   break;
              case 'D':
                   comboPrice = 3.75;
                   break;
        //end switch           
        }   
        // end while 
        }             
        //display output 
        if (comboType = 'T')
    {  
        cout << "Please pay this amount: " << orderTotal << endl;
    }
    and now, a bit cleaner...

    Code:
            //declare variables
            char comboType = ' ';
            double comboPrice = 0.0;
            double orderTotal = 0.0;
    
            //input data
            cout << "Enter item ordered [A/B/C/D] or T to calculate total: ";
            cin >> comboType;
    
            while (comboType != 'T'){
    
                    comboType = toupper(comboType);
                    switch (comboType){
                             case 'A':
                                    comboPrice = 4.25;
                                    break;
                            case 'B':
                                    comboPrice = 5.75;
                                    break;
                            case 'C':
                                    comboPrice = 5.25;
                                    break;
                            case 'D':
                                    comboPrice = 3.75;
                                    break;
                    //end switch
                    }
    
                    cin >> comboType;
                    orderTotal += comboPrice;
    
                    //end while
            }
    
    
    
        //display output
            if (comboType = 'T'){
                    cout << "Please pay this amount: " << orderTotal << endl;

    In the first code, within the loop, comboType was being redefined as 0.0 before the cases were defined, so no matter what, he would be missing exactly 5.75...or the first entry he put in during the sequence of B B A D D D where B == 5.75... since the first
    Code:
    orderTotal += comboPrice;
    was ran before the actual defintion of "B" was run, ...just 0.0 = 0.0 + 0.0 and thus the first entry would always be missed, whether this be B B A D D or A B A D D or C B A D D etc..


    Hope this helps, if you have any questions, just ask. ( And for the expert vets here, please feel free to clarify)

    EDIT: Sorry for the spaces instead of TABS..nano formats oddly(But they should be spaced to standard tabs )...
    Last edited by Spazmotic; 03-06-2010 at 11:30 AM.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    First of all, my apologies, it seems that I shouldn't have asked a question. I didn't ask for the answer just someone to review what I had and maybe steer me in the correct direction. In which Spazmotic did steer me in the correct direction. to you I thank you for not giving the answer. and you can see how quickly I figured it out with the direction you gave. I don't want to get anyone into trouble!

    Secondly,
    I see where the toupper is redundant and the counter moved, it is cleaner than mine, but will stick with mine because it works and I did it. I don't want anyone elses work to use!

    again, sorry and many thanks Spazmotic!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  3. Storing input values while iterating
    By russel1013 in forum C Programming
    Replies: 11
    Last Post: 07-29-2008, 09:32 AM
  4. Finding values in input file
    By ToxicLove in forum C Programming
    Replies: 3
    Last Post: 05-02-2004, 07:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

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