在其他人的帮助下,我从头开始重做代码,因为他们指出了许多错误和无效的事情.因此我大量改变了代码.
我有程序工作,而不是两个格式设置,我无法弄清楚如何开始工作.
我只需要在输出的顶部打印一次"DAILY SCOOP REPORT",但我已经移动了它,但是由于数组的设置方式,我不知道放在哪里.
这是我的代码:
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
}
再次感谢所有的帮助.非常感谢.
感谢所有的帮助,并指导我学习的方向,我完成的课程不仅仅是最后一部分.
我发现当我移动"每日SCOOP报告"线时,为什么它不起作用.我已经重命名了文件,当我编译它时,它输出了"最后工作配置"有点交易,如果这是有道理的.所以我创建了一个新项目(.cpp文件必须有一个提交的特定名称)并将代码放入其中.现在该行仅打印一次.
在下面的代码块中,我有它,它降低了除第一个之外的所有其他字母的套管,它似乎正在做.我按照我的方式编写案例的原因是说明书要求使用每个单词cap的首字母打印出flavor报告,然后降低.我将研究如何限制Rocky Road中的第二个"R",但除了忽略的空白区域之外我还不知道如何.我需要解析这条线吗?
任何指出我正确方向的人都将不胜感激.
我试过但它在第一个if语句中出现错误"语法错误:标识符'flavor_input'".
//converting the first letter of input to uppercase if not already.
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
switch 不适用于字符串.
您需要使用运算符==来选择正确的选择,如下所示:
string count = // ... something
if (count == "choc") {
}
else if (count == "van") {
}
else if (count == "str") {
} // .. etc
Run Code Online (Sandbox Code Playgroud)
其他一些事情:确保拼写string一致的案例,全部小写,没有大写.String是不同的东西string.
确保用双引号""而不是单引号括起字符串''.单引号适用于单个字符,如'a'或'b'.双引号用于多个字符串,如"abc"和"hello"
将单词count作为函数名称和参数名称可能是一个坏主意,并且不会编译,因为相同的名称意味着两个不同的东西.
您无法从函数返回多个值.写一些类似的东西return a,b,c;并不意味着你可能想要的意思.comma(,)运算符允许在同一语句中计算多个表达式,结果是最后一个表达式的值,因此写入return 1,2,3;与写入完全相同return 3;
| 归档时间: |
|
| 查看次数: |
3020 次 |
| 最近记录: |