gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:02 24 4
gpt4 key购买 nike

<分区>

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

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

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

这是我的代码:

#include <iostream>

包括

包括

包括

包括

包括

使用命名空间标准;

主要内容(){ 字符串 flavor_input,大写; 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;

再次感谢所有的帮助。非常感谢。



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

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

在下面的代码块中,除了第一个左右的字母外,我把所有其他字母的大小写都降低了。我之所以用这种方式对案例进行编码,是因为说明希望 flavor 报告打印出每个单词大写的第一个字母,然后再降低。我将研究如何在 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);
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com