【发布时间】:2020-05-25 18:49:19
【问题描述】:
下面我在程序中设置一个y/n 问题,我知道我需要一个名为i 的标识符,以便cin 行在if 中工作。但是,我不确定将我的标识符放在哪里。
#include <iostream>
#include <string>
using std::string, std::cin, std::cout, std::endl;
int main() {
const int MaxNum = 3;
int simcard;
string familyone[MaxNum] = {
"Name One",
"Name Two",
"Name Three"
};
int famnumone[MaxNum];
int choice;
string familytwo[MaxNum] = {
"Name One",
"Name Two",
"Name Three"
};
int famnumtwo[MaxNum];
string familythree[MaxNum] = {
"Name One",
"Name Two",
"Name Three"
};
int famnumthree[MaxNum];
string phonetype[MaxNum] = {
"Iphone",
"Android",
"Random Phone"
};
string familysim[MaxNum] = {
"Family one Sim",
"Family two Sim",
"Family Three Sim"
};
string simtype[MaxNum] = {
"Iphone SIM",
"Android SIM",
"Random Phone SIM"
};
//Here we have information for each family members name to later identify who each phone belongs too
cout << "SIM card matching tool\n";
cout << "Enter Family One Names ==>";
for (int i = 0; i < MaxNum; i++) {
getline(cin, familyone[i]);
}
do {
cout << "would you like to continue to the sim/y, or would you like to add another family/n" << endl;
cin >> choice;
choice = tolower(choice);
} while (choice != 'n' && choice != 'y');
if (choice == 'n') {
cin >> familytwo[i];
if (choice == 'y')
cin >> simtype[i];
}
//add y/n below or possibly add a more complex anwser "if you would like to continue with the SIM type SIM or if you would like to add another family type family
cout << "Would you like to continue to the SIM/y, or would you like to add another family/n";
}
【问题讨论】:
-
什么是 [I] 标识符?你的程序有错误吗?它会给出错误的结果吗?
-
修正缩进。它现在相当混乱且难以阅读。你也错过了
#includes。 -
对您的问题更准确一点?
-
在 do-while 循环之后,大括号的位置错误。如果选择是'n'`,那么检查选择是否是'y'是没有意义的。
-
@ZackHinson 是什么让你写了
cin >> familytwo[i];这行?familytwo是一个由 3 个strings 组成的数组。你想用你从用户那里得到的任何输入覆盖哪一个?
标签: c++