I got an error in “C” that doesn't seem to apply

I got this error ( cash.c:69:7: error: function definition is not allowed here { ) while working for the cash problem of pset 1 of cs50. The point of this program is to get the change of a payment as input and the program tells you the least amount of coins needed to pay back the change

I checked the lines over and over again but the error doesn’t seem to apply. THE ERROR IS ON LINES 69, 78, 87, AND 96. I ALSO PUT COMMENTS LABELED “OVER HERE!!!” (THE ERROR APPEARS AT THE END OF THE CODE BTW) I DELETED THE UNNECESSARY PARTS OF THE CODE Here’s the code:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

//Making computer remember functions

void Quarter(); 

void Dime();

void Nickel();

void Penny();  

int main(void)
{

//variables
    float input = get_float("Change owed: ");
    float process = input;
    int ncoin = 0;
    bool done = false;

    //the array for coins
    float Coins[4];
    //Quarter
    Coins[0] = 0.25;
    //Dime
    Coins[1] = 0.10;
    //Nickel
    Coins[2] = 0.05;
    //Penny
    Coins[3] = 0.01;

//Coin Methods
void Quarter()
      { //OVER HERE!!!! 1
          process -= Coins[0];
          ncoin++;
      }  



      void Dime()
        { // OVER HERE!!! 2
            process -= Coins[1];
            ncoin++;
        }



      void Nickel()
        { //OVER HERE!!! 3
        process -= Coins[2];
        ncoin++;
    }



  void Penny()
    { //OVER HERE!!! 4
      process -= Coins[3];
      ncoin++;
    } 
}

I think you must close } under this sentence to close main function.

You do not declare functions inside of functions in C. Your function definitions should be outside of (below probably) main.

Once you move your definitions, you will want to add these function calls to your main.

At that time you will notice that these functions don’t do anything. That’s because

  1. You need some sort of loops
  2. You need parameter, local variables and return values.

Additional note - if you use doubles or floats to represent numbers of cents, you will have round off errors.

As @JeremyLT mentions, you don’t declare functions inside of functions in C. Once you place your functions properly, you’ll find the program still won’t compile because the Coins and ncoins variables are declared in main() and not available to the other functions. That’s where his second point comes in.

And completely pedantically, your comment is misleading – there are no methods in C , per se.

1 Like