Return statement in function

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?


#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

string breakCash(int numOfPennies)
{
cout << endl;

//Showing what US coins are needed
cout << "In 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
cout << dollars << " dollars, " << quarters << " quarters, ";
cout << dimes << " dimes, " << nickels << " nickels," << endl;
cout << "and ";
cout << pennies << " pennies." << endl;
cout << endl;

//Showing the amount in Canadian currency.
cout << "In Canadian currency you have: ";

//Calculating Canadian currency equivalent.
float cacurrency = (dollars + (cents/100))*1.33;

//Setting precision to 2 decimal places
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << cacurrency;
cout << endl;

}

//Main funciton
int main()
{
int numOfPennies = 0;
cout << "Please enter all of your pennies: ";
cin >> numOfPennies;
cout << breakCash(numOfPennies);
return 0;
}
Last edited on
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.

Change that line in main to
 
    breakCash(numOfPennies);


And change the original function declaration to
 
void breakCash(int numOfPennies)




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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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).
Or use a stringstream instead of cout inside the function. Then at the end, return the .str() which is the contents of the stringstream.

http://www.cplusplus.com/reference/sstream/ostringstream/ostringstream/


Topic archived. No new replies allowed.