4

在其他人的帮助下,我从头开始重新编写代码,因为他们指出了许多错误和无法正常工作的事情。因此,我已经大量更改了代码。

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

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

这是我的代码:

#include <iostream>

包括

包括

包括

包括

包括

使用命名空间标准;

int main() { 字符串风味输入,大写;字符串风味[] = {“巧克力”、“香草”、“草莓”、“薄荷”、“洛基路”、“摩卡”};int scoop_count [6] = { 0, 0, 0, 0, 0, 0 }, 勺子 = 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.

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

5 回答 5

5

switch不适用于字符串。

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

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

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

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

} // .. etc

其他一些事情:确保你拼写string的大小写一致,全部小写,没有大写。String不同于string.

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

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

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

于 2009-11-17T22:44:39.880 回答
1

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

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

于 2009-11-17T22:46:02.637 回答
1

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

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

我认为你需要:

if (flavor.compare("STOP") == 0 || flavor.compare("stop") == 0)
于 2009-11-17T23:10:59.740 回答
1

我在您的来源中注意到的另一件事:您必须在以下行的末尾省略分号(-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;

另外: theendl和 the"\n"是多余的。您将看到选项被空行分隔。

于 2009-11-17T23:19:54.270 回答
0

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

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')

下一个:

switch (count) { case 'choc' :

这里有两个问题。首先,您只能在 switch 语句中使用普通旧数据(pod)。在 switch 中使用 std::string 不会自动调用 operator==; 您将不得不使用 if/else 语句。此外,字符串文字是双引号,而字符文字是单引号。

下一个:

chocCount + count;

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

下一个:

if (flavor = chocolate) chocCount + count;

同样,您想使用==and chocCount += count;

大多数这些问题都会重复出现。您应该在它们存在的任何地方解决这些问题。可能还有其他问题,但我基本上是在脑海中编译它。

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

这就是人类编译器将要解决的 1 个家庭作业。我可以推荐你去阅读一个好的 C++ 教程

于 2009-11-19T06:48:05.187 回答