I am probably well on the wrong path with this one which is why I can't get the program to compile in the slightest.

The assignment instructions are :

The Frozen Tongue Ice Cream Shop sells six flavors of ice cream: chocolate, vanilla, strawberry, mint, rocky road, and
mocha. The shop wants a program that tracks how many scoops of each flavor are sold each day. The console input for each transaction will be the name of a flavor followed by how many scoops of that flavor were sold in that transaction:

example of initial output:

Enter the flavor of the scoops sold (STOP to exit): vanilla
Enter how many scoops were sold: 3

If the user enters a flavor that doesn't match any of the known flavors, the program should display an error message. The user should be allowed to enter as many transactions as desired, and each flavor will likely appear in more than one transaction. Once the user enters "STOP", the program should terminate, and the program should display how many scoops of each flavor were tallied in the program run:

DAILY SCOOP REPORT:
Chocolate: 58
Vanilla: 65
Strawberry: 49
Mint: 23
Rocky Road: 37
Mocha: 31


One way to make sure that the scoops are added to the proper flavor counter is to have a series of decision statements comparing the user's input to each flavor. Another approach would be to use parallel arrays, with an array of strings storing the name of the each flavor and an array of integers storing the count for each flavor at the same index as the
flavor name in the other array.

Write your code so that it handles the flavor name with a space and so that the user's input will be matched to the correct flavor no matter what type of capitalization is used.

Position the output so that the number of scoops for each flavor is right-aligned (assume that the demand for flavors can vary, so the number of scoops for any flavor may be between one and three digits):

Example output:

DAILY SCOOP REPORT:
Chocolate: 92
Vanilla: 103
Strawberry: 89
Mint: 8
Rocky Road: 76
Mocha: 64

I tried to use the switch statement but not sure if that is correct. Also, it wouldn't allow the return of the flavors in the count function due to them being changed int when applying the update to the count, so I tried to use the atoi() but don't really know how to use it and the tutorials I read about it don't fit my situation close enough for me to manipulate it to make it work.

I hope that I have described things enough. Here is my code thus far.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

String count (string& flavor, string& count, string& chocolate, string& vanilla, string& strawberry, string& mint, string& rocky_road, string& mocha);
int main()
{
	string stop, flavor, count = 0, chocolate, vanilla, strawberry, mint, rocky_road, mocha;
	
	cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
	cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl;
		 << "Flavors avaliable:\n"<<endl;
		 << "chocolate\n"<<endl;
		 << "valnilla\n"<<endl;
		 << "strawberry\n"<<endl;
		 << "mint\n"<<endl;
		 << "rocky road\n"<<endl;
		 << "mocha\n"<<endl;
		 << "Enter flavor : ";
	cin.getline >> flavor;

	cout <<"Now enter number of scoops sold\n"<<endl;
	cin >> count;

	if (flavor = 'STOP' || 'stop')
	{
		cout<<"You have chosen to exit the program. Thank you."<<endl;
		exit(1);   
	}

	else count ( flavor, count, chocolate, vanilla, strawberry, mint, rocky_road, mocha);
	
	cout << "\nDAILY SCOOP REPORT: \n"<<endl;
		 << "chocolate :"<<chocolate<<"\n"<<endl;
		 << "vanilla :"<<vanilla<<"\n"<<endl;
		 << "strawberry :"<<strawberry<<"\n"<<endl;
		 << "mint :"<<mint<<"\n"<<endl;
		 << "rocky road :"<<rocky_road<<"\n"<<endl;
		 << "mocha :"<<mocha<<"\n"<<endl;
	
	return 0;
}

String count (string& flavor, string& count, string& chocolate, string& vanilla, string& strawberry, string& mint, string& rocky_road, string& mocha)
{
	string choc, van, str, mt, rrd, mch;

	chocolate = choc;
	vanilla = van;
	strawberry = str;
	mint = mt;
	rocky_road = rrd;
	mocha = mch;

	switch (count) 
	{
	case 'choc' :
		chocolate = count;
		break;
	case 'van':
		vanilla = count;
		break;
	case 'str':
		strawberry = count;
		break;
	case 'mt':
		mint = count;
		break;
	case 'rrd':
		rocky_road = count;
		break;
	case 'mch':
		mocha = count;
		break;
	default:
		cout<<"You have entered an unavaliable flavor. The program will now end."<<endl;
		exit(1);
	}

	cout << atoi(count ())

		return chocolate, vanilla, strawberry, mint, rocky_road, mocha;
}

Thanks in advance for any assistance.

Recommended Answers

All 15 Replies

line 9: string stop, flavor, count = 0

count is a string, so why are you setting it to 0 like it is an integer? I suspect you want this: int count = 0; line 26: >> if (flavor = 'STOP' || 'stop')
strings have to be enlosed in double quotes "STOP". Also the if statement is constructed wrong. You want this: if( flagor == "STOP" || flagor == "stop") Further reading that program shows that most of those other variables also need to be integers -- they are supposed to contain a count of how many scoops of that flavor were ordered. You don't do counts with strings.

Well, there are a few changes that need to be made to your current code. First, you need to make count an int value not a string. You cant set strings equal to an integer value.
The switch statement should be 'switching' on the flavor not the count. Inside each statement you should increment that specific flavor. You will need to include separate counters for each flavor.
Line 81: this is not a recursive function and therefore you cannot call the function within the text of the function.
Finally, you cant return more than one value.

Now I'd like to offer my personal take on the problem. I'd use a while loop like the following:

while(flavor != "STOP" ||  flavor != "stop")

Then use a switch statement with separate incrementers for each flavor. When input is terminated, output the results of each counter. Well, there's my two cents. Let me know if you need any clarification on that.
EDIT - sorry Ancient Dragon, I didnt realize you had already posted.

I have changed the code around quite a bit. Is this way of doing things progress?

I had to create the shortened names for the flavors because the switch statement was giving error that they were too long. I re-read the tutorial on converting the string to an int and I hope that I was able to understand it. The only error I get is that the compiler says that the "+" operator doesn't do anything when it's adding count to its respective flavor counter.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
	string stop, flavor, chocolate, choc, vanilla, van, strawberry, str, mint, mt, rocky_road, rrd, mocha, mc;
	int chocCount = 0, vanCount = 0, strCount = 0, mtCount = 0, rrdCount = 0, mochaCount = 0, flavorI, count = 0;
	
	cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
	cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl;
	cout << "Flavors avaliable:\n"<<endl;
	cout << "chocolate\n"<<endl;
	cout << "valnilla\n"<<endl;
	cout << "strawberry\n"<<endl;
	cout << "mint\n"<<endl;
	cout << "rocky road\n"<<endl;
	cout << "mocha\n"<<endl;
	cout << "Enter flavor : ";
	getline (cin, flavor) >> flavor;
	
	choc = chocolate;
	van = vanilla;
	str = strawberry;
	mt = mint;
	rrd = rocky_road;
	mc = mocha;

	cout <<"Now enter number of scoops sold\n"<<endl;
	cin >> count;
	
	
	flavorI = atoi("flavor");


	if ( flavor != "STOP" || flavor != "stop")
	{
		switch (flavor) 
	{
	case 'choc' :
		chocCount + count;
		break;
	case 'van':
		vanCount + count;
		break;
	case 'str':
		strCount + count;
		break;
	case 'mt':
		mtCount + count;
		break;
	case 'rrd':
		rrdCount + count;
		break;
	case 'mc':
		mochaCount + count;
		break;
	default:
		cout<<"You have entered an unavaliable flavor. The program will now end."<<endl;
		exit(1);
	}
	}

	else 
	{
		cout<<"You have chosen to exit the program. Thank you."<<endl;
		exit(1);   
	}
	
	cout << "\nDAILY SCOOP REPORT: \n"<<endl;
	cout << "chocolate :"<<chocCount<<"\n"<<endl;
	cout << "vanilla :"<<vanCount<<"\n"<<endl;
	cout << "strawberry :"<<strCount<<"\n"<<endl;
	cout << "mint :"<<mtCount<<"\n"<<endl;
	cout << "rocky road :"<<rrdCount<<"\n"<<endl;
	cout << "mocha :"<<mochaCount<<"\n"<<endl;
	
	return 0;
}

The instructor assigned this project and set it's due date on the same day that we have a big test, so I'm having to both study and this at the same time as other classes and my job. I'm sorry if I miss something that I shouldn't. Thanks again for the assistance.

Also, I have tried the tolower() function but the compiler will not accept it. Here is how I tried to code it:

flavor = tolower(flavor)

What did I miss?

When you are incrementing the counts just use flavorName++;
By the way your teacher is a jerk.

line 21: >> getline (cin, flavor) >> flavor;
Remove the ">> flavor" at the end of that line.

You can not do switch statements on strings! switch only accepts integers. You will have to replace that switch statement with a series of if statements. Another way to do it is to change that menu so that you enter a number instead of a string

cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
	cout << "Please enter the flavor number of icecream sold or 'STOP' to exit.\n"<<endl;
	cout << "1.  Flavors avaliable:\n";
	cout << "2.  chocolate\n";
	cout << "3.  valnilla\n";
	cout << "4.  strawberry\n";
	cout << "5.  mint\n";
	cout << "6.  rocky road\n";
	cout << "7.  mocha\n";

Also note that you do not need both "\n" and endl because endl also causes '\n' -- leave it if you want your menu displayed with a blank line between menu items..

With the above menu you can enter a number instead of a string. Then change the data type of flavor to int.

Also, I have tried the tolower() function but the compiler will not accept it. Here is how I tried to code it:

flavor = tolower(flavor)

What did I miss?

tolower() only works on a single character, not the entire string.

I have changed the switch to an if/else-if and now the only error I get is it says that the + operator has no effect. I'm trying to use it to add the count to the current number of scoops for its respective counter. As in the user could enter

chocolate 4
vanilla 3
mint 14
chocolate 84
mocha 8

I want it to be able to add the count respectively if the user enters the same flavor twice and they will also add more than 1 scoop so ++ will only increment 1.

Here is the code now

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string stop, flavor, chocolate, vanilla, strawberry, mint, rocky_road, mocha;
	int count, chocCount, vanCount, strCount, mtCount, rrdCount, mochaCount;
	
	cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
	cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl;
	cout << "Flavors avaliable: "<<endl;
	cout << "chocolate "<<endl;
	cout << "valnilla "<<endl;
	cout << "strawberry "<<endl;
	cout << "mint "<<endl;
	cout << "rocky road "<<endl;
	cout << "mocha "<<endl;
	cout << "\nEnter flavor : ";
	getline (cin, flavor);
	
	//flavor = tolower(flavor);

	cout <<"Now enter number of scoops sold : ";
	cin >> count;

	if ( flavor != "STOP" || flavor != "stop")
	{
	if (flavor = chocolate)
		chocCount + count;
	else if (flavor = vanilla)
		vanCount + count;
	else if (flavor = strawberry)
		strCount + count;
	else if (flavor = mint)
		mtCount + count;
	else if (flavor = rocky_road)
		rrdCount + count;
	else if (flavor = mocha)
		mochaCount + count;
	else 
		cout<<"You have entered an unavaliable flavor. The program will now end."<<endl;
		exit(1);
	}

	else 
	{
		cout<<"You have chosen to end the program. Thank you."<<endl; 
	}
	
	cout << "\nDAILY SCOOP REPORT: "<<endl;
	cout << "chocolate :"<<chocCount<<endl;
	cout << "vanilla :"<<vanCount<<endl;
	cout << "strawberry :"<<strCount<<endl;
	cout << "mint :"<<mtCount<<endl;
	cout << "rocky road :"<<rrdCount<<endl;
	cout << "mocha :"<<mochaCount<<endl;
	
	return 0;
}

I'm looking up a way to loop through a string to check each character of a string to see if it's lower case or not and if not it changes it to lower. Like a tolower() loop. But I'm still trying to figure out how to get it to work.

I found another way to convert to lower case and wanted to see if you thought it would work. I have put it into my code as thus far I haven't found anything else that seems to work.

#include <algorithm>
.... //stuff happening then when user enters flavor,
std::string lowerCase = flavor;
std::transform(lowerCase.begin(), lowerCase.end(),
	lowerCase.begin(), ::tolower);

I have made changes to the various computing codes and the else-if statements and now the program compiles. It runs and ask the user for the flavor then scoops sold. Then when it ask again, it doesn't read in the getline for flavor and thus the else-if falls through to the error message. Any idea of how I can change the loop around to make it work? Also, on another site I have this posted, the user said that the program isn't keeping track of the counters for the flavors. Any idea as to what he's talking about?

In the if/else statements you have to use for example

chocCount = chocCount + count;

This will add the current value of chocCount to count. You need to repeat this for each flavor. I think this is what you are looking for.

I found another way to convert to lower case and wanted to see if you thought it would work. I have put it into my code as thus far I haven't found anything else that seems to work.

#include <algorithm>
.... //stuff happening then when user enters flavor,
std::string lowerCase = flavor;
std::transform(lowerCase.begin(), lowerCase.end(),
	lowerCase.begin(), ::tolower);

Why didn't you just convert flavor to lower case instead of creating yet another variable?

I changed the transform statement but it still doesn't work. Didn't think about that. The program runs but doesn't add the flavor counts if the user puts same flavor 2+ times.

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
	string stop, flavor, chocolate, vanilla, strawberry, mint, rocky_road, mocha;
	int count, chocCount=0, vanCount=0, strCount=0, mtCount=0, rrdCount=0, mochaCount=0;

	cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
	cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl;
	cout << "Flavors avaliable: "<<endl;
	cout << "chocolate "<<endl;
	cout << "valnilla "<<endl;
	cout << "strawberry "<<endl;
	cout << "mint "<<endl;
	cout << "rocky_road "<<endl;
	cout << "mocha "<<endl;
	cout << "\nEnter flavor : ";
	getline (cin, flavor);

	transform(flavor.begin(), flavor.end(),
		flavor.begin(), ::tolower);

	cout <<"Now enter number of scoops sold : ";
	cin >> count;

	while ( flavor != "STOP" || flavor != "stop")
	{
		cout << "\nEnter flavor : ";
		getline (cin, flavor);

		std::string lowerCase = flavor;
		std::transform(lowerCase.begin(), lowerCase.end(),
			lowerCase.begin(), ::tolower);

		//cout <<"Now enter number of scoops sold : ";
		//cin >> count;

		while ( flavor != "STOP" || flavor != "stop")
		{
			if (flavor == "chocolate")
				chocCount += count;
			else if (flavor == "vanilla")
				vanCount += count;
			else if (flavor == "strawberry")
				strCount += count;
			else if (flavor == "mint")
				mtCount += count;
			else if (flavor == "rocky_road")
				rrdCount += count;
			else if (flavor == "mocha")
				mochaCount += count;
			else 
				cout<<"You have entered an unavaliable flavor. The program will now end."<<endl;
			exit(1);
		}}

	if ( flavor == "STOP" || flavor == "stop") 
	{
		cout<<"You have chosen to end the program. Thank you."<<endl; 
	}

	cout << "\nDAILY SCOOP REPORT: "<<endl;
	cout << "chocolate :"<<chocCount<<endl;
	cout << "vanilla :"<<vanCount<<endl;
	cout << "strawberry :"<<strCount<<endl;
	cout << "mint :"<<mtCount<<endl;
	cout << "rocky road :"<<rrdCount<<endl;
	cout << "mocha :"<<mochaCount<<endl;

	return 0;
}

With the assistance of others, I have redone the code from scratch due to them pointing out numerous errors and things that wouldn't work. Thus I have changed the code massively.

I have the program working other than two formatting settings that I can't figure out how to get to work.

I need to only print "DAILY SCOOP REPORT" once at the top of the output, but I've moved it around but due to the way the arrays are set up I don't know where to put it.

Here is my code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <iomanip>

using namespace std;

int main()
{
	string flavor_input, Capitalize;
	string flavors[] = { "Chocolate", "Vanilla", "Strawberry", "Mint", "Rocky Road", "Mocha" }; 
	int scoop_count [6] = { 0, 0, 0, 0, 0, 0 }, scoops = 0, j, k;

	bool valid_option; 

	cout << "Welcome to Frozen Tongue Ice Cream Shop\n"<<endl;
	cout << "Flavors avaliable: "<<endl;
	cout << "Chocolate "<<endl;
	cout << "Valnilla "<<endl;
	cout << "Strawberry "<<endl;
	cout << "Mint "<<endl;
	cout << "Rocky Road "<<endl;
	cout << "Mocha \n"<<endl;

	while(true) { 

		cout << "Please enter the flavor of icecream sold or 'STOP' to exit.\n"<<endl;
		getline (cin, flavor_input); // getline causes rocky road to be accepted with a space between the words. 


		string::iterator it( flavor_input.begin());  //converting the first letter of input to uppercase if not already.

		if (it != flavor_input.end())
			flavor_input[0] = toupper((unsigned char)flavor_input[0]);

		while(++it != flavor_input.end())
		{
			*it = tolower((unsigned char)*it);
		}


		if (flavor_input == "STOP" || flavor_input == "Stop")
			break; 

		valid_option = false; 

		for(int i=0;i<6;i++)  //Checks to see if input matches those of possible inputs.
			if(!flavor_input.compare(flavors[i]))
			{
				valid_option = true;
				break;
			}

			if(!valid_option)
			{
				cout << "Invalid Flavor. Try again.\n\n"; 
				flavor_input.clear();
				continue; 
			}

			for(int i=0;i<6;i++)
			{
				if(!flavor_input.compare(flavors[i])) 
				{
					cout << "Enter how many scoops were sold: ";
					cin >> scoops;
					cin.ignore();
					scoop_count[i] += scoops; 
					scoops = 0; 
					cout << '\n';
					break;
				}
			}
	}


	for(int i=0;i<6;i++)
	{
		if(!scoop_count[i])
			continue;
		else
		{
			cout << "\nDAILY SCOOP REPORT: "<<endl;   
			cout << setiosflags( ios::left )
				<< setw( 11 )  << flavors[i]
			<< resetiosflags( ios::left )
				<< setw( 4 ) << scoop_count[i] << endl;

		}
	}


	cin.get();
	return 0;
}

Thanks again for all of the assistance. It is greatly appreciated.

Thanks to all the assistance and pointing me in the direction of what to study, I have the program completed other than one last part.

I figured out that why it wasn't working when I moved the "DAILY SCOOP REPORT" line around. I had renamed the file and when I compiled it, it was outputing the "last working configuration" kinda deal if that makes sense. So I created a new project (the .cpp file has to have a certain name for submission) and put the code in it. Now the line is printed only once.

In the code block below, I have it where it lowers casing for all other letters other than the first or so it seems to be doing. The reason I have the case coding the way I do is that the instructions want the flavor report to print out with first letter of each word cap and lower after that. I am going to look into how to cap the 2nd "R" in Rocky Road, but other than the ignore white-space I don't really know how. Do I need to parse the line?

Anyone to point me in the right direction would be appreciated.

I tried but it gives error that in the first if statement "syntax error : identifier 'flavor_input'".

//converting the first letter of input to uppercase if not already.
string::iterator it( flavor_input.begin());  

		if flavor_input = "rocky road"
			(it != flavor_input.end())
			flavor_input[6] = toupper((unsigned char)flavor_input[6]);

		if (it != flavor_input.end())
			flavor_input[0] = toupper((unsigned char)flavor_input[0]);

		while(++it != flavor_input.end())
		{
			*it = tolower((unsigned char)*it);
		}
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.