简体   繁体   中英

C++: Flawed console output on using a conditional statement

I'm a newbie to C++ (and to programming in general). 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...

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() . 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). However, unless I removed that 'if' statement from my code, nothing I else I tried worked.

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 .

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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