user8533488
user8533488

Reputation: 21

Global variables not updating in C

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);
}

Upvotes: 0

Views: 2471

Answers (3)

Christian Gibbons
Christian Gibbons

Reputation: 4370

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.

Upvotes: 0

Matteo Ragni
Matteo Ragni

Reputation: 2956

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
}

Upvotes: 0

pm100
pm100

Reputation: 50120

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

Upvotes: 2

Related Questions