My professor says my breakCash function needs a return statement, and the function needs to be a string. The function works perfectly but a warning pops up telling me I should have a return statement. I just don't understand how I can possibly return a string here. Any help?
//Main funciton
int main()
{
int numOfPennies = 0;
cout << "Please enter all of your pennies: ";
cin >> numOfPennies;
cout << breakCash(numOfPennies);
return 0;
}
My professor says my breakCash function needs a return statement, and the function needs to be a string. The function works perfectly but a warning pops up telling me I should have a return statement. I just don't understand how I can possibly return a string here.
It hardly works perfectly. When I run the program, it outputs page after page of garbage characters to the screen.
At this line in main()
cout << breakCash(numOfPennies);
the cout statement is expecting to print out a string. But it doesn't find one. The program has a serious problem - even though the compiler issues only a warning, which might suggest it is a minor problem, it actually causes undefined behaviour.
My professor says my breakCash function needs a return statement, and the function needs to be a string.
Is that exactly what s/he said?
Is usually said every function should perform one task. For example, if you have a function which makes a calculation and displays the result, you have a function which performs two tasks. It should be better to make two functions.
That’s a general advise, not a strict rule, but it normally helps to write good code.
Is it possible your teacher is just pushing you in following this principle and separating your calculation from the job of printing on screen?
The better you explain us what your teacher asked you, the better we can help you.
You can return a string from your function breakCash() for example this way:
The above code doesn’t format correctly the value for the Canadian currency, so it needs to be improved, but it could be a start (if I have guessed what your teacher wants).