0

I have done CS50 for about 2 weeks now, and I keep on getting stuck at this part of the code. (Greedy Problem set)

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

#define Q 25;
#define D 10;
#define N 5;

    int main(void)
    {

    do
        {
        printf ("How much change do you owe?\n");
        printf ("I will count the coins it takes: ");
        float Change = GetFloat();
        }
    while(Change < 0);

    }

Whenever I test run this code in the compiler, it keeps on telling me

greedy.c:16:11: error: unused variable 'Change' [-Werror,-Wunused-variable]
    float Change = GetFloat();
          ^
greedy.c:18:7: error: use of undeclared identifier 'Change'
while(Change < 0);

I don't know why this happens. For all other programs I see that do this, it runs perfectly. However, when I type, the compiler seperates my variable from the outside.

2

Change when declared inside the loop has local scope, and that's why compiler isn't able to recognize it, try declaring it before the loop.

float Change;
do
{
      printf ("How much change do you owe?\n");
      Change = GetFloat();
} while (Change < 0);

Hope that helps!

  • But, then how do I make it so that when a user types something else, I reprompt them? – BooFluff May 16 '15 at 23:31
  • Answer edited, now it will make sure that only non-negative values of Change are allowed for further execution. You may play with the test condition to see how you can achieve other constraints. – sinister May 17 '15 at 2:40
  • wat? that has never worked even when I added the variable before... err... thx – BooFluff May 17 '15 at 4:13

You must log in to answer this question.

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