Thread: c++ parrallel array program

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    c++ parrallel array program

    Im trying to write a program from this programming book i just bought and am having some difficulty. Actually im totally lost i need some direction or any help would be usefull.

    Here is what the program calls for:

    Write a program that lets a maker of chips and salsa keep track of their sales for 5 different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel 5-element arrays: an array of strings that holds the 5 salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the time the name array is created. The program should prompt the user to enter the number of jars sold for each type. Once the sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products. Input Validation: Do not accept negative values for number of jars sold.
    Heres what i have so far:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
      string salsa[] = {"mild", "medium", "sweet", "hot", "zesty"};
      int jars[5];
      int total_sales = 0;
      int index, i;
    
      cout << "Enter the number of jars sold for the mild salsa: ";
      cin>>jars[0];
      cout << "Enter the number of jars sold for the medium salsa: ";
      cin>>jars[1];
      cout << "Enter the number of jars sold for the sweet salsa: ";
      cin>>jars[2];
      cout << "Enter the number of jars sold for the hot salsa: ";
      cin>>jars[3];
      cout << "Enter the number of jars sold for the zesty salsa: ";
      cin>>jars[4];
    
    
      return 0;
    } // end main
    Last edited by corbinc; 11-07-2009 at 02:35 PM. Reason: Updated code

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The salsa names should be stored using an initialization list at the time the name array is created.
    This means that when you declare the "salsa" array, you also have to initialize it, i.e. give it values. Your doing
    Code:
      string salsa[5];
    but you need to do
    Code:
      string salsa[5] = { "values", "of", "array", "go", "here"};
    To initialize the values of the array at the same time that you declare the array.

    The program should prompt the user to enter the number of jars sold for each type.
    Ok, so you have prompted the user. Now you have to get the values from the user. Use "cin" (console input) to do this, do a quick search somewhere to find a simple example of how to use it. As the requirements state, the user cant enter a negative number, so you have to check if the number they entered is negative and if so, ask for a different number (until it is not negative).

    Also, I just noticed you hardcoded the value at the prompt ("mild"). What you should really do is ("for") loop through the "salsa" array, from 0 to less than (the size of the array), in this case 5, and each iteration of the loop, ask the user for the number of jars for the salsa at that index. Any decent tutorial on "for" loops would give an example of this concept.

    Also, to do the report, find the max, min, you will use "for" loops. But get everything working before that, then focus on the last part.
    Last edited by nadroj; 11-07-2009 at 01:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-28-2009, 03:15 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM