执行问题的流程

时间:2019-02-25 00:46:27

标签: c++

我已经很难解决这个问题了。我的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 preferred\n";
            rate = .079;
        }

        if (stat != 'X')
        {
            cout << "Customer is not preferred\n";
            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语句和else 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,则应该自动转到else if语句。相反,它同时执行if和else if语句,我似乎无法弄清楚为什么会发生这种情况。

这是让我退缩的仅有的两件事,我都无法弄清。

1 个答案:

答案 0 :(得分:0)

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 preferred\n";
            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 preferred\n";
            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
        }
    }
}