array problem

find high and low in array naming it to string

Page 1 of 1

3 Replies - 764 Views - Last Post: 08 February 2010 - 09:42 AM Rate Topic: -----

#1 Guest_Rick*


Reputation:

array problem

Posted 08 February 2010 - 09:09 AM

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int const types = 5;					//how many types of salsa
	int jarsSold [types];					//array to hold sales of salsa
	char salsaNames[types][7] = {"Mild",	//array to hold salsa names
							 "Medium",
							 "Sweet",
							 "Hot",
							 "Zesty" };
	char answer = 'y';						//declared for loop around program
	char highest[7];
	char lowest[7];
	
	//Program Description to user
	cout << "This program will ask for the total amount of jars sold for the month.\n";
	cout << "Then it will compute the total sales and the names of the highest\n";
	cout << "selling and the lowest selling products.\n\n";

	cout << "Do you want to enter the amount of jars sold? ";
	cin >> answer;
	while (answer == 'y' || answer == 'Y')
	{
		int total = 0;
		 
		
		//Input the salsa sales totals
		for (int count = 0; count < types; count++)
		{
			cout << "Enter the total jars sold for type " << salsaNames[count] << ".";
			cin >> jarsSold[count];
			
			if (jarsSold[count] < 0)
			{
				cout << "ERROR:The lowest number you can enter is 0.\n";
				cin >> jarsSold[count];
			}
		}

		//Display the type with their sales total.
		for ( int count = 0; count < types; count++)
		{
			cout << salsaNames[count] << " has " << jarsSold[count] << " jars sold for the"
				 << " month.\n";
		}

		//calculate the total amount of jars sold
		for (int count =0; count < types; count++)
		{
			total += jarsSold[count];
		}
		//Display the total
		cout << "The total amount of jars sold for the month is " << total <<".\n";

		//Find the product with the highest amount of jars sold
		highest = salsaNames[0];
		for (int count = 1; count < types; count++)
		{
			if (salsaNames[count] > highest)
				highest = salsaNames[count];
		}

		//Find the product with the lowest amount of jars sold
		lowest = salsaNames[0];
		for (int count = 1; count < types; count++)
		{
			if (salsaNames[count] > lowest)
				lowest = salsaNames[count];
		}

		//Display the product with the lowest and the highest amount of jars sold
		cout << "The product who had the highest amount of jars sold is " << highest << ".\n\n";
		cout << "The product who had the lowest amount of jars sold is " << lowest << ".\n\n";

		cout << "Would you like to enter another months totals?\n";
		cout << "Y = Yes and N = No\n";
		cin >> answer;

		//Programmers Name
		cout << "\nWritten By:Rick Klinger\n";


	}
		return 0;
	
}
//Find the product with the highest amount of jars sold
highest = salsaNames[0];
for (int count = 1; count < types; count++)
{
if (salsaNames[count] > highest)
highest = salsaNames[count];
}

//Find the product with the lowest amount of jars sold
lowest = salsaNames[0];
for (int count = 1; count < types; count++)
{
if (salsaNames[count] > lowest)
lowest = salsaNames[count];
}

This is the part the is coming up with the error.Says that left hand error must '='1value.Tying to make highest and lowest = a character string.somethings different than a value.(Let me know if i posted it right)

Is This A Good Question/Topic? 0

Replies To: array problem

#2 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,438
  • Joined: 22-August 09

Re: array problem

Posted 08 February 2010 - 09:20 AM

ok, couple of points on this code. You have five for loops, three of which correctly start with 'for ( int count = 0', and two loops tat start with 'for ( int count = 1'. Remember in the C/C++ language, arrays start at index 0.

In answer to your question, the code line

 highest = salsaNames[count];



is incorrect. Highest is defined as an array of seven characters. You will need to copy the salsaNames to it by using strcpy, or some similar routine.

Why not use the string class which will permit you to write the kind of code you have written?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_rick*


Reputation:

Re: array problem

Posted 08 February 2010 - 09:31 AM

i set them to one because i set highest and lowest to to subscript [0] then the loop will start at the one after that. What do you mean by string class.
Was This Post Helpful? 0

#4 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,438
  • Joined: 22-August 09

Re: array problem

Posted 08 February 2010 - 09:42 AM

The string class is part of the std namespace and can be used if you '#include <string>. It provides some neat facilities such as assignment, concatenation etc etc.

You can for example say

string a = "abcdef";
string b = a;



You do not have to worry about the size of the array needed to contain the value being assigned, string deallocation and so on.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1