Hi,

I have just completed this small cafeteria program. I do have an issue I hope you can advise me with. Here is my question.

1) On the // Display the contents of the array output I wish to have the numbers as well as the asterisks representation of that number displayed. For example.

1 = *, 5 = *****, etc.

I already have a for loop there, but it represents the "Rating" portion, and I need one to represent the "Frequency" in "*".

I was thinking along the lines of:

for(response = 0; response > SIZE; response++)
{
cout <<"*";
}
cout endl;
return;

however, it doesn't work or I can't code it just right.
_________________________________________________________________________________________

#include <iostream>
#include <iomanip>

using namespace std;
void main()
{
	const int SIZE = 5;
	double response[SIZE] = {0};
	double responseTotal = 0,
	       responseAverage = 0,
	       responseHigh = 0,
	       responseLow = 0;

	system("color 1F");

	    cout <<"\n\t\t***********************************"
		 <<"\n\t\t*     Cafeteria Food Survey       *"
		 <<"\n\t\t***********************************"
		 <<endl
		 <<"\n\n" << "  " <<"Enter a rating between 1 and 10:  1=AWFUL 10=EXCELLENT"
		 <<endl
		 <<endl;

	for(int index = 0; index < SIZE; index++)

	{
	cout<<"\n" << "  " << "Rate Cafeteria Food (scale 1 to 10): ";
	cin >> response[index];

		while(response[index] <= 0 || response[index] > 10)
{
	cout <<"\n" << "  " <<"\a\a\aError: Specify a number within a valid range. ";
		cin >> response[index];
}
		}


// Dispaly the contents of the array
	system("color 2F");
	cout <<"\n" <<" "<<" Rating:" << "   "<<" Frequency:\n\n";
for(int index = 0; index < SIZE; index++)

	{
	
	cout <<"    " << response[index]; cout <<"\t\t" <<'*' << endl; 

	
	}
// Display Ratings Average
for(int index = 0; index < SIZE; index++)
responseTotal += response[index];

responseAverage = responseTotal/SIZE;
cout <<"\n" << "  " << "The Ratings average is: " << responseAverage;


// Display the highest rating
responseHigh = response[0];
for(int index = 0; index < SIZE; index++)
 if (response[index] > responseHigh)
 {
	 responseHigh = response[index];

 }
 cout <<"\n" <<"  " <<"The Highest rating is: " << responseHigh;

 // Dispaly the lowest rating

responseLow = response[0];
for(int index = 0; index < SIZE; index++)
 if (response[index] < responseLow)
 {
	 responseLow = response[index];

 }
 cout <<"\n" << "  " <<"The Lowest rating is: " << responseLow;


cout << endl << endl;

	return;
}

I believe this will fix an number of issues with the code:

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

void setColor(int color);

int main()
{
    const int SIZE = 5;
    double response[SIZE] = {0};
    double responseTotal = 0,
           responseAverage = 0,
           responseHigh = 0,
           responseLow = 0;

    setColor(0x1F);

    cout << endl << setw(16) << right << " " << "***********************************"
         << endl << setw(16) << right << " " << "*     Cafeteria Food Survey       *"
         << endl << setw(16) << right << " " << "***********************************"
         << endl << endl << endl
         << "  " << "Enter a rating between 1 and 10:  1=AWFUL 10=EXCELLENT"
         << endl << endl;

    for(int index = 0; index < SIZE; index++)

    {
        cout<< endl << "  " << "Rate Cafeteria Food (scale 1 to 10): ";
        cin >> response[index];

        while(response[index] <= 0 || response[index] > 10)
        {
            cout << endl << "  " << "\a\a\aError: Specify a number within a valid range. ";
            cin >> response[index];
        }
    }


    // Display the contents of the array
    setColor(0x2F);
    cout <<endl << setw(7) << right << "Rating" << setw(12) << right << "Frequency" << endl << endl;
    for(int index = 0; index < SIZE; index++)
    {
        cout << setw(4) << right << response[index]
             << setw(6) << ' '
             << setfill('*') << setw(response[index]) << left << '*'
             << setfill(' ') << endl;
    }

    // Display Ratings Average
    for(int index = 0; index < SIZE; index++)
    {
        responseTotal += response[index];
    }

    responseAverage = responseTotal/SIZE;
    cout << endl << "  " << "The Ratings average is: " << responseAverage;


    // Display the highest rating
    responseHigh = response[0];
    for(int index = 0; index < SIZE; index++)
    {
        if (response[index] > responseHigh)
        {
            responseHigh = response[index];
        }
    }
    cout << endl << "  " << "The Highest rating is: " << responseHigh;

// Dispaly the lowest rating

    responseLow = response[0];
    for(int index = 0; index < SIZE; index++)
    {
        if (response[index] < responseLow)
        {
            responseLow = response[index];
        }
    }

    cout << endl << "  " << "The Lowest rating is: " << responseLow
         << endl << endl;

    return 0;
}

void setColor(int color)
{
    stringstream cmd;
    cmd << "color " << hex << color;
    system(cmd.str().c_str());
}

I'm not sure what you mean by 'frequency', but right now it should fill the correct number of asterisks for the rating.

commented: And yet another free code post, with no explanation what the fixes are to boot! Courtesy of the DaniWeb Free Homework Service. -4

Thank you very much! I just received contact regarding this application. I was wondering why it didn't work correctly. Now I am told that it should reflect the number of times each rating is given and that is what is meant by frequency.

for example if a rating of 5 is given twice then under the ratings 5 , under the frequency I should have ** two asterisks indicating that a rating of 5 was entered twice. I truly appreciate all the help, and if you have any suggestion I greatly appreciate them. I will continue to work with this, but your help has made me look at other things as well.

Taino

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.