-4

im having some trouble completing one of my assignments for my intro coding class. i keep getting the error when compiling, "[Error] 'displayBills' was not declared in this scope. I will attach my code, any suggestions would be greatly appreciated, Thanks!

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int dollars;
cout << "Please enter the whole dollar amount (no cents!).  Input 0 to terminate: ";
cin >> dollars;
while (dollars != 0)
    {
    displayBills(dollars);
    cout << "Please enter the a whole dollar amount (no cents!).  Input 0 to terminate: ";
    cin >> dollars;
    }
return 0;
}

displayBills(int dollars)
{
int ones;
int fives;
int tens;
int twenties;
int temp;

twenties = dollars / 20;
temp = dollars % 20;
tens = temp / 10;
temp = temp % 10;
fives = temp / 5;
ones = temp % 5;

cout << "The dollar amount of ", dollars, " can be represented by the following monetary denominations";
cout << "     Twenties: " << twenties;
cout << "     Tens: " << tens;
cout << "     Fives: " << fives;
cout << "     Ones: " << ones;
}
  • Order of definition/forward declaration. Btw, don't create long lists of uninitialized variables only to assign to them few lines later. – LogicStuff Apr 05 '17 at 21:13
  • Imagine that the compiler reads your program text exactly one time from top to bottom. At the point where it sees you calling displayBills(), it has not yet seen any declaration or definition of that function. You can fix the problem by putting the displayBills() function definition _before_ the main(...) function definition. – Solomon Slow Apr 05 '17 at 21:13

3 Answers3

0

You did not specify a forward declaration for your displayBills function. You must either specify one or put your function before any calls to it.

Huynh
  • 176
  • 1
  • 11
0

In function main, you call function displayBills, yet the compiler does not know this function at this point (because it is declared/defined later in the file).

Either put the definition of displayBills(int dollars) { ... before your function main, or put at least a forward declaration of this function before function main:

displayBills(int dollars);  // Forward declaration; implementation may follow later on;
// Tells the compiler, that function `displayBills` takes one argument of type `int`.
// Now the compiler can check if calls to function `displayBills` have the correct number/type of arguments.

int main() {
   displayBills(dollars);  // use of function; signature is now "known" by the compiler
}

displayBills(int dollars) {  // definition / implementation
  ...
}

BTW: there are several issues in your code which you should take care of, e.g. using namespace std is usually dangerous because of unintended name clashes, functions should have an explicit return type (or should be void), ...

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

Like other people have been saying putting displayBills above main will help with your problem. But also declaring displayBills in a header file called displayBills.h and

#ifndef DISPLAYBILLS_H_INCLUDED
#define DISPLAYBILLS_H_INCLUDED

displayBills(int dollars);
#endif DISPLAYBILLS_H_INCLUDED

then you can have a cpp file of displayBills.cpp where you will define the function displayBills (dont forget to include displayBills.h)

 #include "displayBills.h"

and just move it from under your main function to its own cpp file. and then above your main function include your header file.

I would do this because it makes it a lot easier to know which functions are where in your project instead of jamming all of your functions into the main.

daviCo
  • 1
  • 2