How to get Function names?

C++ function clarification please?

  • I am currently taking a entry level of C++ and we are currently covering functions. I was trying to come up with a program to practice them. I just want the program to ask the user the age, and if it is between 1 and 100 then it outputs the message with the variables "concatenated" in it. However, I am trying to use the functions for the error checking. I have problems with realizing how the sort of function hierarchy works. By that I mean initializing at top, calling it to whatever you choose in main, and down when you are actually declaring the functions, I get confused on how to tell how to get the keywords I have chosen to work. I am unclear about what names should go in the parenthesis and how that affects the variables in the declaration. You will probably see a few mistakes, but the only ones I'm aware of are in the variable declarations. If someone could set me on the right path, it would be very greatly appreciated. Here is what I have come up with so far. P.S. In the variable declarations you will see the age < '9' I was trying to error check for any characters or special characters. #include <iostream> #include <string> using namespace std; int main() { //Declare needed variables. void showAge(int); void ageCheck(int); const int LOW = 1; const int HIGH = 100; int age; //Get age from the user. cout << "Please enter your age from 1 through 100: "; cin >> age; showAge(age); ageCheck(age); return 0; } //Declare function for outputting age. int showAge(int age) { while(age > LOW || age < HIGH) { cout << "You are " << age << " years old!" << endl; } } //Declare function for error checking. int ageCheck(int check) { while(age < LOW || age > HIGH) { cout << " " <<endl; } if(age < '0' || age > '9') { cout << "Please enter a valid age from 1 through 100: "; cin >> age; } else { showAge(age); } }

  • Answer:

    DataType of variable 'age' is 'int' and you are checking it with 'char' if(age<'0' || age>'9').

Sonastus at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

A few things The first thing - it's your choice, but for me, it makes sense to have the functions declared BEFORE int main(). Like this: //Declare function for outputting age. int showAge(int age) { while(a... } //Declare function for error checking. int ageCheck(int check) { while( .... } int main() { //Declare needed variables. const int LOW = 1; ... } This eliminates the need for void showAge(int); void ageCheck(int); Also you need const int LOW = 1; const int HIGH = 100; outside of int main. The functions are outside of int main, so they can't access them. Why do you have while loops in your functions? If statements would be much better as they won't return an endless stream of " You are __ years old." '0' and '9' shouldn't be in single quotes. age is an int, not a char or string. Why are showAge and ageCheck ints? They don't return anything. They should be void showAge(int age) and void ageCheck(int AGE). As you see, I changed int check to int age. You are using age in the function ageCheck, not check, so change one. I missed a bunch probably, so this is what I changed the code to: #include <iostream> #include <string> using namespace std; const int LOW = 1; const int HIGH = 100; //Declare function for outputting age. int showAge(int age) { if(age >= LOW && age <= HIGH) { cout << "You are " << age << " years old!" << endl; } } //Declare function for error checking. int ageCheck(int age) { if(age <= LOW || age >= HIGH) { cout << " " <<endl; } if(age < 0 || age > 9) { cout << "Please enter a valid age from 1 through 100: "; cin >> age; ageCheck(age); } else { showAge(age); } } int main() { //Declare needed variables. int age; //Get age from the user. cout << "Please enter your age from 1 through 100: "; cin >> age; showAge(age); ageCheck(age); return 0; }

Imagine you have a cursor that moves from line to line in your program, starting from main. It will get to showAge and then jump inside the function and continue from there. When the cursor reaches the end of the function, it will go back to the line after where the function was called. The function parameters (the variables between the parenthesis) will be copied into the function as if they were declared at the top. So in showAge, you will start with a variable called age which is a copy of the age that you passed into the function from main. This variable will disappear when the function ends. If you modify this copy, it will not affect the original variable that you passed in. To do that you will need to use pointers or references.

this should get you going: #include <iostream> #include <string> using namespace std; void showAge(int); void ageCheck(int); int main() { //Declare needed variables. const int LOW = 1; const int HIGH = 100; int age; //Get age from the user. cout << "Please enter your age from 1 through 100: "; cin >> age; showAge(age); ageCheck(age); return 0; } //Declare function for outputting age. void showAge(int age) { if(age > LOW || age < HIGH) { cout << "You are " << age << " years old!" << endl; } } //Declare function for error checking. void ageCheck(int check) { if(age < LOW || age > HIGH) { cout << " " <<endl; } while(age <LOW || age > HIGH) { cout << "Please enter a valid age from 1 through 100: "; cin >> age; } else { showAge(age); } }

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.