0
#include <iostream>
using namespace std;

//MainFunctions
int computeDiscount(int);
int mainProgram();

int main()
{
    mainProgram();
}

int computeDiscount(int total)
{
    if (total >= 10 && total <= 19)
    {
        total = (total-(total * .2));
        cout << total;
    }
}

int mainProgram()
{
    int t;
    cout << "What is the total amount for today? " << endl << ">>>";
    cin >> t;

    cout << "The total is: " << computeDiscount(t);
}

Output: 
What is the total amount for today?

10

7The total is: 5007456

What do I do? I want seven to go where the "5007456" is appearing

If I put the function outside of the cout it works... Not sure

I want to call the function in the cout

5 Answers5

3

This is because you are not actually returning the computed value from the computeDiscount function. What you are seeing after the string "The total is: " is a garbage number because of undefined behavior. The nature of value returned implicitly is nondeterministic. With g++ adding an extra flag -Wall would have allowed you to catch that warning([-Wreturn-type])

The function should have returned the value like

int computeDiscount(int total)
{
    if (total >= 10 && total <= 19)
    {
        total = (total-(total * .2));
    }
    return total; 
}

Also the same way, you should have had a return value for mainProgram() at the end, to specify a successful termination of the program.

Also see why should you avoid using namespace std in your programs. See Why is using namespace std; considered bad practice?. Also in C++, you could very well pass around the total variable as a reference and do the manipulation on that. Something like

void computeDiscount(int&);
void computeDiscount(int& total)
{
    if (total >= 10 && total <= 19)
    {
        total = (total-(total * .2));
    }
}

int mainProgram()
{
    int t;
    std::cout << "What is the total amount for today? " << std::endl << ">>>";
    std::cin >> t;
    computeDiscount(t);
    std::cout << "The total is: " << t << std::endl;
    return 0;
}
Inian
  • 80,270
  • 14
  • 142
  • 161
0

int computeDiscount(int total)
{
    if (total >= 10 && total <= 19)
    {
        total = (total-(total * .2));
        cout << total;
    }
}
In this method you are not returning any integer which is suppose to be there and in main function you are trying to print the output which will give you garbage value.
Rajeev
  • 147
  • 1
  • 1
  • 17
0

#include <iostream>
using namespace std;

//MainFunctions
int computeDiscount(int);
int mainProgram();

int main()
{
    mainProgram();
}

void computeDiscount(int *total)
{
    if (*total >= 10 && *total <= 19)
    {
        *total = (*total-(*total * .2));
    }
}

int mainProgram()
{
    int t;
    cout << "What is the total amount for today? " << endl << ">>>";
    cin >> t;
    computeDiscount(&t);
    cout << "The total is: " << t;
    return 0;
}

Or you can pass reference to the function

Rajeev
  • 147
  • 1
  • 1
  • 17
0

in place of cout << total;
is return total;

int computeDiscount(int total)
{
    if (total >= 10 && total <= 19)
    {
        total = (total-(total * .2));
        return total;
    }
}

and do properly ie. take the most C/C++ advantageous feature

 int computeDiscount(int total)
    {
        if (total >= 10 && total <= 19)
            return total -= total / 5 ;
         return   
    }
0

Use pass by reference in function computeDiscount(int&)

int computeDiscount(int&);

int computeDiscount(int& total)
{
    if (total >= 10 && total <= 19)
    {
        total = (total-(total * .2));
        cout << total;
    }
}
Rajeev
  • 147
  • 1
  • 1
  • 17