0

Been working on the homework assignment for the edxCS50 course, and still pretty new at C and programming so I was pretty proud to finally get this to compile. But the final result seems like I'm missing something somewhere

The point of the code is to take the user inputted money, and spit out the minimum amount of coins necessary to make that money. The idea was to first convert everything to integers so I could use modulo. If the amount entered was divisible by 25, then that's the end of it and it would divide by 25 to get amount of quarters needed. If not then it would find out how many quarters there are, find the remainder, use it as the remaining change, and rinse and repeat until you get to pennies. At the end it's supposed to add up the amount of coins and spit it out.

The problem is every single time I run it, the end sum results in a big fat "0" . I'm pretty sure it's because the variables remain at the starting value of 0 but I can't figure out why it's not changing the variable values as the code is being run.

First time posting here so feel free to correct me if I'm not specific enough or missing info in this post

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

int main(void)

{float moneytotf=0.0;
int moneytot= moneytotf*100;
int qtr=0;
int dime=0;
int nickel=0;
int penny=0;
int change;
do
{
printf("O hai! How much change is owed?\n");
moneytotf=get_float();
}
while (moneytot<0);

if (moneytot%25==0)
{qtr=moneytot/25;}
else
    {if (moneytot/25<1)
    {qtr=0;}
    else
    {
        qtr=(moneytot-moneytot%25)/25;
        change=moneytot%25;
        if (change%10==0)
        {dime=change/10;}
        else
            {if (change/10<1)
            {dime=0;}
            else
                {dime=((change-change%10)/10);
                change=change%10;
                if (change%5<1)
                {
                    nickel=0;
                }
                else
                    {
                        if ((change-5)<1)
                        {nickel=0;}
                        else
                            {nickel=((change-change%5)/5);
                            change=change%5;
                            if (change==0)
                            {
                                penny=0;
                            }
                            else
                                {penny=change;}
                            }
                    }

                }

    }


    }
}
int sum = qtr+dime+nickel+penny;
printf("%i\n", sum);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

3 Answers3

2

I suspect that you think that

float moneytotf=0.0;
int moneytot= moneytotf*100;

means

"moneytot should always contain moneytotf times 100"

It doesnt mean that - it means

"set moneytot to whatever moneytotf 's value is right now"

Ie 0. You never set it to any other value. You need to assign it after you read moneytotf

pm100
  • 48,078
  • 23
  • 82
  • 145
0

There is no issue with global variables, but simple mistakes in the code

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

int main(void) {
  float moneytotf = 0.0;
  int moneytot = moneytotf * 100; // moneytotf is a float and you are using
                                  // it to assign to an integer. BTW moneytot is
                                  // still 0. Maybe it is better to write:
  // int moneytot = -1;  ??
  int qtr = 0;
  int dime = 0;
  int nickel = 0;
  int penny = 0;
  int change;
  do {
    printf("O hai! How much change is owed?\n");
    moneytotf = get_float(); // You are changing moneytotf and NOT moneytot
  } while (moneytot < 0); // but this condition operates on moneytot that is 0,
                          // so it runs only once

  if (moneytot % 25 == 0) {
    qtr = moneytot / 25; // qtr is always 0
  } else {
    // This code never runs as for now
  }
  int sum = qtr + dime + nickel + penny; // all the others are zero by default
  printf("%i\n", sum); // it will always print 0\n
}
Matteo Ragni
  • 2,837
  • 1
  • 20
  • 34
  • Thanks for the help. – user8533488 Aug 30 '17 at 17:30
  • Whoops tried editing but just found out it had a 5 min limit. I can see where things went wrong now, I assumed that updating the "moneytotf" value (with Getfloat() )would retroactively update moneytot because I had set it equal to moneytotf*100, but I see now that it doesn't go back and update it (is there any way to set it up to do so?). Also with the float/int, is there a way to convert the floats easily into integers? I heard you can declare the variable and define it later, but everytime I try to compile it says I can't do that. Is it not possible? – user8533488 Aug 30 '17 at 17:39
  • No never. The language is imperative. One instruction at the time change the state of the variable. So if the value of moneytotf changes, moneytot does not if not explicitly written in a instruction. – Matteo Ragni Aug 30 '17 at 17:55
  • The correct way to transform a float in an integer is by using the functions `round`/`floor`/`ceil`. Even if you can cast a float/double to an int and in the majority of the cases you get a valid result, it may not work for some cases. The fact that you can declare and than define means that you can write (some time ago you were forced to) something like `int v; v = 10;`. `int v;` is the variable declaration, while `v = 10;` is the variable definition. before the variable definition you may don't know what `v` contains. – Matteo Ragni Aug 30 '17 at 17:58
0

You are asking the wrong question. You do not have any global variables; all of yours are inside the scope of the main function.

Your most immediate issue is that you take in a value to moneytotf and seemingly expect that it will automatically change moneytot, however that is something you need to explicitly do in the code.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29