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

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.

Edited 5 Years Ago by WaltP: n/a

// 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?

This question has already been answered. Start a new discussion instead.