简体   繁体   English

C ++:使用条件语句时控制台输出错误

[英]C++: Flawed console output on using a conditional statement

I'm a newbie to C++ (and to programming in general). 我是C ++(以及一般编程)的新手。 I wrote a program that reads and writes staff contact details. 我编写了一个程序,该程序读取和写入员工联系方式。 This code works well: 这段代码运作良好:

// appropriate headers...

int main() 
{
    char trigger{};
    int options = 0;
    bool testing{};


    fileIps Inptbox;            // for entering new data
    char ext_title[20]; char ext_intercomNum[4]; char ext_dept[20];

    printf("%s", "Enter Officer's Title:\n");
    gets_s(ext_title, 20);
    printf("%s", "Enter Officer's Intercom Number:\n");
    gets_s(ext_intercomNum, 4);
    printf("%s", "Enter Officer's Department:\n");
    gets_s(ext_dept, 20);

    Inptbox.rcv_values(ext_title, ext_intercomNum, ext_dept);
    Inptbox.create_string();
    testing = Inptbox.validate();
    if (testing == true)
        return -1;
    else if (testing == false)
        Inptbox.write_string();

    // more code below...

My question is regarding the console output. 我的问题是关于控制台输出。 I had tried to introduce conditional statements to enable selecting a read or write mode. 我试图引入条件语句以启用选择模式。 The above code is for writing to file. 上面的代码用于写入文件。 There are more lines of code below for reading from file and they, too, worked okay. 下面还有更多行代码可以读取文件,它们也可以正常工作。

My challenge is that once I introduce a conditional statement for the above code... 我的挑战是,一旦我为上述代码引入了条件语句...

    printf("%s", "Enter 1 to WRITE DATA or 2 to READ DATA\n");
    cin >> options;
    if (options == 1)
    {
        fileIps Inptbox;           // for entering new data
        //... rest of code...
    }

    // more code below...

...the output on the console is flawed, as the prompt for the first entry is displayed but is totally skipped, forcing the user to enter ' Officer's Intercom Number ' first. ...控制台上的输出有缺陷,因为显示了第一个输入的提示,但被完全跳过了,迫使用户首先输入“ 官员的对讲机号码 ”。 The third prompt worked well. 第三个提示效果很好。

To elaborate further, when I used cin to assign the value 1 to options (ie applying the condition), the console would immediately print... 详细说明,当我使用cin将值1分配给options (即应用条件)时,控制台将立即打印...

Enter Officer's Title:

Enter Officer's Intercom Number:

...making it impossible for me to fill in the first entry (ie ' Title '). ...使我无法填写第一个条目(即“ 标题 ”)。

I struggled with this and tried several things to fix it. 我为此苦苦挣扎,并尝试了几项修复方法。 I used fgets() and even tried it with gets() . 我使用了fgets() ,甚至尝试使用gets() I revisited my classes, yet nothing worked. 我重新上课,但没有任何效果。 I read widely on things like buffering, studied questions on this site and looked through various resources on cstdio as well as ios_base and its derived classes (which was good because I learned a bunch of other things). 我广泛阅读了诸如缓冲之类的内容,在此站点上研究了问题,并浏览了cstdio上的各种资源以及ios_base及其派生类(这很好,因为我学到了很多其他东西)。 However, unless I removed that 'if' statement from my code, nothing I else I tried worked. 但是,除非我从代码中删除了“ if”语句,否则我尝试的其他任何方法都不会起作用。

So, my question is: "How can this kind of behaviour be explained and how best could I implement my code to enable me toggle between read and write mode?" 因此,我的问题是:“如何解释这种行为?如何最好地实现我的代码以使我能够在读写模式之间切换?”

I am working with MS Visual Studio 2015 . 我正在使用MS Visual Studio 2015

Using the formatted extraction operator '>>' has its problems. 使用格式化的提取运算符'>>'存在问题。 In this case, it reads all values that can convert to an integer. 在这种情况下,它将读取所有可以转换为整数的值。 However, you must give an enter to signal that you're ready. 但是,您必须输入Enter表示您已准备好。 The >> operator does not process that newline. >>运算符不处理该换行符。 When the next time input is read, it sees the previously given newline character. 下次读取输入时,它将看到先前给定的换行符。 Hence the 'Enter Officer's title' input is immediately set to a newline and continues. 因此,“输入官员的头衔”输入立即设置为换行符并继续。 Try using something like: 尝试使用类似:

std::string line;
getline(cin, line);

And test the string or convert it. 并测试字符串或将其转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM