Need help urgently

This is a discussion on Need help urgently within the C++ Programming forums, part of the General Programming Boards category; I have a program due tonight and am having numerous problems. Here is one function that is not working right: ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    Exclamation Need help urgently

    I have a program due tonight and am having numerous problems. Here is one function that is not working right:

    Code:
    void accept_item_code (char& upc)
    {
       bool valid = false;
    
       cout << endl << "--------------------" << endl;
       cout << "Enter product code: ";
       cin >> upc;
       upc = toupper(upc);
       cout << upc;
       cout << endl;
       while (!valid)
       {
          if (upc == 'A' || upc == 'B' || upc == 'C' || upc == 'D' || upc == 'E')
          {
             valid = true;
          }
          else if (upc == 'F' upc == 'G' || upc == 'H' || upc == 'I' || upc == 'X')
          {
             valid = true;
          }
          else
          {
             cout << "!!! Invalid product code" << endl << endl
                  << "Enter product code: ";
             cin >> upc;
             upc = toupper(upc);
             cout << endl;
          }
       }
    
       return;
    }
    Josh Stevanus
    jrstevan@svsu.edu

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Isn't this like the 25th topic you've posted? Please keep them all in one thread.

    You need to be more specific, tell us what is not working, show more code (how do you call this function), tell us what input you are giving it.

    More information, not more threads!

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    i. Read the homework policy at the top of the board.
    ii. Ask a very specific question, and it is possible that someone may help.
    iii. I count two problems right off the bat that won't allow your code to compile. Fix them and you may be in better shape.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Code:
    void accept_item_code (char& upc)
    {
       bool valid = false;
    
       cout << endl << "--------------------" << endl;
       cout << "Enter product code: ";
       cin >> upc;
       upc = toupper(upc);
       cout << upc;
       cout << endl;
       while (!valid)
       {
          if (upc == 'A' || upc == 'B' || upc == 'C' || upc == 'D' || upc == 'E')
          {
             valid = true;
          }
          else if (upc == 'F' || upc == 'G' || upc == 'H' || upc == 'I' || upc == 'X')
          {
             valid = true;
          }
          else
          {
             cout << "!!! Invalid product code" << endl << endl
                  << "Enter product code: ";
             cin >> upc;
             upc = toupper(upc);
             cout << endl;
          }
       }
    
       return;
    }
    try this. I fixxed what I found, im not saying its 100
    fixed though. It is an easything to fix, try reading it over carefully before posting.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    function call

    You're right, I have posted many topics. I will continue using this thread. As far as my function call I'm just using:

    accept_item_code(upc);


    another note: do I need to declare upc as a char in my main function before the function call?


    Josh Stevanus
    jrstevan@svsu.edu

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    A variable declared/defined in one function (like main) is completely separate from a variable declared in a different function (like accept_item_code). The variable named upc is declared in the function accept_item_code as a parameter, but if you had another variable (also named upc) in main that you are using to call accept_item_code, then yes, you'd have to declare it in main.

    When I said more information, I meant a lot more infromation:
    Quote Originally Posted by jlou
    tell us what is not working, show more code (how do you call this function), tell us what input you are giving it.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    19
    Frantic, I don't see where you changed my code at all...

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by jstevanus
    Frantic, I don't see where you changed my code at all...
    It has been mentioned a couple times in other threads:
    Quote Originally Posted by jlou
    Also notice a missing || in your code:
    Code:

    else if (c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'X')

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    19
    jlou, after I make the function call accept_item_code(upc); i use cout to print a message so that I know the function call was completed. I do not get that message, so I know the function is wrong somehow.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    19
    the || portion of my IF statement has been fixed, I can post the new code if you would like. Thanks again for your patience. I am sorry if I'm unable to phrase my questions correctly, this is my first class. As far as the homework policy is concerned, I am allowed to get outside help but I do not expect anyone to do all my work for me. I am interested in learning this too.

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    That's better, but a lot more information is needed. If the code is small enough, post it all. If not, post all of the code in your main function. Make note of where that function is being called. Tell us what input you are giving it (what letter are you typing in when it asks for a letter?).

    More, more, more information. You are giving us a thimble-full, we need a dump-truck.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    19
    Ok, I'm posting my *.cpp program here, I also have a *.h file with my function prototypes and struct definition included.

    Code:
    // Josh Stevanus Prog 5
    // last program
    
    using namespace std;
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cctype>
    
    
    
    #include "cs116prog5.h"
    
    
    int main()
    {
       ifstream inFile;
       SalesItem itemList[NUM_ITEMS];       // the table for the whole sale items o
       int itemsPurchased[NUM_ITEMS];       // the quantity of each item per each e
       char cont;
       char upc;
       float subTotal;
       float itemTotal;
       float total;
       float dailyTotal = 0.00;
      
       if (!inFile)
       {
          cout << "Cannot open the input file!" << endl;
          return 1;
       }
    
       initialize_item_list (itemList);     // initialize the item table
    
       cout << endl << "Welcome to the POST system!" << endl << endl
            << "Beginning a new sale? (Y/N) ";
       cin >> cont;
       cont = toupper(cont);
    
       while (cont == 'Y')
       {
          initialize_items_purchase_list (itemsPurchased);
          accept_item_code(upc);         // get a valid item code
          cout << endl << endl << endl << "item code done";
    
          while (upc != 'X')
          {
             get_item_name (itemList, upc); // prints the item name for upc
             int quantity = get_quantity (itemsPurchased);
    
             cout << "        item total: $ "; 
             itemTotal = get_item_total(itemList, upc, quantity);
    	 cout << fixed << showpoint << setprecision(2) << itemTotal
                  << endl << endl;
    
             accept_item_code(upc);      // SELF-ADDED FUNCTION CALL
    	  }
    
          subTotal = list_sales_items (itemList,itemsPurchased);
    
          total = get_total(subTotal, TAX_RATE);
                    // Computes the total sales amount with tax
          dailyTotal += total;
          cout << "Beginning a new sale? (Y/N) ";
          cin >> cont;
          cont = toupper(cont);
       }
    
       cout << endl << "===================" << endl 
    	<< "The total sale for the day is  $" << setw(7) << fixed
            << setprecision(2) << dailyTotal << endl << endl
    	<< "Thanks for using POST system. Goodbye." << endl;
    
       return 0;
    }
    
    
    // function definitions
    
    void initialize_item_list (SalesItem itemList[])
    {
       ifstream inFile;
       int i = 0;
    
       inFile.open("items.txt");            // read items data from an input file
      
    
    
       inFile >> itemList[0].upc;            // priming read
    
       while (inFile)
       {
          inFile >> itemList[i].name;
          inFile >> itemList[i].price;
          i++;
          inFile >> itemList[i].upc;
       }
       cout << "INITIALIZE_ITEM_LIST FUNCTION COMPLETE" << endl; // DEBUG
    }
    
    void initialize_items_purchase_list (int itemsPurchased[])
    {
       cout << "INITIALIZE_ITEMS_PURCHASE_LIST STARTED";   // DEBUGGING
       for (int i=0; i < NUM_ITEMS; i++)
       {
          itemsPurchased[i] = 0;
       }
    }
    
    void accept_item_code (char& upc)
    {
       bool valid = false;
    
       cout << endl << "--------------------" << endl;
       cout << "Enter product code: ";
       cin >> upc;
       upc = toupper(upc);
       cout << upc;
       cout << endl;
       while (!valid)
       {
          if (upc == 'A' || upc == 'B' || upc == 'C' || upc == 'D' || upc == 'E')
          {
             valid = true;
          }
          else if (upc == 'F' || upc == 'G' || upc == 'H' || upc == 'I' || upc 
                       == 'X')
          {
             valid = true;
          }
          else
          {
             cout << "!!! Invalid product code" << endl << endl
                  << "Enter product code: ";
             cin >> upc;
             upc = toupper(upc);
             cout << endl;
          }
       }
    
       return;
    }
    
    
    void get_item_name (const SalesItem itemList[], char upc)
    {
       int i=0;
    
       while (i < NUM_ITEMS)
       {
          if (itemList[i].upc == upc)
          {
             cout << setw(18) << "item name: " << itemList[i].name;
          }
       }
    }
          
       
    int get_quantity (int itemsPurchased[])
    {
       static int i=0;
       int item;
    
       cout << endl << "Enter quantity:      ";
       cin >> itemsPurchased[i];
       item = itemsPurchased[i];
       cout << endl;
       i++;
    
       return item;
    }
       
    float get_item_total (const SalesItem itemList[], char upc, int quantity)
    {
       float itemTotal;
    
       for (int i=0; i < NUM_ITEMS; i++)
       {
          if (itemList[i].upc == upc)
          {
             itemTotal = itemList[i].price * quantity;
          }
       }
    
       return itemTotal;
    }
    
    
    float list_sales_items (const SalesItem itemList[],const int itemsPurchased[])
                    // Lists the quantity, name, and price for all sale items, and
                    // returns the subtotal of all items purchased
    
    {
       float subTotal = 0;
       float itemTotal = 0;
       
       cout << "----------------------------" << endl << "Items list:" << endl;
    
       for (int i=0; i < NUM_ITEMS; i++)
       {
          if (itemsPurchased[i] != 0)
          {
             cout << setw(5) << itemsPurchased[i] << " " << itemList[i].name
    			  << setw(10) << "$"; 
    	 itemTotal = itemsPurchased[i] * itemList[i].price;
    	 cout << setw(7) << itemTotal << endl;
    	 subTotal = subTotal + itemTotal;
          }
       }
    
       return subTotal;	  
    }
    
    float get_total(float subTotal, float TAX_RATE)
    {                // Computes the total sales amount with tax
       float tendered;
       float total;
    
       cout << "Subtotal" << setw(13) << "$" << setw(7) << subTotal << endl
            << "Total with Tax (6%)  $" << setw(7) << fixed << showpoint
    	<< setprecision(2);
       total = subTotal * TAX_RATE;
       cout << total << endl << "Tendered amount     $  ";
       cin >> tendered;
       cout << endl << "Change              $" << setw(7)
            << tendered - total << endl << "----------------------------"
    	<< endl << endl;
    
       return total;
    }
    The user inputs an item code, then the program prints the corresponding name. A text file contains a list of each product code, name, and price and my array and struct stores that information. The user also then enters the quantity of the item, and then the total cost is printed. The user continues entering code, quantity, price, until they are finished they type "X" as their product code. Then, a list is printed including the quantity, name, and price of each item, then the subtotal, then total with tax, then the user enters the amount "tendered", then the change is printed. The program then asks if they want to "begin a new sale" and if they enter "N" the "total sale for the day" is printed and the program exits.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    19
    This is my header file cs116prog5.h that goes with my cs116prog5.cpp file:

    Code:
    const float TAX_RATE = 0.06;
    const int NUM_ITEMS = 9;
    
    struct SalesItem
    {
       char upc;       // universal product code
       string name;    // item name
       float price;    // unit price
    };
    
    // function prototypes
    
    // initialize the sales table (item code/name/price)
    void initialize_item_list (SalesItem itemList[]);
    
    // initialize all elements to zero before a new sale begins
    void initialize_items_purchase_list (int itemsPurchased[]);
    
    // accept an item code
    void accept_item_code (char&);
    
    // get the item name for the upc and print it
    void get_item_name (const SalesItem itemList[], char upc);
    
    // accept the quantity for an item
    int get_quantity (int itemsPurchased[]);
    
    // compute the item total for each item
    float get_item_total (const SalesItem itemList[], char upc, int quantity);
    
    // lists the quantity, name, and price for all sale items and returns
    // the subtotal of all items purchased
    float list_sales_items (const SalesItem itemList[], const int itemsPurchased[]);
    
    // computes the total sales amount with tax
    float get_total(float subTotal, float TAX_RATE);

    Josh Stevanus

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Well done Josh! I would have preferred you give me the sample input that you gave it (I only asked three times ), but your current problem is most likely in here:
    Code:
    void get_item_name (const SalesItem itemList[], char upc)
    {
       int i=0;
    
       while (i < NUM_ITEMS)
       {
          if (itemList[i].upc == upc)
          {
             cout << setw(18) << "item name: " << itemList[i].name;
          }
       }
    }
    You never increment i, so it goes into an infinite loop because i is always 0!

    There are probably other issues, but hopefully you can continue to work on them. I assume that the reason "item code done" is not displayed for you is that it was not flushed to the console output before your infinite loop started (notice get_item_name is called right after you tried to print "item code done"). Just because you send something to cout doesn't mean it gets printed right away.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    19
    jlou,

    thanks for getting back online!! I found that problem myself, and was glad when I did find it. Do you have an instant messaging program you use that I could communicate with you on? It seems that would be much easier. The problem I'm having now is with my order summary screen. I need to print out the quantity, name, and price of each item but it seems impossible since my itemsPurchased[] array only holds ints. It seems like my SalesItem struct should include a quantity variable. What do you think?

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

Similar Threads

  1. Urgently in need of help...!!!
    By patron in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 10:12 PM
  2. Need urgently help!
    By Frandy in forum Windows Programming
    Replies: 4
    Last Post: 03-05-2005, 07:02 AM
  3. Help urgently needed, please =(
    By *Michelle* in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-03-2003, 07:14 AM
  4. Need Help Urgently
    By chener in forum C Programming
    Replies: 3
    Last Post: 06-26-2002, 05:21 PM

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