Dynamic Arrays

This is a discussion on Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; I have created a program allows you to input the amount of items purchased, and then input the price of ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    17

    Dynamic Arrays

    I have created a program allows you to input the amount of items purchased, and then input the price of each item and the sales tax. It then creates a receipt which totals all the prices and sales tax.

    Now I want to be able to list the items and their prices in the sales receipt as well. What would I have to do so lets say you pick 5 items and type in 5 prices and it give you the sales receipt, before it tells you the total, I want it to list the 5 items so looks like this:

    Sales Receipt

    Item 1: Price
    Item 2: Price
    Item 3: Price
    Item 4: Price
    Item 5: Price

    **********

    (rest of sales receipt)

    Here is what I have so far:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    char rerun;
    
    		do //The do-while for a program rerun option
    		{
    
    // Declare variables: counter, items, sales items, sales total, sales tax, and grand total
    int items, count;
    int * price;
    double total, percent, g_t, tax;
    
    // Input information
    	cout<<"How many sales items do you have? :";
    	cin>>items;
    
    	price= new (nothrow) int[items];
    	if (price == 0)
    		cout << "error";
    	else
    	{
    			for (count = 0; count < items; count++)
    			{
    				cout<<"Enter the value of the sales item. : $";
    				cin>>price[count];
    				total = total + price;
    			}
    	}
    		
    	cout << endl << endl;
    
    	cout<<"Enter in the sales tax percentage. :";
    	cin>>percent;
    		
    		tax = total * (percent/100);
    		g_t = tax + total;
    		
    	cout << endl << endl;
    		
    	cout << "********************************************" << endl;
    	cout << "********  S A L E S  R E C E I P T  ********" << endl;
    	cout << "********************************************" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "entered";
    	for (count = 0; count < items; count++)
    		cout << price[count] << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << setiosflags(ios::fixed) << setprecision(2);
    	cout << "**  Total Sales:    $" << setw(9) << total << "            **" << endl;
    	cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
    	cout << "**                  -----------           **" << endl;
    	cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "********************************************" << endl;
    		
    	cout << endl << endl;
    
    		cout<<"Do you want to run this program again? (y/n)";
    		cin>>rerun;
    			
    		}
    
    	while (rerun == 'y' || rerun == 'Y');
    	
    	system ("PAUSE");
    	return 0;
    } //End Main Function
    I receive the following error:
    salerec.cpp(33) : error C2111: pointer addition requires integral operand
    Error executing cl.exe.

    salerec.exe - 1 error(s), 0 warning(s)
    How should I be writing it so that an operator is used on a pointer?

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Edit: Sorry, misread the code.

    You need to change this:

    Code:
    total = total + price;
    To:

    Code:
    total = total + price[count];
    
    // or
    
    total += price[count];
    Then it should compile.
    Last edited by Ushakal; 08-04-2011 at 06:23 PM.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    char rerun;
    
    		do //The do-while for a program rerun option
    		{
    
    // Declare variables: counter, items, sales items, sales total, sales tax, and grand total
    int items, count;
    int * price;
    double total, percent, g_t, tax;
    
    // Input information
    	cout<<"How many sales items do you have? :";
    	cin>>items;
    
    	price= new (nothrow) int[items];
    	if (price == 0)
    		cout << "error";
    	else
    	{
    			for (count = 0; count < items; count++)
    			{
    				cout<<"Enter the value of the sales item. : $";
    				cin>>price[count];
    				total = total + price[count];
    			}
    	}
    		
    	cout << endl << endl;
    
    	cout<<"Enter in the sales tax percentage. :";
    	cin>>percent;
    		
    		tax = total * (percent/100);
    		g_t = tax + total;
    		
    	cout << endl << endl;
    		
    	cout << "********************************************" << endl;
    	cout << "********  S A L E S  R E C E I P T  ********" << endl;
    	cout << "********************************************" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	for (count = 0; count < items; count++)
    		cout << price[count] << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << setiosflags(ios::fixed) << setprecision(2);
    	cout << "**  Total Sales:    $" << setw(9) << total << "            **" << endl;
    	cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
    	cout << "**                  -----------           **" << endl;
    	cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "********************************************" << endl;
    		
    	cout << endl << endl;
    
    		cout<<"Do you want to run this program again? (y/n)";
    		cin>>rerun;
    			
    		}
    
    	while (rerun == 'y' || rerun == 'Y');
    	
    	system ("PAUSE");
    	return 0;
    } //End Main Function
    Fixed it. The prices are now being displayed except it screwed up everything else in the display.

    http://i44.servimg.com/u/f44/16/74/15/46/output10.jpg

    It should be like this:
    http://i44.servimg.com/u/f44/16/74/15/46/output11.jpg

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't initializing total before you start adding to it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Quote Originally Posted by quzah View Post
    You aren't initializing total before you start adding to it.


    Quzah.
    How would I go about doing that?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    You need to initialise the variable total to 0 before you use it for the sum of the prices entered by the user, otherwise it could contain any value and give you an incorrect output.

    Edit:
    Quote Originally Posted by xenhancd View Post
    How would I go about doing that?
    Code:
    total = 0;
    Before you start using it.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Quote Originally Posted by Ushakal View Post
    You need to initialise the variable total to 0 before you use it for the sum of the prices entered by the user, otherwise it could contain any value and give you an incorrect output.

    Edit:


    Code:
    total = 0;
    Before you start using it.
    DUH! Not sure why I changed that from the get go, I originally had total = 0.

    However, I still cannot get the sales receipt portion to look the way I need to. It seems I cannot simply just add a cout and sales items there. How would I get it so that it automatically lists the number of items with the price. right now only the price is being listed.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    cout << "Item # << itemnumber << ", quantity: " << howmany << endl;
    Something like that. You just need to add a section there to actually display what you want to display.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    I do have that in my code and it displays correctly, however, I want to number my items. I want it to ask "Input sales item 1" "Input sales item 2" "Input sales item 3" and then on the receipt I want it to do the same thing:

    Sale item 1: $(price)
    Sale item 2: $(price)
    Sale item 3: $(price)

    I want those number to show up. I can't find any tutorials or anything to read that teaches how to number items either.

    Here is my current code:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    char rerun;
    
    		do //The do-while for a program rerun option
    		{
    
    // Declare variables: counter, items, sales items, sales total, sales tax, and grand total
    int items, count;
    int * price;
    double total = 0, percent, g_t, tax;
    
    // Input information
    	cout<<"How many sales items do you have? :";
    	cin>>items;
    
    	price= new (nothrow) int[items];
    	if (price == 0)
    		cout << "error";
    	else
    	{
    			for (count = 0; count < items; count++)
    			{
    				cout<<"Enter the value of the sales item: $";
    				cin>>price[count];
    				total = total + price[count];
    			}
    	}
    		
    	cout << endl << endl;
    
    	cout<<"Enter in the sales tax percentage: ";
    	cin>>percent;
    		
    		tax = total * (percent/100);
    		g_t = tax + total;
    		
    	cout << endl << endl;
    
    	cout << "********************************************" << endl;
    	cout << "********  S A L E S  R E C E I P T  ********" << endl;
    	cout << "********************************************" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	for (count = 0; count < items; count++)
    		cout << "**  Sales Item #:    $" << setw(9) << setprecision(2) << price[count] << "           **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "********************************************" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << setiosflags(ios::fixed) << setprecision(2);
    	cout << "**  Total Sales:    $" << setw(9) << total << "            **" << endl;
    	cout << "**  Sales Tax:      $" << setw(9) << tax << "            **" << endl;
    	cout << "**                  -----------           **" << endl;
    	cout << "**  Grand Total:    $" << setw(9) << g_t << "            **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "**                                        **" << endl;
    	cout << "********************************************" << endl;
    		
    	cout << endl << endl;
    
    		cout<<"Do you want to run this program again? (y/n)";
    		cin>>rerun;
    			
    		}
    
    	while (rerun == 'y' || rerun == 'Y');
    	
    	system ("PAUSE");
    	return 0;
    } //End Main Function

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Look, you are not going to find a specific tutorial for every possible programming scenario you are going to come across. This is where you need to apply what you learn to solve problems. Your questions are verging on ridiculous. Now apply this to the rest of your problem:
    Code:
    #include <iostream>
    
    int main(){
    
    	int itemNumber = 1;
    
    	for(int i = 0; i < 5; i++){
    		std::cout<<"Enter item number "<<itemNumber<<":"<<std::endl;
    		itemNumber++;
    	}
    
    	std::cin.get();
    	return (0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    17
    Code:
    cout << "**  Sales Item " << count + 1 << ":   $" << setw(9) << setprecision(2) << price[count] << "            **" << endl;
    Ended up doing it that way and it works like a charm!

    I found one big issue! I cannot input decimal value for the price of an item. I know this is because price is set to integer. I need to be able to use decimal values for the price. Previously I used double for price originally but to create the dynamic array, I changed it to integer.

    What options are there? Or what can I do? The entire program just stops working as soon as I input decimals into the sales item price such 10.50.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Simply change your price array to an array of floats or doubles, e.g. double *price; and price = new (nothrow) double[items];
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    By the way, why do you use no-throw new[]? Actually, why do you directly use new[] when you can use a std::vector<int>?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    By the way, why do you use no-throw new[]? Actually, why do you directly use new[] when you can use a std::vector<int>?
    I'm sure that is what they are learning in whatever class the OP is taking. From what I've seen on this board many of the textbooks in use are creating many C+ programmers in order to still teach some basic comp sci stuff, such as arrays and memory management.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    But the OP has not mentioned such a thing, thus we are in the dark, hence the necessary question.
    Also important, you have a memory leak because you use new and not delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. 2d dynamic arrays
    By zbest in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2008, 12:53 PM
  2. dynamic arrays
    By s.nelson64 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2008, 06:27 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. help with dynamic arrays
    By Chaplin27 in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2004, 09:22 PM
  5. Dynamic arrays
    By fry in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2002, 06:00 AM

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