// erase.cpp - Script 4.1

// We need the iostream file in order
// to use cout and cin.
#include <iostream>

// Start the main function.
int main() {
    
    // Declare a variable for the user input.
    // Single character to store yes (Y) or (N)
    char answer;
    
    // Prompt the user and take the imput.
    std::cout << "Would you like to erase your hard drive? [Y/N] ";
    std::cin >> answer;
    
    // Create a blank line in the output.
    std::cout << "\n";
    
    // Print a messege based upon the value.
    switch (answer) {
           case 'Y':
           case 'y':
           std::cout << "I'm thinking that's not such a good idea. \n\n";
           break;
           case 'N':
           case 'n':
           std::cout << "That's probably for the best.\n\n";
           break;
           default:
           std::cout << "Can't answer a simple Yes or No question, eh?\n\n";
           break;
           }
           
           // Wait for the user to press Enter or Return.
           std::cout << "Press Enter or Return to continue.\n";
           std::cin.get();
           
           // Return the value 0 to indicate no problems.
           return 0;
           
           } // End of the main() function.

I am a total newbie when it comes to programming and am following an example out of a book I'm reading. Basically the program is compileing and running but is not doing what I want it to do and what it says it's supposed to do in the book!
It prints the line "Would you like to erase your hard drive? [Y/N]_ but when I type in 'Y' or 'n' etc and press enter it's not printing the statements in the switch conditional and the program is just ending. A little help would be much appreciated. Thanks in advance.

Dani AI

Generated

As hinted, nothing mystical is wrong with Dev‑C++ or g++; it was the leftover newline in the input buffer. Using formatted extraction (the operator >> you used for a char) consumes the character you typed but leaves the trailing Enter (a '\n') in the stream. The next unformatted read that accepts whitespace will immediately consume that '\n' and make it look like the program skipped waiting for input.

std::cin.ignore(100, '\n') tells the stream to discard up to 100 characters or stop when it sees a newline. That works, but the fixed 100 is arbitrary — a very long line could outrun it. A more robust discard is to ignore up to the stream maximum, e.g.:

#include <limits>
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

(See the docs for ignore and numeric_limits for details: std::basic_istream::ignore and std::numeric_limits.)

Better patterns to avoid these buffer puzzles:

  • Read the whole line and examine its first character. This is simple and predictable:
    std::string line;
    std::getline(std::cin, line);
    char answer = line.empty() ? '\0' : line[0];
  • Or, if you must mix formatted and unformatted I/O, flush the rest of the line with the ignore(max, '\n') trick before the next unformatted read. You can also use the std::ws manipulator to skip leading whitespace when appropriate (std::ws).

For a simple yes/no prompt, the getline approach combined with validating the first non‑whitespace character is the easiest to get right and avoids platform/input-length surprises.

Recommended Answers

All 4 Replies

I don't think there's anything wrong with the code. It compiles and runs fine using GCC. Which compiler are you using?

As a side note, you could probably scale the number of comments back a bit - you can have too many :o)

I'm using the Dev-C++ IDE and the compiler that comes with that I think it's g++ but I'm not sure to be honest.

What keys -- all of them -- are you hitting when you answer the question? Isn't it n[ENTER] ?
If so, your code reads the n , correct? What happens to the [ENTER] ? Well, the next input gets it and does what it's supposed to do. Exit.

You need to clear out the input buffer after reading the first character. There's a sticky at the top of the forum to explain how.

// erase.cpp - Script 4.1

// We need the iostream file in order
// to use cout and cin.
#include <iostream>

// Start the main function.
int main() {
    
    // Declare a variable for the user input.
    // Single character to store yes (Y) or (N)
    char answer;
    
    // Prompt the user and take the imput.
    std::cout << "Would you like to erase your hard drive? [Y/N] ";
    std::cin >> answer;
    
    // Create a blank line in the output.
    std::cout << "\n";
    
    // Print a messege based upon the value.
    switch (answer) {
           case 'Y':
           case 'y':
           std::cout << "I'm thinking that's not such a good idea. \n\n";
           break;
           case 'N':
           case 'n':
           std::cout << "That's probably for the best.\n\n";
           break;
           default:
           std::cout << "Can't answer a simple Yes or No question, eh?\n\n";
           break;
           }
           
           // Discard any extraneous input.
           std::cin.ignore(100, '\n');
           
           // Wait for the user to press Enter or Return.
           std::cout << "Press Enter or Return to continue.\n";
           std::cin.get();
           
           // Return the value 0 to indicate no problems.
           return 0;
           
           } // End of the main() function.

I read on in my book and worked out how to get it working. I'm not really sure what std::cin.ignore(100, '\n'); means though. I just presume the '\n' acounts for the first typed Enter or Return as opposed to just ending the program or returning 0 like it was originally?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.