执行问题流
Flow of execution questions
我已经困难了一段时间。我一直对我的if/else if语句遇到问题。在我的程序中,我正在尝试循环浏览电话市场的客户列表,以阅读并将呼叫信息记录到另一个文档中。我只有两个我认为相对较小的问题,但我无法用Google fu弄清楚。
这是我的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char stat, answ;
float rate;
string fname, lname, phone, address;
ifstream potentials("C:\Users\Brandon\Desktop\C++Projects\Lab6\potentials.txt");
ofstream results("C:\Users\Brandon\Desktop\C++Projects\lab6\results.txt");
if (!potentials) {
cerr << "Unable to open file";
exit(1);
}
while (!potentials.eof())
{
potentials >> stat >> fname >> lname >> phone;
if (stat == 'X')
{
cout << "customer is preferredn";
rate = .079;
}
if (stat != 'X')
{
cout << "Customer is not preferredn";
rate = .129;
}
cout << fname << " " << lname << " " << phone << " " << rate << endl;
cout << "Is this customer interested in this card? Y or N" << endl;
cin >> answ;
if (answ == 'Y')
{
cout << "Thank the customer and ask for their current address.n";
cin >> address;
cout << "Thank the customer for accepting the offer, to expect a card in the mail soon, and happy charging!n";
results << fname << " " << lname << " " << stat << " " << address << " r " << endl;
}
if (answ == 'N')
{
if (stat == 'X')
{
cout << "Tempt them with the cash back rewards and to call 18003838383 for a better offer.n";
results << fname << " " << lname << " " << answ << " " << address << " r" << endl;
}
else if (stat != 'X');
{
cout << "Tell them thank you anyhow and to have a nice day.n";
results << fname << " " << lname << " " << answ << " " <<
address << " r" << endl;
}
}
}
}
问题编号1:
对于地址部分,我一直遇到错误,如果我包括一个普通地址,例如123 W Main St。这种类型的地址将奇怪地完成循环。像[so] [1]。我不知道,也不明白为什么会发生这种情况。如果我只放一个数字,它将编译并执行正常。
问题编号2:
如果客户继续致电并拒绝卡,则if语句和其他IF语句将执行:
customer is preferred
Joe SCholz 437-4798 0.079
Is this customer interested in this card? Y or N
Y
Thank the customer and ask for their current address.
123 W Main St
Thank the customer for accepting the offer, to expect a card in the mail soon, and happy charging!
Customer is not preferredTim Wade 768-7658 0.129
Is this customer interested in this card? Y or N
customer is preferredSara JObs 326-7857 0.079
Is this customer interested in this card? Y or N
Customer is not preferredJaynce Bee 354-8678 0.0129
Is this customer interested in this card? Y or N
我也不理解这部分,因为IF语句应执行。如果客户在文件中的状态上没有X,则应自动转到否则if语句。相反,它同时执行IF和其他IF语句,我似乎无法弄清楚为什么会发生这种情况。
这些是让我退缩的仅有的两件事,我无法弄清楚它们。
if语句之后的半隆。另外,if/else逻辑没有仔细排除并处理所有可能的答案选择。最后,我使用了std :: getline()进行std :: cin的阅读,当您期望从cin中读取多个输入时,它会更好地工作。
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
char stat, answ;
float rate;
std::string fname, lname, phone, address, partial_address;
std::ifstream potentials("C:\Users\Brandon\Desktop\C++Projects\Lab6\potentials.txt");
std::ofstream results("C:\Users\Brandon\Desktop\C++Projects\lab6\results.txt");
if (!potentials) {
std::cerr << "Unable to open file ";
return 1;
}
while ( true)
{
// This 'if' statement removes the while(!potentials.eof()) which created an extra read error
// There are other ways to handle this, but this was a quick solution. The read itself will return
// a false value, but as we are trying to read 4 variables, this was one way of doing it.
if(!(potentials >> stat &&
potentials >> fname &&
potentials >> lname &&
potentials >> phone)) {
std::cout << "End of file." << std::endl;
return -1;
}
// if you get into the habit of putting the constant on the left side of an equality,
// you won't accidentally confuse '=' with '=='
if ( 'X' == stat)
{
std::cout << "customer is preferredn";
rate = .079;
} else if('Y' == stat) { // I usually only check for the value you are looking for, but this is OK, if you create the catch-all else at the end.
std::cout << "Customer is not preferredn";
rate = .129;
} else {
std::cout << "File corrupted. First word in each line must be 'Y' or 'X'" << std::endl;
return -1; // exit the program as the file is corrupted
}
std::cout << fname << " " << lname << " " << phone << " " << rate << std::endl;
std::cout << "Is this customer interested in this card? Y or N" << std::endl;
std::cin >> answ;
if ('Y' == answ || 'y' == answ) // Just has to check for lower case as well You can chuck that if you like
{
std::cin.ignore(); // cin has a tendency to skip the next input based on a prior "return" or "enter". Gobble the new line
std::cout << "Thank the customer and ask for their current address.n";
std::getline(std::cin, address);
std::cout << "Thank the customer for accepting the offer, to expect a card in the mail soon, and happy charging!n";
results << fname << " " << lname << " " << stat << " " << address << " r " << std::endl;
} else if('N' == answ || 'n' == answ) {
// AT this point, we already know that stat is either 'Y' or 'N', so we don't check explicitly on the else
if ('X' == stat)
{
std::cout << "Tempt them with the cash back rewards and to call 18003838383 for a better offer.n";
results << fname << " " << lname << " " << answ << " " << address << " r" << std::endl;
} else {
std::cout << "Tell them thank you anyhow and to have a nice day.n";
results << fname << " " << lname << " " << answ << " " <<
address << " r" << std::endl;
}
} else { // catch all else. Not my preferred way to do this, but follows your design.
std::cout << "Invalid response. Response must be 'Y' or 'X'" << std::endl;
return -1; // exit the program as the response was not corrupted
}
}
}
相关文章:
- 如何解决 Ninja c++ 构建和执行问题
- 无法在问题解决方案中执行输出逻辑
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 在 C++ Consol 中的数组中执行用户命令时出现问题
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- 执行用 C++ 编写的.exe(使用 mingw 编译器)时出现问题
- Win7 C++ - 从以用户身份登录的服务启动可执行文件的问题
- MFC执行线程问题
- 代码问题将字符串转换为图表执行
- 将我的主输出库与测试可执行文件链接时出现问题
- 在C++中编译/执行 GDAL 时出现问题
- 函数返回本地变量,尽管变量不超出范围,没有编译器问题,并且代码执行
- R6010 执行后出错.if 语句在 for 循环中.[作业][中止已解决,发现新问题]
- 定义可以执行对象方法和独立函数的函数时出现问题
- 从Windows任务栏执行时ifstream问题
- 生成文件问题(体系结构x86_64的未定义符号:"_main",引用自:主可执行文件的隐式入口/启动)
- 这段代码出了什么问题?它在第二个 cin 可以执行之前终止
- 执行问题流
- 将静态和动态库链接到同一个可执行文件会导致哪些问题
- 关于如何在函数执行后使用和获取返回值的问题