3

i want to change my variable passed as argument to this function:

bool verifyStudent(string id, string name, int grade, int points, string type) {
if(!verifyId(id)){
    cerr << "Please enter 8 charactes id! format: YYMMDDCC\n";
    cin >> id;
    return false;
} else
if(!verifyName(name)){
    cerr << "Please enter name to 35 characters!\n";
    cin >> name;
    return false;
} else
if(!verifyGrade(grade)){
    cerr << "Please enter class between 8 and 12!\n";
    cin >> grade;
    return false;
} else
if(!verifyPoints(points)){
    cerr << "Please enter points between 0 and 300!\n";
    cin >> points;
    return false;
} else
if(!verifyType(type)){
    cerr << "Please enter 1 charater type! format: R,r - regional, D,d - district, N,n - national, I,i - international\n";
    cin >> type;
    return false;
} else {
    return true;
}

}

how i should get access to the given variable and change it when it isn't verified by other function?

here is the way i call the function:

verifyStudent(iId, iName, iGrade, iPoints, iType);
fre2ak
  • 329
  • 2
  • 8
  • 18
  • Though I happily answer these kinds of questions, consider that this is basic c++ knowledge that can be found and learned in any books or tutorials in the first few chapters/lessons. – Vincent Jun 10 '12 at 22:04
  • Each call to `verifyStudent` allows at most one value to be corrected, and doesn't verify the correction, so I hope you're calling it in a loop ala `while (!verifyStudent(iId, iName, ...))) ;`, but then in realistic systems it's good to have a way for them to cancel the operation too if they realise they don't have all the information to hand etc.. – Tony Delroy Jun 10 '12 at 22:34
  • Possible duplicate of [When I change a parameter inside a function, does it change for the caller, too?](http://stackoverflow.com/questions/1698660/when-i-change-a-parameter-inside-a-function-does-it-change-for-the-caller-too) – Jason C Sep 29 '16 at 19:39

2 Answers2

10

In order to change the arguments, you would have to take references:

bool verifyStudent(string& id, string& name, int& grade, int& points, string& type) 

Although I'd say that function is not verifyStudent as much as verifyAndCorrectStudentIfNeeded.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
2

Quote:

Therefore, C++ has two parameter passing mechanisms, call by value (as in Java) and call by reference. When a parameter is passed by reference, the function can modify the original. Call by reference is indcated by an & behind the parameter type.

Here is a typical function that takes advantage of call by reference [...]

void swap(int& a, int& b) { [...] }

More here -> A3.5. Functions

Community
  • 1
  • 1
Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59