Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
/* 	Program:		prog3.cpp
	By:				Mackenzie Ritter
	Last Modified:	Oct 31, 2017
	Purpose:		To give customer their cost based on amount of books purchased.
	Notes:
*/
#include 
#include 

using namespace std ;

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

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

void Instructions ()
{
	cout > books ;
}

void totalCost (int books)
{
	float total = books * 8.99 ;
}

void percentoff (float total)
{
	float discount = 0.15 * total ;
}

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

void receipt (int books, float total, float discount, float cost)
{
	cout << books << endl ;
	cout << "$" << total << endl ;
	cout << discount << "% off" << endl ;
	cout << "$" << cost << endl ;
}


What I have tried:

I have tried adjusting the parameters because I believe I had them wrong before. When I run the program, it gives me strange output. For example, when it asks me for a number of books, I will enter 2 and the program will give
"-2091432192
$4.59163e-41
0% off
$0"
If you could help me, I would greatly appreciate it.
Posted
Updated 31-Oct-17 20:19pm
v2
Comments
Richard MacCutchan 1-Nov-17 5:16am    
Where is the code for the numBooks function? Also, your Instructions function will not work (or even compile).
Member 13479017 1-Nov-17 7:32am    
Sorry, I think someone edited my question. The code is supposed to be:
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 ;
}
... for that part.
Richard MacCutchan 1-Nov-17 9:11am    
I just checked and the only thing that person did was to add <pre> tags around your code so it is easy to read.

However, as to your problem, you declare numBooks as a function that returns an integer value, but a) you do not return anything from it , and b) even if it did, you do not capture the returned value when you call it. I would suggest going through your course notes in more detail to understand the use of parameters and returned values in C functions.
Member 13479017 1-Nov-17 7:50am    
I used the debugger and it says that after I enter the number of books, 2, the program says that books equals -5040. Where does that number even come from?

1 solution

You need to learn what is the scope of variables with C/C++ language, and how to propagate values that are results of routines.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900