#include <iostream> using namespace std; int main() { //Declare data types int NumChild; //var const int BaseFee = 150; //constant //Welcome + Instructions for users cout << "Welcome to IOU Secondary School" << endl <<endl<< "How many children are attending the school this year? "<<endl; //Nested IF-ELSE (error checking) if (cin >> NumChild)/ { //do not accept any non-integer values if (cin.peek() == EOF|| cin.peek() == '\n' ) { } else { cout <<endl<< "Error: enter a whole number."<<endl<< "The total amount below represents the initial number entered e.g. if 5.7 is entered, the fee will be calculated according to 5 children" << endl;//error message for float entry cin.ignore(100, '\n'); //read and discard up to 100 characters from the input buffer, or until a newline is read cin.clear();//resets any error flags in the cin stream } } else { // Edit: You should probably also clear the steam cin.ignore(100, '\n');//read and discard up to 100 characters from the input buffer, or until a newline is read cin.clear(); //resets any error flags in the cin stream cout << "Error: Restart the program and enter a number"; } //nested IF statement (Calculation of fees) if(NumChild == 1) //1 child attending { cout<<endl<<"The total cost of your child's fees are $"<< BaseFee+50 << endl; } if(NumChild == 2) //2 children attending { cout<<endl<<"The total cost of your children's fees are $"<< (BaseFee*2)+85 << endl; } if(NumChild == 3)//3 children attending { cout<<endl<<"The total cost of your children's fees are $"<< (BaseFee*3)+110 << endl; } if(NumChild > 3) //More than children attending { cout<<endl<<"The total cost of your children's fees are $"<< (BaseFee*NumChild)+150 << endl; } } 

Can someone explain what the cin.peek statement and what it is doing in the above code? How does EOF|| cin.peek() == '\n') affect this.

When I enter a float value, the first else value activates but a result still shows. How do I adapt the code to make the total fee not appearing?

2 Answers

the 'peek' function on input streams (in your case cin) retrieves the next character from the stream without actually consuming it. That means that you can "preview" the next character in the input, and on the next call to any consuming operation (overloaded operator >> or cin.read) will read that character and consume it.

The condition eof() || cin.peek == '\n' checks if either the end of the input file stream is reached or if the user provided a newline character.

Regarding your other question: In case of an invalid input (e.g. a floating-point value), you do not exit the function. Thus, you continue execution and hence print the value. Simply exit from the function using return 1;.

peek looks at a character without removing it from the stream. That code is checking to make sure that the next character in the stream is the end of the file or the end of a line without extracting the character from the stream and damaging a subsequent valid input.

It cannot accept a floating point number because cin >> NumChild will read an int and will stop as soon as it hits the decimal point. Example: input "3.14". numChild will contain 3. The ".14" remains in the stream, so peek will read '.', not the end of the file or a newline, and prints the error message.

It then continues with the rest of the program because nothing told it to stop. You need a loop around the input to continue requesting more input until a valid input is provided.

A simple example:

bool goodInput = false; while (!goodInput) { your code here if (cin.peek() == EOF|| cin.peek() == '\n' ) { goodInput = true; // change made here } your code here } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy