-1

I have created this code for my class and I have worked on it for a week without it working. The program compiles, but the output is not correct. I believe that my parameters are the problem, but I have researched many questions on this forum and two other discussion forums. The questions being asked did not help me with my situation.

*/
#include <iostream>
#include <cmath>

using namespace std ;

void Instructions () ;
int numBooks (int) ;
float totalCost (int, float) ;
float percentoff (float, float) ;
float finalCost (float, float, float) ;
float receipt (int, float, float, float) ;

int main ()
{
    int books ;
    float total, cost, discount, novels ;
    Instructions () ;
    numBooks (books) ;
    totalCost (books, total) ;
    percentoff (total, discount) ;
    finalCost (discount, total, cost) ;
    receipt (books, total, discount, cost) ;    
}

void Instructions ()
{
    cout << "This program will ask for the number of books being purchased. It will then calculate your discount, and provide the total cost." << endl ;
}

int numBooks (int books)
{
    cout << endl << "Enter the number of books you wish to purchase." << endl ;
    cin >> books ;
    return books ;
}

float totalCost (int books, float total)
{
    float price = 8.99 ;
    total = books * price ;
    return (total) ;
}

float percentoff (float total, float discount)
{
    float percent = 0.15 ;
    discount = percent * total ;
    return (discount) ;
}

float finalCost (float discount, float total, float cost)
{
    cost = total - discount ;
    return (cost) ;
}

float receipt (int books, float total, float discount, float cost)
{
    cout << books << endl ;
    cout << "$" << total << endl ;
    cout << discount << "% off" << endl ;
    cout << "$" << cost << endl ;
}
Mackenzie
  • 11
  • 3
  • 3
    [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – François Andrieux Nov 02 '17 at 17:01
  • 1
    `"I believe that my parameters are the problem"` - Why do you believe that? What debugging has led you to that conclusion? – David Nov 02 '17 at 17:02
  • 1
    To start with, edit the question and paste in the output that you are expecting and the output that you are getting here. – Sameer Sawla Nov 02 '17 at 17:03
  • 1
    Your function parameters are passed by value. Each function parameter is a copy of the original value. Assigning to it does not change the original variable passed to the function. – François Andrieux Nov 02 '17 at 17:03
  • 1
    You are ignoring the value returned by `numBooks`. – vishal-wadhwa Nov 02 '17 at 17:03
  • 2
    Debugger. Use a debugger. A debugger allows you to single step through your program, *watching* values of variables and exploring the execution path. Using a debugger is often faster than correctly posting to StackOverflow, and waiting for a code inspection or for somebody to debug your program for you. – Thomas Matthews Nov 02 '17 at 17:07
  • Since you are here on this problem now. It would be really wise to truly understand what pass by value and pass by reference. Following the link given by @FrançoisAndrieux above would be adequate to start with... – Sameer Sawla Nov 02 '17 at 17:08

3 Answers3

2

your functions are passing almost all by copy and you are doing nothing with the returned value, for example this function:

int numBooks (int books)
{
    cout << endl << "Enter the number of books you wish to purchase." << endl ;
    cin >> books ;
    return books ;
}

so as it is, it will never change the value of the non initialized variable in the main function

int main ()
{
    int books ;

you can instead pass a reference so the changes inside the function take effect on the variable passed as parameter, example:

int numBooks (int& books) {....

and then your functions dont need to return any value anymore, so they can be rewritten as

void numBooks (int& books) {....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

Your functions return a value which the main program doesn't catch.

Some functions have a return type but they don't return a value.

Frank
  • 9
  • 3
0

You are not using return values of your function

inside main

int main ()
{
    int books ;
    float total, cost, discount, novels ;
    Instructions () ;
    books = numBooks (books) ;
    total = totalCost (books, total) ;
    discount=percentoff (total, discount) ;
    cost = finalCost (discount, total, cost) ;
    receipt (books, total, discount, cost) ;    
}
Sniper
  • 1,428
  • 1
  • 12
  • 28