0

I have finish a program and it can run smoothly on other platform like replit... But one I run it on vs code it shows the following error and it also have something weird happen when I run check 50 for it.

Error occurs when running check50 cs50/problems/2022/x/cash

:) cash.c exists
:) cash.c compiles
:( get_cents returns integer number of cents
    expected "100", not "32662"
:) get_cents rejects negative input
:) get_cents rejects a non-numeric input of "foo" 
:) calculate_quarters returns 2 when input is 50
:( calculate_quarters returns 1 when input is 42
    expected "1", not "32638"
:) calculate_dimes returns 1 when input is 10
:( calculate_dimes returns 1 when input is 15
    expected "1", not "32528"
:) calculate_dimes returns 7 when input is 73
:( calculate_nickels returns 1 when input is 5
    expected "1", not "32559"
:( calculate_nickels returns 5 when input is 28
    expected "5", not "32731"
:( calculate_pennies returns 4 when input is 4
    expected "4", not "32588"
:) input of 41 cents yields output of 4 coins
:) input of 160 cents yields output of 7 coins

Error occurs when I run make cash

cash.c:13:1: error: non-void function does not return a value [-Werror,-Wreturn-type]
}
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [<builtin>: cash] Error 1

My code is attached below

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

  int x=0;
  int amount=0;

int get_cents(){
do{

 amount=get_int("Change owed:");
}while(amount<=0);
}

int calculate_quarters(){

     while(amount>=25){
     amount=amount-25;
     x++;
    }
    }


int calculate_dimes(){
     while(amount>=10 && amount<25){
     amount=amount-10;
     x++;
    }
}

int calculate_nickels(){
     while(amount>=5 && amount<10){
     amount=amount-5;
     x++;
    }
}

int calculate_pennies(){
  while(amount>=1 && amount<5){
     amount=amount-1;
     x++;
}}

int main()
{


  get_cents();
  do{
  calculate_quarters();
  calculate_dimes();
  calculate_nickels();
  calculate_pennies();

}while(amount>0);
  printf("%d\n",x);


return 0;
}

Please help me, thanks a lot.

1 Answer 1

2

You've created 5 functions that each is required to return an int value, yet there's not a single return statement anywhere. That won't get past the cs50 compilers.

The code needs to be changed to either have the return values (and then use them in main) or the functions need to be designed to return void instead of int.

void quarters(...)

BUT, let's take this further. The code uses global variables x and amount. While it may be technically correct and will work, it's a very, very bad practice that often leads to software bugs. Global vars should be avoided unless there's a really good justification for them. Also, part of the exercise of creating these functions is to learn how to pass vars around between functions and main, as well as how to modify vars in the calling code.

You should try to rewrite the code without globals.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .