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:
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
std::string breakCash(int numOfPennies)
{
//Showing what US coins are needed
std::cout << "\nIn US Currency you have: ";
//Calculating the number of US coins
float cents = numOfPennies % 100; // Necessary for CA
int dollars = numOfPennies / 100; //dollars amount
numOfPennies = numOfPennies - dollars * 100;
int quarters = numOfPennies / 25; //quarters amount
numOfPennies = numOfPennies - quarters * 25;
int dimes = numOfPennies / 10; //dimes amount
numOfPennies = numOfPennies - dimes * 10;
int nickels = numOfPennies / 5; //nickels amount
numOfPennies = numOfPennies - nickels * 5;
int pennies = numOfPennies; //pennies amount
//Displaying US coins
std::string s = std::to_string(dollars) + " dollars, "
+ std::to_string(quarters) + " quarters, "
+ std::to_string(dimes) + " dimes, "
+ std::to_string(nickels) + " nickels, and "
+ std::to_string(pennies) + " pennies.\n\n";
//Showing the amount in Canadian currency.
//Calculating Canadian currency equivalent.
float cacurrency = (dollars + (cents/100)) * 1.33;
//Setting precision to 2 decimal places
s += "In Canadian currency you have: " + std::to_string(cacurrency);
return s;
}
int main()
{
int numOfPennies = 0;
std::cout << "Please enter all of your pennies: ";
std::cin >> numOfPennies;
std::cout << breakCash(numOfPennies) << '\n';
return 0;
}
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).