0

I thought I had finished Greedy but the program won't run beyond the initial request for change.

Can't for the life of me see where the error is.

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

int main(void)
{
    //prompt user for change required
    //and ensure entry is valid
    int chng_req = 0;
    do
    { 
        printf("How much change is owed? \n");
        int chng_req = GetFloat();
    }
    while (chng_req <= 0);

    //convert to cents, round up to eliminate
    //floating point imprecision and express as int
    int cents = (int) roundf(chng_req * 100);

Not looking for answers, just a big red pointy finger to say, 'Here, look at this again!'

9
  • Acts which are reasonable under cs50x specs -" Communicating with classmates about problem sets' problems in English (or some other spoken language). Discussing the course‚Äôs material with others in order to understand it better. Helping a classmate identify a bug in his or her code, as by viewing, compiling, or running his or her code, even on your own computer. " ;) Commented Jun 29, 2014 at 4:40
  • Okay, so according to my version of what is reasonable...
    – lavoren
    Commented Jun 29, 2014 at 5:00
  • you can share your's code snippets to others to discuss errors .And you may point many red pointy fingers :P Commented Jun 29, 2014 at 5:11
  • have a glance at some other questions on here , you would surely get an idea .Which can help you to debug and discuss more effectively . Commented Jun 29, 2014 at 5:13
  • Okay, didn't realise I could post snippets. Every day is a school day... :)
    – lavoren
    Commented Jun 29, 2014 at 5:59

3 Answers 3

1
   int chng_req = 0;
do
{ 
    printf("How much change is owed? \n");
    int chng_req = GetFloat();

You are declaring an integer chng_req and then, in your do while loop, you are declaring another integer called chng_req. That stores the user entry, but immediately disappears when the loop ends (it goes out of scope).

Then, you are trying to use chng_req in your calculation, but it has a 0 value. Don't declare multiple ints.

1

You are declaring chng_req to be an int but you are getting a floating value from the user, so you should do float chng_req = GetFloat();

0

that formula for cents with the roundf looks funky.

Try putting a simple printf statement before and after it ("this is before roundf") and then one ("this is after roundf") to see if that statement is executing or if that's the hang up

You must log in to answer this question.

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