0

I understand the logic but I keep getting these errors

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

int main(void)

float n;


 do
   {
    printf("how much do I owe you\n");

      n = get_float();
    int cents = n * 100;

   cents= round (cents);
   }
   while (n < 0);

{
    int quater = 0;
    int dime = 0;
    int nickel = 0;
    int penny = 0;
    int balance = 0;

    while (cents > 25);
    quater = cents / 25;
    cents = cents - (quater * 25);

    while (cents > 10);
    dime = cents / 10;
    cents = cents - (dime * 10);

    while (cents > 5);
    nickel = cents / 5;
    cents = cents - (nickel * 5);

    while (cents > 1);
    penny = cents / 1;
    cents = cents - (penny * 1);

    balance = cents;

printf("Quaters:%i \n", quater);
printf("Dime:%i \n", dime);
printf("Nickels:%i \n", nickel);
printf("Penny:%i \n", penny);
printf("Balance:%i \n", balance);

}

greedy.c:5:15: error: expected ';' after top level declarator
int main(void)
              ^
              ;
greedy.c:10:2: error: expected identifier or '('
 do
 ^
greedy.c:19:4: error: expected identifier or '('
   while (n < 0);
   ^
greedy.c:21:1: error: expected identifier or '('
{
^
4 errors generated.
make: *** [greedy] Error 1

I tried removing the do function

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

int main(void)

//float n;


// do

{
    printf("how much do I owe you\n");

     float n = get_float();
    int cents = n * 100;

   cents= round (cents);

   int quater = 0;
    int dime = 0;
    int nickel = 0;
    int penny = 0;
    int balance = 0;

    while (cents > 25);
    quater = cents / 25;
    cents = cents - (quater * 25);

    while (cents > 10);
    dime = cents / 10;
    cents = cents - (dime * 10);

    while (cents > 5);
    nickel = cents / 5;
    cents = cents - (nickel * 5);

    while (cents > 1);
    penny = cents / 1;
    cents = cents - (penny * 1);

    balance = cents;

printf("Quaters:%i \n", quater);
printf("Dime:%i \n", dime);
printf("Nickels:%i \n", nickel);
printf("Penny:%i \n", penny);
printf("Balance:%i \n", balance);

}


  // while (n < 0);

but now I get no errors but it doesn't run as expected.

What am I doing wrong in both cases

Thanks

1

It looks like your code is just missing brackets {}. Whenever you use while loops, for loops, if statements, etc, you must use brackets around your code.

Example 1:

while(x>y) {

    //your code here

}

Example 2:

int main(void){

    //your code here

}

I see that you have while(cents > 25); (for example) but you actually don't need a semicolon here. Instead, you should use brackets.

Hopefully, this helps :D

You must log in to answer this question.

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