-5

I am making a greedy algorithm to calculate number of coins in a value in cents stored in a variable. I had it all working out basically, but I had no way of differentiating between singular and plural for the denominations, ( printing "1 quarters" instead of "1 quarter" for example) and the way how I was doing it with sequential if conditions for each denomination, I couldn't do the same for singular or plural differentiation for each coin since it would give me back two quarters and one quarter instead of either or, so I'm trying to use an if else statement but it says that the else statement basically has no if statement to relate to which I don't understand. My compiler also says that the if statement nested in the other one has "no body" for some reason.

Don't mind the scanf, I will change to a better input method once I get the rest figured out.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void)
{
   int n;
   scanf("%d",&n);
   int q = (n/25);

   if (n > 24)
  {
     if (n > 49);
        printf("%d quarters\n", q);

     else
        printf("one quarter\n");
  }
    return 0;
}
Derrick Rose
  • 664
  • 2
  • 9
  • 21
Evan
  • 3
  • 3

4 Answers4

1

You have bad syntax in your code. if (n > 49); {. This line is wrong, there should not be a semi-colon before your opening bracket. Try this.

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

int main(void) {
    int n;
    scanf("%d", & n);
    int q = (n / 25);
    if (n > 24) {
        if (n > 49) {
            printf("%d quarters\n", q);
        } else {
            printf("one quarter\n");
        }
    }
    return 0;
}
DominicEU
  • 3,585
  • 2
  • 21
  • 32
0

Remove the semicolon after:

if (n > 49);
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
0

This line:

if (n > 49);

has a semicolon. That semicolon is treated as the body of the true path of the if statement. You must also have gotten a compiler error about the unmatched else as well.

FYI: you will sometimes see an empty expression like this used by people writing loops that have no body.

Speed8ump
  • 1,307
  • 10
  • 25
0

Search well before asking a question. See this answer to know about how to code if..else and nested if

if(
condition)

if(condition)

is only allowed nit

if
()
Community
  • 1
  • 1
Embedded C
  • 1,448
  • 3
  • 16
  • 29