array help

This is a discussion on array help within the C++ Programming forums, part of the General Programming Boards category; i need to write code for a program that 1. allows user to enter a code 2. will display the ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20

    array help

    i need to write code for a program that

    1. allows user to enter a code
    2. will display the amount and quantity
    3. the user can enter as many code ids without having to execute the program again


    how would i write this?

    here my code

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {	
    	//arrays
    	int codes[5]        = {22, 64, 74, 85,98};
    	int amount[5]     = {345, 650, 240, 250, 215};
    	int quantity[5] = {7, 1, 8, 10, 3};
    
    	
    
    	return 0;
    }
    i just need help not the answer

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    3. the user can enter as many code ids without having to execute the program again
    This is probably the easiest of the requirements, so put it on hold until the other two are done--ignore it for now.

    1. allows user to enter a code
    How do you get a number from the user? Use "scanf" with the format specifier used to denote an integer. Search around for examples of or the manual page of "scanf".

    After you have the number, you have to make sure it's a valid code. To do this you could either use 5 "if" statements, checking each of the 5 valid codes. The better approach would be to iterate over every code, and make sure it's equal to one of them. If it's not equal to any 5, so it's not a valid code, so you could keep asking the user to enter a valid code (which implies another loop).

    2. will display the amount and quantity
    When you're iterating over the codes to make sure what the user entered is a valid code, once you arrive at the valid code, you know the index that it exists at. So you simply use "printf" to print this index of the two other arrays, to print this code's amount and quantity.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    In C++, there are smart arrays called vectors you can use that can automatically resize themselves to hold as much data as you need them to. You can make a vector of integers like this:

    Code:
    vector<int> codes;
    codes.push_back(5); /* Put 5 into vector codes */
    codes.push_back(4); /* Put 4 into vector codes */
    Vectors automatically keep track of how many elements they are storing. You can find out just how many with the size() member function:
    Code:
    cout<<"There are "<<codes.size()<<" codes.\n";

    You can use the cin object to get keyboard input like this:
    Code:
    int number;
    cin>>number; /* Get an integer from the user and put it into number */
    When programming in C++ or in any other language, you can use loops to repeat a task (such as getting keyboard input) until a condition you specify is met. You put the condition inside a part of the loop statement (like, while(number>=0)).
    Code:
            int number=0;
    	while(number<5)
    	{
            number++;
    	}
    Alternatively, you can check for the condition in the midst of the loop and break out of it.
    Code:
    if(number==5)
    break;
    If the number of codes the user is to enter into the program isn't known at the time you compile (create) the program, you should ask the user to enter something that could not possibly be a code when she is done entering the actual codes (like, say, a negative number or a character such as 'q').
    Code:
    	while(true)
    	{
    	cout<<"\n>";
    	cin>>input;
    	if(input<=0)
    		break;
    	codes.push_back(input);/* Put the newest keyboard input into codes*/
    	}
    Last edited by Boxknife; 04-23-2010 at 11:32 AM.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    In C++, there are smart arrays called vectors you can use that can automatically resize themselves to hold as much input as you need. You can make a vector of integers like this:

    Code:
    vector<int> codes;
    codes.push_back(5); /* Put 5 into vector codes */
    codes.push_back(4); /* Put 4 into vector codes */
    Vectors automatically keep track of how many elements it is storing. You can find out just how many with the size() member function:
    Code:
    cout<<"There are "<<codes.size()<<" codes.\n";

    You can use the cin object to get keyboard input like this:
    Code:
    int number;
    cin>>number;
    When programming in C++ or in any other language, you can use loops to repeat a task (such as getting keyboard input) until a condition you specify is met. You put the condition inside a part of the loop statement (like, while(number>=0)).
    Code:
            int number=0;
    	while(number<5)
    	{
            number++;
    	}
    Alternatively, you can check for the condition in the midst of the loop and break out of it.
    Code:
    if(number==5)
    break;
    If the number of codes the user is to enter into the program isn't known at the time you compile (create) the program, you should ask the user to enter something that could not possibly be a code when she is done entering the actual codes (like, say, a negative number or a character such as 'q').
    Code:
    	while(true)
    	{
    	cout<<"\n>";
    	cin>>input;
    	if(input<=0)
    		break;
    	codes.push_back(input);/* Put the newest keyboard input into codes*/
    	}
    Last edited by Boxknife; 04-23-2010 at 03:49 PM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    Solution is very simple.
    Just try this logic and report me the errors if occured.


    LOGIC:
    Take a while or do while loop and asks from the user each time whether he wants to enter more codes or not, if yes then enter code and get its amt. and quantity print run this loop until user says no

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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