CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2012
    Posts
    2

    Total not adding correctly

    Hey all
    I've written a program that is designed to add records which include, itemId, itemPrice, discount, quantity. It then adds the Item prices of all the records together (Well that's what it should be doing), but the problem i'm experiencing is that i'm not getting the total of all records. Any help would be appreciated.

    Code:
    #include <iostream>
    using namespace std;
     
    int readSaleRecord(unsigned&, double&, char&, unsigned&, double&);
    double calculateItemCost(double itemPrice, double& totalCost, char discountType, unsigned quantity);
    void displayTotalCost(double totalCost, unsigned recordNum, bool aborted);
     
    int main()
    {
     unsigned itemId, quantity;
     double itemPrice, totalCost, recordNum= 0, discount;
     char discountType;
     bool aborted=false;
     
    readSaleRecord(itemId, itemPrice, discountType, quantity, recordNum);        
    calculateItemCost(itemPrice, totalCost, discountType, quantity);          
    displayTotalCost(totalCost, recordNum, aborted);
     
    system("pause");
    return 0;
    }
     
    void displayTotalCost(double totalCost, unsigned recordNum, bool aborted)
         {
          cout << "The total cost is $" <<totalCost<<"\n";
          cout << "from " <<recordNum<<" record(s) \n";
       return;
         }
     int readSaleRecord(unsigned & itemId, double & itemPrice,char & discountType, unsigned & quantity, double& recordNum)
    {
        int anotherSale = 0;
        cout << "Enter item ID: ";
        cin >> itemId;
            if (itemId==0 || itemId >1000000)
               {
               cout << "incorrect id";
               system("pause");
               exit(0);
               }
       
        cout << "Enter full item price: ";
        cin >> itemPrice;
     
            if (itemPrice<0)
               {
               cout << "please enter postive integer";
               system("pause");
               exit(0);
               }
     
        cout << "Enter discount type: ";
        cin >> discountType;
     
            if (discountType!='N' && discountType!='D' && discountType!='T' && discountType!='B')
               {
               cout << "invalid entry";
               system("pause");
               exit(0);
               }
     
        cout << "Enter quantity: ";
        cin >> quantity;
            if (quantity<0)
               {
               cout << "please enter postive integer";
               system("pause");
               exit(0);
               }
     
        cout << "would you like to enter another sale? (1 for yes and 0 for no)";
        cin >> anotherSale;
        recordNum++;
     
     
             while(anotherSale==1)
                 {
                 cout << "Enter item ID: ";
                 cin >> itemId;
     
                     if (itemId==0 || itemId >1000000)
                        {
                         system("pause");
                          exit(0);
                         }
       
                 cout << "Enter full item price: ";
                 cin >> itemPrice;
     
                      if (itemPrice<0)
                       {
                       cout << "please enter postive integer";
                       system("pause");
                       exit(0);
                       }
     
                 cout << "Enter discount type: ";
                 cin >> discountType;
     
                     if (discountType!='N' && discountType!='D' && discountType!='T' && discountType!='B')
                     {
                       cout << "invalid entry";
                       system("pause");
                       exit(0);
                       }
     
                  cout << "Enter quantity: ";
                  cin >> quantity;
     
                      if (quantity<0)
                          {
                         cout << "please enter postive integer";
                         system("pause");
                         exit(0);
                          }
     
                  cout << "Enter another record? (1 for yes and 0 for no)";
                  cin >> anotherSale;
                  recordNum++;
                  }
     
    return (itemId, itemPrice, discountType, quantity, recordNum);
     
    }
    
     
    double calculateItemCost(double itemPrice, double& totalCost, char discountType, unsigned quantity){
    double discount;
       switch (discountType){
     
           case 'N': discount= 1; break;
           case 'B': discount = 0.9; break;
           case 'D': discount = 0.8; break;
           case 'T': discount = 0.7; break;
         
            default: discount =1;
             }
    totalCost=itemPrice*discount*quantity;
       
    return totalCost;
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Total not adding correctly

    I'm not trying to be rude but this is a good opportunity to learn how to debug. MSVC has a very good debugger so start your code by pressing F10 (it will start at the very first line in main). Single step with F10 and step into a function with F11. After every line has been passed check the content of your variables by hovering with the mouse over them. Are they as you expect them to be? Why not? How can you correct the bug?

    Some other commonly used keys:
    Shift+F5 terminates the debugging session.
    Shift+F11 steps out of the function
    F9 sets a breakpoint on the current line. F9 again clears it.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Total not adding correctly

    I'll just add that calling exit from inside a function like that is terrible form. Break that habit now.

    Also functions can only return one value.
    return (itemId, itemPrice, discountType, quantity, recordNum);
    Isn't doing what you think it is.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Total not adding correctly

    Quote Originally Posted by Geordienev View Post
    Hey all
    I've written a program that is designed to add records which include, itemId, itemPrice, discount, quantity. It then adds the Item prices of all the records together (Well that's what it should be doing), but the problem i'm experiencing is that i'm not getting the total of all records. Any help would be appreciated.
    As S_M_A mentioned, use the debugger.

    Also, your code is very poorly formatted. For the sake of the others looking at your code:
    Code:
    #include <iostream>
    using namespace std;
    
    int readSaleRecord(unsigned&, double&, char&, unsigned&, double&);
    double calculateItemCost(double itemPrice, double& totalCost, char discountType, unsigned quantity);
    void displayTotalCost(double totalCost, unsigned recordNum, bool aborted);
    
    int main()
    {
        unsigned itemId, quantity;
        double itemPrice, totalCost, recordNum= 0, discount;
        char discountType;
        bool aborted=false;
    
        readSaleRecord(itemId, itemPrice, discountType, quantity, recordNum);        
        calculateItemCost(itemPrice, totalCost, discountType, quantity);          
        displayTotalCost(totalCost, recordNum, aborted);
    
        system("pause");
        return 0;
    }
    
    void displayTotalCost(double totalCost, unsigned recordNum, bool aborted)
    {
        cout << "The total cost is $" <<totalCost<<"\n";
        cout << "from " <<recordNum<<" record(s) \n";
        return;
    }
    
    int readSaleRecord(unsigned & itemId, double & itemPrice,char & discountType, unsigned & quantity, double& recordNum)
    {
        int anotherSale = 0;
        cout << "Enter item ID: ";
        cin >> itemId;
        if (itemId==0 || itemId >1000000)
        {
            cout << "incorrect id";
            system("pause");
            exit(0);
        }
    
        cout << "Enter full item price: ";
        cin >> itemPrice;
    
        if (itemPrice<0)
        {
            cout << "please enter postive integer";
            system("pause");
            exit(0);
        }
    
        cout << "Enter discount type: ";
        cin >> discountType;
    
        if (discountType!='N' && discountType!='D' && discountType!='T' && discountType!='B')
        {
            cout << "invalid entry";
            system("pause");
            exit(0);
        }
    
        cout << "Enter quantity: ";
        cin >> quantity;
        if (quantity<0)
        {
            cout << "please enter postive integer";
            system("pause");
            exit(0);
        }
    
        cout << "would you like to enter another sale? (1 for yes and 0 for no)";
        cin >> anotherSale;
        recordNum++;
    
    
        while (anotherSale==1)
        {
            cout << "Enter item ID: ";
            cin >> itemId;
    
            if (itemId==0 || itemId >1000000)
            {
                system("pause");
                exit(0);
            }
    
            cout << "Enter full item price: ";
            cin >> itemPrice;
    
            if (itemPrice<0)
            {
                cout << "please enter postive integer";
                system("pause");
                exit(0);
            }
    
            cout << "Enter discount type: ";
            cin >> discountType;
    
            if (discountType!='N' && discountType!='D' && discountType!='T' && discountType!='B')
            {
                cout << "invalid entry";
                system("pause");
                exit(0);
            }
    
            cout << "Enter quantity: ";
            cin >> quantity;
    
            if (quantity<0)
            {
                cout << "please enter postive integer";
                system("pause");
                exit(0);
            }
    
            cout << "Enter another record? (1 for yes and 0 for no)";
            cin >> anotherSale;
            recordNum++;
        }
        return(itemId, itemPrice, discountType, quantity, recordNum);
    }
    
    double calculateItemCost(double itemPrice, double& totalCost, char discountType, unsigned quantity)
    {
        double discount;
        switch (discountType)
        {
            case 'N': discount= 1; break;
            case 'B': discount = 0.9; break;
            case 'D': discount = 0.8; break;
            case 'T': discount = 0.7; break;
        
            default: discount =1;
        }
        totalCost=itemPrice*discount*quantity;
    
        return totalCost;
    }
    This line doesn't make sense:
    Code:
        return(itemId, itemPrice, discountType, quantity, recordNum);
    As GCDEF stated, only one value can be returned. If you really wanted to return 5 values, then it's time to learn what a struct is in C++, as that is the most popular way to return multiple values at once (the other is a std::tuple, but that is for later).

    Right now, you have all of your related items such as itemId, itemPrice, discountType, etc. in separate variables, thereby having to write functions that take 5 arguments. What if you had to maintain 50 related items? Write all your functions with 50 parameters? That would be crazy, and that is what a struct prevents from happening.
    Code:
    struct SaleInfo
    {
        unsigned itemId;
        unsigned quantity;
        double itemPrice;
        double totalCost;
        // add more related items.
    };
    Then you pass a SaleInfo around instead of 5, 6, 7, or 50 different variables. Each SaleInfo consolidates all the information for one sale. See how easier that looks?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 31st, 2012 at 04:17 PM.

  5. #5
    Join Date
    May 2012
    Posts
    2

    Re: Total not adding correctly

    Cheers for the replies guys, I'll be the first to admit that my coding is absolute shiet to read lol.
    But with the struct, do you declare that at the start of the code or within the fucntion? and how exactly do you use it.
    Do you replace the;
    Code:
    return (itemId, itemPrice, discountType, quantity, recordNum);
    Or does it sit somewhere else, I can understand why its simpler to use and i thank you for that.
    I'll also give it a go at debugging now.

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Total not adding correctly

    Geordienev: Please do not repost the same question in a different section (Non Visual C++ Issues). you are getting good help here, and almost all the same people reply in both sections..

    Thanks.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Total not adding correctly

    Quote Originally Posted by Geordienev View Post
    Cheers for the replies guys, I'll be the first to admit that my coding is absolute shiet to read lol.
    But with the struct, do you declare that at the start of the code or within the fucntion? and how exactly do you use it.
    Do you replace the;
    Code:
    return (itemId, itemPrice, discountType, quantity, recordNum);
    Or does it sit somewhere else, I can understand why its simpler to use and i thank you for that.
    I'll also give it a go at debugging now.
    Using a struct is one way. You'd create the struct and return it. In your case, it's not really necessary as you're passing in the parameters by reference. That means that when you set the values of the parameters in the function, they'll contain the values in the calling function when the called function returns. For example

    Code:
    void somefunction(int& i)
    {
        i = 1;
    }
    
    int main()
    {
        int nInt = 0;
        somefunction(nInt);
        //nInt = 1 now.
    }
    Setting up parameters like that are called output parameters. That's typically how you get a function to populate multiple variables.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Total not adding correctly

    Quote Originally Posted by Geordienev View Post
    Cheers for the replies guys, I'll be the first to admit that my coding is absolute shiet to read lol.
    But with the struct, do you declare that at the start of the code or within the fucntion?
    For now, all you need to do is declare the struct right before main().
    Code:
    struct SaleInfo
    {
        unsigned itemId;
        unsigned quantity;
        double itemPrice;
        double totalCost;
        // add more related items.
    };
    
    int main()
    {
       SaleInfo mySale;  // declare a variable of type SaleInfo
    }
    As long as whoever needs to know about SaleInfo has it defined before use, you're OK. Note that you now treat SaleInfo just like any other type such as int, double, etc. when it comes to declaring variables, return values, etc.

    Second, you use a struct (in this case), using the dot operator to operate on each item in the struct.
    Code:
    int main()
    {
       SaleInfo mySale;  // declare a variable of type SaleInfo
       SaleInfo mySale2;  // declare a variable of type SaleInfo
       //...
       mySale.itemInfo = whatever;
       mySale.quantity = 10;
      //  etc...
       mySale2.itemInfo = another_item;
       mySale2.quantity = 4;
    }
    In the fake example above, there are two completely different SaleInfo items. Each one contains all of the information for a SaleInfo.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 1st, 2012 at 11:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured