开发者

string program for ice cream shop (Edited again) [closed]

开发者 https://www.devze.com 2022-12-11 13:42 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

With the assistance of others, I have redone the code from scratch du开发者_如何转开发e 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

include

include

include

include

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);
    }


switch doesn't work with strings.

You need to use the operator == to select the right choice like so:

string count = // ... something
if (count == "choc") {

}
else if (count == "van") {

}
else if (count == "str") {

} // .. etc

A few other things: make sure you spell string with a consistent case, all lower case and no upper case. String is something different than string.

Make sure you surround strings with double quotes "" and not single quotes ''. single quotes are for single characters like 'a' or 'b'. double quotes are for multiple characters strings like "abc" and "hello"

Having the word count as both the function name and an argument name is probably a bad idea and will not compile because the same name means two different things.

You can't return multiple values from a function. writing something like return a,b,c; doesn't mean what you probably want it to mean. the comma (,) operator allows several expressions to be evaluated in the same statement and the result is the value of the last expression so writing return 1,2,3; is exactly the same as writing return 3;


C++ cannot switch on a string. Replace your switch(count) {...} with if/else if statements. Additionally the proper format for a string is "string", not 'string' (single quotes designate a single character, like: 'a'). Also, ensure that you always use the correct casing for string objects (string as opposed to String, like you have as your return values)

Other than that, it would be helpful to see the compiler errors you are getting.


Another thing I noticed in your source: you will have to omit the semicolons (-cola?) at the end of the following lines:

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 : ";

This is just a single huge expression. The same applies to

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;

Also: the endl and the "\n" are redundant. You will see the choices being separated by empty lines.


I haven't looked at the whole thing, but this isn't going to do what you want:

if (flavor = 'STOP' || 'stop')

I think you need:

if (flavor.compare("STOP") == 0 || flavor.compare("stop") == 0)


Let's go down the problems I see.

String count (string& flavor, string& count, string& chocolate, string& vanilla, string& strawberry, string& mint, string& rocky_road, string& mocha);

You're using String here, I'm sure you meant std::string or just string.

Inside the count function (SO is truncating the code when pasted), you're ending the line with a semicolon after endl yet trying to continue the stream output. I think you meant

Next:

if (flavor = 'STOP' || 'stop')

I think you meant to use the operator== instead of operator=, which is assignment not comparison. Also, there are no junctions in c++, so you will have to write that out as:

if (flavor == 'STOP' || flavor == 'stop')

Next:

switch (count) { case 'choc' :

Two problems here. First, you can only use plain-old-data (pod) in switch statements. Using std::string in a switch will not automatically call operator==; you will have to use if/else statements. Also, string literals are double quoted whereas character literals are single quoted.

Next:

chocCount + count;

This isn't really a statement. I'm sure you meant chocCount += count;

Next:

if (flavor = chocolate) chocCount + count;

Again, you want to use == and chocCount += count;.

Most of these problems are repeated. You should fix each of these problems everywhere they exist. There may be other problems, but I was basically compiling that in my head.

I didn't read through all of it to find semantic problems, but your count function is clearly not returning a count (at least what I currently see posted). You are returning a String, which I assume you meant string.

That's all this human compiler is going to solve for 1 homework assignment. I could recommend you go read a good C++ tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消