0

I don't know what I'm doing wrong I get no errors. I feel like my code

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

int main(void) { float x;

float a = 25;
float b = 10;
float c = 5;
float d = 1;
int coins = 0;

do
{
    //prompt the user
    x = get_float("%s", "change owed? ");
}
while (x < 0);


int y = round(x * 100);

while (a <= y ){
    return(y - b);
    coins++;
}
while (b <= y ){
    return(y - c);
    coins++;
}
while (c <= y ){
    return(y - c);
    coins++;
}
while (d <= y ){
    return(y - d);
    coins++;
}
printf("The number of coins is %i\n", coins);

}

3 Answers 3

0

The return statement ends the execution of a function, returning control to the calling function.

In this case your function is main(), so when you call return it will exit your program, passing the value to the operating system as an "exit code" (which you won't see unless you explicitly check for it).

1
  • Yeah I figured it out return doesn't work there!
    – Labritzky
    Commented Dec 23, 2020 at 19:41
1

try using /* */ to surround all of your comments instead of the other // prefix. i don’t know why, but my program wouldn’t pass the checks when i used //. it could be something to do with the 2021 upgrades or some issue with clang maybe, i really don’t know but might be worth a shot..

2
  • If your comments were failing because you used // instead of /* */, then it was likely because of something in the comment or your comment ran across multiple lines, each of which would need to start with //
    – Cliff B
    Commented Dec 24, 2020 at 1:20
  • neither applied actually. when i changed the comment format to the above, i got 4/4 1.0 as a score. i thought the same as you initially, but the // wouldn’t process. i’m aware it’s not incorrect formatting. but the file wouldn’t process yesterday evening with the //. weird i know. it was an anomaly Commented Dec 24, 2020 at 11:10
1

~First of all when you prompt user why did you typed %s just put like this x = get_float( "change owed") ; ~Second mistake is logical For while loop you just have to divide the rounded number by 25 or 10 or 5 and so on. And then increment the number.

Example while( y / 25){ Coins ++ } ; And so on :-)

1
  • Thanks for your input I didn’t though of dividing. Brilliant!
    – Labritzky
    Commented Dec 24, 2020 at 20:16

You must log in to answer this question.

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