#include <cstdio> // 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() { printf("Something!\n"); } // 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; printf("Please enter an integer value\n"); scanf("%d", &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 printf("x is 3!\n"); } |
// variables to hold two numbers the user will enter, // here we have initialized both to negative values float x = -1; float y = -1; // variable to count how many variables were successfully read int scanCount; // prompt the user to enter two non-negative numbers print("Please enter two non-negative numbers\n"); // read the numbers into x and y, use scanCount to record how many variables were filled // i.e. scanCount is 0 if scanf couldn't read anything, // 1 if scanf could read the value for x but not y // 2 if scanf successfully read both x and y values scanCount = scanf("%f %f", &x, &y); // check for non-numeric input if (scanCount == 0) { printf("Sorry, the first value entered was not a number, we are discarding both values\n"); scanf("%*s"); // discards the first value scanf("%*s"); // discards the second value } else if (scanCount == 1) { printf("Sorry, the second value entered was not a number, we are discarding it\n"); scanf("%*s"); // discards the second value, the first value is already in x } // if we reach this case we know they entered numbers, // but we still need to check for negative values else if ((x < 0) || (y < 0)) { if (x < 0) { printf("You entered a negative value for the first number\n"); } if (y < 0) { printf("You entered a negative value for the second number\n"); } } // if we reach this case then they actually entered valid data! else { printf("Your valid non-negative values were %g and %g\n", x, y); } |
// 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 cout << "Please enter a non-negative number" << endl; // read the number into x, use cin.fail() to check if it succeeded cin >> x; if (cin.fail()) { cout << "Sorry, the value entered was not a number, we are discarding the value" << endl; cin.clear(); 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) { cout << "You entered a negative value for the number" << endl; } // if we reach this case then they actually entered valid data! else { cout << "Your valid non-negative value was " << x << endl; } |