Void summary wont work

Last Edited By Krjb Donovan
Last Updated: Mar 12, 2014 02:46 PM GMT

Question

I was hoping you could look at this and tell me why my void summary is not working I cant figure it out, Thank You


/* program name: David Early

       -------------------what the user is asked for or enters----------------
       INPUTS: 
               
       -----------------what results from the user's input--------------------
       OUTPUTS:   

Chips and Salsa- name

Once this 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.

Extra (4): use for-loops to cycle through the arrays. i.e. Whenever you are hardcoding an index/subscript (example: scores[0], scores[1], etc...) replace the hardcoded coding with a for-loop so that the subscript is done using the variable that controls the for-loop. (Loads of examples of this in the textbook and on the web.)

Extra (1): Initialize the starting values in the declaration statement of the array salsas instead of using assignment statements to assign the values one salsas element at a time.

  • /
  1. include <cstdlib>
  2. include <iostream>
  3. include <string>
  4. include <iomanip>
// welcome screen mains
  1. define who " David P. Early "
  2. define when " 07 July 2010 "
  3. define assiegnment " program 6 "

// the following is the quote on the flash screen

  1. define line1P "\"Why do you say these things to me,"
  2. define line2P "\ when you know I will kill you for it\""
  3. define line3P " ~General Zod (superman 2) "

// the following will be the discription

  1. define line1W " This program keeps track of sales for five "
  2. define line2W " different types of salsa sold by a company. "


using namespace std;

void splashscreen (); void welcome (); void getSalsaSales(string[], int[]); void summary(string[], int[], int, int ); int highest(int); int lowest(int); int validate(); const int SIZE = 5;

int main(int argc, char *argv[]) {

   string salsa[SIZE]; 
   int jars[SIZE];     
   
   splashscreen ();  
   welcome ();
   
   salsa[0] = "mild";
   salsa[1] = "medium";
   salsa[2] = "sweet";
   salsa[3] = "hot";
   salsa[4] = "zesty";
   getSalsaSales(salsa, jars); 
   summary(salsa, jars, highest(jars), lowest(jars)); 
   
   system("PAUSE");
   return EXIT_SUCCESS;

}

int validate(string salsa) {

   int jars;
   
   cin >> jars;
   
   while(jars < 0 )
   {
        system("CLS");
        cout << "\n\n\n\n\n\n\n\n\n\n"
             << "\n\t\t\t" << jars << " is an invalid number!\n"
             << "\n\t\t\t please enter a positive numbers!\n"
             << "\n\t\t\t Jars of " << salsa << " salsa : ";
        cin  >> jars;
        system("CLS");
   }
   return jars;

} int lowest(int jars[]) {

 int lowest = jars[0];
       
       if (jars[1] < jars[0])
          {
            lowest = jars[1]; 
          }
          else if (jars[2] < jars[1])
             {
                lowest = jars[2];
             }
         else if (jars[3] < jars[2])
             {
                lowest = jars[3];
             }
         else if (jars[4] < jars[3])
             {
                 lowest = jars[4];
             }
      
       return lowest; 

} int highest(int jars[]) {

  int highest = jars[0];
       
       if (jars[1] > jars[0])
          {
            highest = jars[1]; 
          }
          else if (jars[2] > jars[1])
             {
                highest = jars[2];
             }
         else if (jars[3] > jars[2])
             {
                highest = jars[3];
             }
         else if (jars[4] > jars[3])
             {
                 highest = jars[4];
             }
      
       return highest; 

} void summary(string salsa[SIZE], int jars[], int highest , int lowest ) {

    cout << "\n\n\n\n\n\n\n\n"
         << "\t\t Salsa Type \t\t # of jars sold \n"
         << "\t\t________________________________\n"
         << "\t\t" << salsa[0] << " : "  << jars[0] <<"\n"
         << "\t\t" << salsa[1] << " : "  << jars[1] <<"\n"
         << "\t\t" << salsa[2] << " : "  << jars[2] <<"\n"
         << "\t\t" << salsa[3] << " : "  << jars[3] <<"\n"
         << "\t\t" << salsa[4] << " : "  << jars[4] <<"\n"
         << "\n\t\t\t Highest : "  << highest << "\n"
         << "\n\t\t\t Lowest  : "  << lowest << "\n"; 
    

} void getSalsaSales(string salsa[], int jars[]) {

    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\n\n\t\t\t      Please enter jars of salsa \n\n";
    cout << "\n\n\n\n\n\n\n\n\n\n";
    system("PAUSE");
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\t\t\t Jars of " << salsa[0] <<" salsa : ";
    jars[0] = validate(salsa[0]);     
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\t\t\t Jars of " << salsa[1] <<" salsa : ";
    jars[1] = validate(salsa[1]);
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\t\t\t Jars of " << salsa[2] <<" salsa : ";
    jars[2] = validate(salsa[2]);
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\t\t\t Jars of " << salsa[3] <<" salsa : ";
    jars[3] = validate(salsa[3]);
    system("CLS");
    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\t\t\t Jars of " << salsa[4] <<" salsa : ";
    jars[4] = validate(salsa[4]);
    
                   

} void splashscreen ()//function definition {

   cout << "              _____________________________________________  \n " 
        << "           //:::::::::::::::::::::::::::::::::::::::::::::\\\\ \n"  
        << "          //:::_______:::::::::________::::::::::_____:::::::\\\\ \n" 
        << "        //:::_/   _-\"\":::_--\"\"\"        \"\"\"--_::::\\_  ):::::::::\\\\ \n"  
        << "       //:::/    /:::::_\"                    \"-_:::\\/:::::|^\\:::\\\\ \n "
        << "     //:::/   /~::::::I__                      \\:::::::::|  \\:::\\\\ \n "  
        << "     \\\\:::\\   (::::::::::\"\"\"\"---___________     \"--------\"  /:::// \n " 
        << "      \\\\:::\\  |::::::::::::::::::::::::::::\"\"\"\"==____      /:::// \n "
        << "       \\\\:::\"\\/::::::::::::::::::::::::::::::::::::::\\   /~:::// \n "
        << "         \\\\:::::::::::::::::::::::::::::::::::::::::::)/~:::// \n "
        << "           \\\\::::\\\"\"\"\"\"\"------_____:::::::::::::::::::::::// \n "
        << "             \\\\:::\"\\               \"\"\"\"\"-----_____::::::// \n "
        << "               \\\\:::\"\\    __----__                ):::// \n "
        << "                 \\\\:::\"\\/~::::::::~\\_         __/~::// \n "
        << "                   \\\\::::::::::::::::\"\"----\"\"\"::::// \n "
        << "                     \\\\:::::::::::::::::::::::::// \n "
        << "                       \\\\:::\\^\"\"--._.--\"\"^/:::// \n "
        << "                         \\\\::\"\\         /\"::// \n "
        << "                           \\\\::\"\\     /\"::// \n " 
        << "                             \\\\::\"\\_/\":://  \n "
        << "                               \\\\:::::// " << line1P << "  \n "
        << "                                 \\\\_// " << line2P << " \n "
        << "                                   \"  \t\t " << line3P <<" \n " ;
   system("PAUSE");
   system ("cls");

} void welcome ()//function definition {

   cout << " \n\n\n\n\n "
        << " \n\t/--------------------------------------------------------------\\ "   
   	 << " \n\t|                      About This Program                      |   "
   	 << " \n\t+--------------------------------------------------------------+   "
  	     << " \n\t|                                                              |   "
   	 << " \n\t|             Programmer's Name:" who                        " |   "
   	 << " \n\t|                          Date:" when                       " |   "  
   	 << " \n\t|               Name of program:" assiegnment                " |   "   
   	 << " \n\t|    /-------------------- Description --------------------\\   |  "  
  	     << " \n\t|    |" line1W                                             "|   |   "   
   	 << " \n\t|    |" line2W                                             "|   |   "  
   	 << " \n\t|    \\-----------------------------------------------------/   |  "  
   	 << " \n\t\\--------------------------------------------------------------/  "
        << " \n\n\n\n\n\n\n ";  
   system("PAUSE");
   system("cls");

}

Answer

Hello David.

It's nice to hear from you again. The problem with the program is that the lowest and highest functions just don't work. You need a different algorithm. To see why, look at the lowest function. Imagine jars[4] to be the lowest, but then if jars[1] < jars[0], then jars[4] is never even looked at. To find the lowest, you need to set lowest to the first jar, as you have done. Then you need to compare the rest of the jars against lowest, not against each other. Each time you find a jar lower than lowest, the lowest takes on the value of that jar.

A similar change has to be done for the highest function. Give that a try and let me know how you do.

Also, correct your version of these lines:

line 36

  1. define line2P "\"when you know I will kill you for it\"" // ZM changed \space, to \" at start of line

lines 50 and 51 int highest(int[]); // ZM change int to int[] int lowest(int[]); // ZM change int to int[]

Advertisement

©2024 eLuminary LLC. All rights reserved.