#include <iostream> // prototype for a function that takes no parameters and returns no value void printSomething(); // prototype for a function that takes no parameters // and returns a value of type integer int getAValue(); // prototype for a function that takes two parameters (an int and a float) // and returns a value of type double double multiply(int x, float y); // a main routine calling each of the three functions listed above int main() { int a = 1; float b = 2.0; double c; printSomething(); a = getAValue(); c = multiply(a, b); return 0; } // ===== implementations of the three functions ===== // prototype for a function that takes no parameters and returns no value void printSomething() { std::cout << "Something!" << std::endl; } // prototype for a function that takes no parameters // and returns a value of type integer int getAValue() { // getting the user to type in a value, and return whatever it is int v; std::cout << "Please enter an integer value" << std::endl; std::cin >> v; return v; } // prototype for a function that takes two parameters (an int and a float) // and returns a value of type double double multiply(int x, float y) { // compute and return the product of x and y double result = x * y; return result; } |
// an if by itself if (x == 3) { // do this if x is 3, otherwise move on to whatever is next std::cout << "x is 3!" << std::endl; } |
// presumed maximum length of a typed line of user input const int LINELEN = 256; // variables to hold the numbers th user will enter, // here we have initialized it to a negative value float x = -1; // prompt the user to enter a non-negative number std::cout << "Please enter a non-negative number" << std::endl; // read the number into x, use cin.fail() to check if it succeeded std::cin >> x; if (std::cin.fail()) { std::cout << "Sorry, the value entered was not a number, we are discarding the value" << std::endl; std::cin.clear(); std::cin.ignore(LINELEN,'\n'); // flush to the end of line or 80 characters, whichever comes first } // if we reach this case we know they entered numbers, // but we still need to check for negative values else if (x < 0) { std::cout << "You entered a negative value for the number" << std::endl; } // if we reach this case then they actually entered valid data! else { std::cout << "Your valid non-negative value was " << x << std::endl; } |
#include <iostream> #include <string> using std::cin; using std::cout; using std::endl; const int LineLen = 80; // max number of input characters to discard on bad input const float Pi = 3.1415; // our approximation for Pi // getANumber gets the user to provide a real number that is greater than the specified minimum // it repeats until the user provides a valid value then returns the valid response float getANumber(std::string prompt, float minimum); int main() { float response; // the valid value the user eventually provided // get a number > 3.1415 from the user then display it response = getANumber("Please enter a number bigger than Pi", Pi); cout << response << " is indeed bigger than our Pi value, " << Pi << endl << endl; // get a number > 0 from the user then display it response = getANumber("Please enter a number bigger than zero", 0); cout << response << " is indeed bigger than 0" << endl << endl; } float getANumber(std::string prompt, float minimum) { // we'll start userInput with an invalid value (their input is supposed to be > minimum) float userInput = minimum; // show the user the prompt and get their current response cout << prompt << endl; cin >> userInput; // first we must check if their response was a non-number if (cin.fail()) { cout << "That was not a number, please try again" << endl << endl; cin.clear(); // clears the error status on cin's input checking cin.ignore(LineLen, '\n'); // discards the non-numeric input stuck in the buffer userInput = getANumber(prompt, minimum); // gets a new value (recursively) } // if it got past the cin.fail check then it was a number, // but we need to check if the number was too small else if (userInput <= minimum) { cout << "Sorry, " << userInput << " is not greater than " << minimum; cout << ", please try again" << endl << endl; // NOTE: we don't use cin.clear/ignore here because the cin succeeded, // we just don't like the numeric value the user gave us userInput = getANumber(prompt, minimum); // gets a new value (recursively) } // otherwise it was ok else { cout << "Thanks!" << endl << endl; } // we now have a valid response, either from the user entering correctly here // or from the recursive call(s) return userInput; } |