冰淇淋店的字符串程序(再次编辑)

发布于 2024-08-12 00:18:29 字数 3696 浏览 8 评论 0原文

在其他人的帮助下,我从头开始重新编写了代码,因为他们指出了许多错误和不起作用的事情。因此我对代码进行了大量更改。

除了两个格式设置之外,我的程序正在运行,我不知道如何开始工作。

我只需要在输出顶部打印一次“DAILY SCOOP REPORT”,但我已将其移动,但由于数组的设置方式,我不知道将其放在哪里。

这是我的代码:

#include <iostream>

include

include

include

include

using

namespace std;

int main() { 字符串风味输入,大写; string Flavors[] = { "巧克力", "香草", "草莓", "薄荷", "洛基路", "摩卡" }; 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;

再次

感谢您的所有帮助。非常感谢。



感谢所有的帮助并为我指明了学习的方向,我完成了该计划,除了最后一部分。

当我移动“每日独家报道”行时,我明白了为什么它不起作用。我已经重命名了该文件,当我编译它时,它输出了“最后的工作配置”,如果这有意义的话。所以我创建了一个新项目(.cpp文件必须有一定的名称才能提交)并将代码放入其中。现在该行仅打印一次。

在下面的代码块中,我在其中降低了除第一个字母之外的所有其他字母的大小写,或者似乎正在这样做。我按照我的方式进行案例编码的原因是,说明希望风味报告打印出每个单词大写的第一个字母,然后是小写字母。我将研究如何限制 Rocky Road 中的第二个“R”,但除了忽略空白之外,我真的不知道如何操作。我需要解析该行吗?

任何人指出我正确的方向将不胜感激。

我尝试过,但在第一个 if 语句中出现错误“语法错误:标识符'flavor_input'”。

//converting the first letter of input to uppercase if not already.

字符串::迭代器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);
    }

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何以心动 2024-08-19 00:18:29

switch 不适用于字符串。

您需要使用运算符 == 来选择正确的选择,如下所示:

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

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

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

} // .. etc

其他一些事项:确保您拼写的 string 大小写一致,全部小写且没有大写。 Stringstring 不同。

确保用双引号 "" 引起字符串,而不是单引号 ''。单引号用于单个字符,例如 'a''b'。双引号用于多个字符串,例如 "abc""hello"

将单词 count 作为函数名称和参数名称可能是一个坏主意,并且不会编译,因为相同的名称意味着两个不同的东西。

您不能从一个函数返回多个值。编写类似 return a,b,c; 的内容并不意味着您可能想要它的含义。逗号 (,) 运算符允许在同一语句中计算多个表达式,结果是最后一个表达式的值,因此编写 return 1,2,3; 是与编写 return 3; 完全相同

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;

水水月牙 2024-08-19 00:18:29

C++ 无法打开字符串。将 switch(count) {...} 替换为 if/else if 语句。此外,字符串的正确格式是“string”,而不是“string”(单引号指定单个字符,例如:“a”)。另外,请确保您始终对字符串对象使用正确的大小写(string 而不是 String,就像您的返回值一样)

除此之外,这也会有所帮助查看您遇到的编译器错误。

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.

猫九 2024-08-19 00:18:29

我在您的源代码中注意到的另一件事:您必须在以下几行末尾省略分号(-cola?):

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

这只是一个巨大的表达式。这同样适用于

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:endl"\n" 是多余的。您将看到选项由空行分隔。

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.

青萝楚歌 2024-08-19 00:18:29

我没有看过整个事情,但这不会做你想做的事:

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

我认为你需要:

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

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)
浪荡不羁 2024-08-19 00:18:29

让我们来看看我看到的问题。

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

您在这里使用的是 String,我确定您的意思是 std::string 或只是 string

在 count 函数内部(SO 在粘贴时截断代码),您在 endl 之后用分号结束该行,但尝试继续流输出。我认为你的意思是

下一步:

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

我认为你的意思是使用 operator== 而不是 operator= ,这是赋值而不是比较。另外,C++ 中没有连接,因此您必须将其写为:

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

Next:

switch (count) { case 'choc' :

这里有两个问题。首先,您只能在 switch 语句中使用 plain-old-data (pod)。在 switch 中使用 std::string 不会自动调用operator==;你必须使用 if/else 语句。此外,字符串文字是双引号的,而字符文字是单引号的。

下一篇:

chocCount + count;

这并不是一个真正的声明。我确信您的意思是 chocCount += count;

接下来:

if (flavor = chocolate) chocCount + count;

再次,您想使用 ==chocCount += count;

大多数这些问题都会重复出现。您应该解决所有存在的问题。可能还有其他问题,但我基本上是在脑子里编译的。

我没有通读所有内容来查找语义问题,但您的计数函数显然没有返回计数(至少是我目前看到的)。您正在返回一个字符串,我认为您的意思是字符串。

这就是这个人类编译器要解决的 1 项家庭作业的全部内容。我建议您去阅读优秀的 C++ 教程

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文