Thread: Having problems with my for loop

  1. #1
    Registered User
    Join Date
    Sep 2004

    Having problems with my for loop

    Hello, newbie here

    I need some help with my programming assignment, I've gotten most of it but I'm having problems with what my for loop is displaying. My teacher wanted us to create a program to act as a cash register. I'm supposed to display the output of each transaction after the user has entered all their items. That's where I'm having the problem because when the program displays the stuff in the for loop, it's all gibberish. Here's what my teacher sent me:

    Ok the way you have the program set up you are using a pointer to point to each element of the structure array. I noticed that you were not incrementing the pointer so I add a line of code at the end that does that. Put I know that once you start incrementing the pointer you need to remember where the first element is. So I added some code to create a temporary pointer that you can store the original address. Then when you are ready to read the data back out of the array you just restore the pointer to that first address.

    I don't understand how to go about restoring the pointer to the first address. Here's the source code, and I included a link that shows the program output so you'll see what I'm talking about when I said the for loop is displaying gibberish. Ignore my comments. Any help would be appreciated, thank you very much

    #include <iostream> 
    #include <iomanip> 
    using namespace std; 
    struct Sales 
    //variables listed below 
    float price; 
    float quantity; 
    float discount; 
    float tax; 
    float total; 
    float totaltax; 
    float totaldiscount; 
    //function prototypes listed below 
    void get_name(char name[]); 
    Sales get_info(); 
    int main() 
    Sales* SalesList; 
    Sales* Temp; 
    int itemsold=0; 
    float subtotal=0; 
    float totaldiscount=0; 
    float granddiscountedprice=0; 
    float grandtax=0; 
    float grandtotal=0; 
    float priceb4discount; 
    float priceafterdiscount; 
    int choice; 
    char name1[50]; 
    int index; 
    cout << "Welcome to the Akpala SuperStore!! This program will determine the cost\n"; 
    cout << "of the items you have purchased..."; 
    cout << endl; 
    cout << endl; 
    cout <<"----------------------------------"; 
    cout << endl; 
    SalesList = new Sales[10]; 
    Temp = SalesList; 
    get_name(name1);//Function call so user can enter name of item 
    *SalesList = get_info(); 
    priceb4discount=(SalesList->price*SalesList->quantity); //Calculates subtotal, before discount and tax 
    cout << endl; 
    cout << endl; 
    cout << "To enter another item, press 1, otherwise press 2: "; 
    cin >> choice; 
    cout << endl; 
    for (index=0; index<10; index++) 
    cout << "You purchased " << SalesList[index].quantity << " items at a price of $ " << SalesList[index].price << endl; 
    cout << "each, at a discount of " << SalesList[index].discount << " %" << " and at a tax " << SalesList[index].tax << endl; 
    cout << endl; 
    cout << fixed << showpoint << setprecision(2); 
    cout << endl; 
    cout << "Here is your receipt:"; 
    cout << endl; 
    cout << "--------------------------------------"; 
    cout << endl; 
    cout << "Items sold: " << itemsold << endl; 
    cout << "Subtotal: $ " << subtotal << endl; 
    cout << "Total Discount: $ " << totaldiscount << endl; 
    cout << "Total Tax: $ " << grandtax << endl; 
    cout << "Total Cost: $ " << grandtotal << endl; 
    cout << endl; 
    cout << "Thanks for shopping at the Akpala SuperStore! Please come back soon"; 
    cout << endl; 
    return 0; 
    //Definition of get_name function 
    void get_name(char name[]) 
    cout << "Please enter the name of the item: " ; 
    cin >> name; 
    //Definition of get_info function 
    Sales get_info() 
    Sales items; 
    cout << "Please enter the price of the item: $ "; 
    cin >> items.price; 
    while (items.price < 0 || items.price == 0) 
    cout << "Invalid, please enter a positive amount. "; 
    cin >> items.price; 
    }//Validaton of input 
    cout << "Please enter the quantity of the item: "; 
    cin >> items.quantity; 
    while (items.quantity < 0 || items.quantity == 0) 
    cout << "Invalid, please enter a positive amount. "; 
    cin >> items.quantity; 
    }//Validation of input 
    cout << "Please enter a discount: ";//User enters discount 
    cin >>; 
    while ( < 0 || > 100) 
    cout << "Invalid, please enter an amount greater than 0 or less than 100: "; 
    cin >>; 
    }//Validation of input 
    items.totaldiscount = (items.price*items.quantity*(; //Calculation of total discount 
    cout << "Please enter a tax amount, enter 0 if not taxable: "; //User enters tax, if any 
    cin >>; 
    items.totaltax = (items.price)*(; //Calculation of total tax  (items.price*items.quantity)-(items.totaldiscount) + (items.totaltax); //Total amount of sales 
    return items; 

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Well, good thing you used code tags. Unfortunately, it seems the indentation didn't paste properly into the code window.

    I don't want to read through all of the code, since:
    1) Your variable names are hard to read ("totaldiscountedprice" not "totalDiscountedPrice")
    2) It's a long-ish program
    3) And the indentation in the code is shot

    So, I'll see what I can do without extensively examining your code.
    >I don't understand how to go about restoring the pointer to the first address.
    Ok, pointers basically act as any other variable. You can store them, copy them, etc. So in this way, as long as you create a temporary pointer (as mentioned in the quote you posted), you can:
    a) Assign it the value of the original pointer (pBackupPointer = pOriginalPointer; )
    b) Move the original pointer away (++pOriginalPointer; pOriginalPointer = &something; etc.)
    c) Restore it using the backup (pOriginalPointer = pBackupPointer; )
    d) Tada, pOriginalPointer is pointing back where it was before you moved it!

    Next: While investigating your for loop, I noticed the do-while loop just before it. You must remember the scope rules in C++. If you want to create SalesList, you must create it before the loop begins - otherwise you're creating a new array for each iteration of the loop! Similarly, the "Temp = SalesList;" line must be before the loop begins, otherwise you're effectively destroying your backup pointer. Once this is fixed, the for loop will probably work properly.
    Last edited by Hunter2; 09-16-2004 at 06:45 PM. Reason: Too many smilies...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM
Website Security Test