执行问题流

Flow of execution questions

本文关键字:问题 执行      更新时间:2023-10-16

我已经困难了一段时间。我一直对我的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
        }
    }
}