0

EDIT - I changed a few things, and now I'm getting the error message below for line 49. Still needing some assistance to understand! I pasted the entire code below.

I'm working on CS50 Problem Set 1, the Cash problem. I need some help understanding the error I'm getting for the printf function on line 52. Can anyone help me understand what it means by, "expected expression" in this context? TIA!


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

int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int nickels);
int calculate_pennies(int pennies);
int add(int a, int b, int c, int d);

int main(void)
{
    //prompt use for changed owed, in cents
    int cents;
    do
    {
        cents = get_int("Change owed: ");
    }
    while (cents < 0);

    //calculate how many quarters you should give the customer
    int calculate_quarters(int cents);

    int quarters = 0;
    //subtract the value of those quarters from cents
    cents = cents - (quarters * 25);

    //calculate how many dimes you should give the customer
    int calculate_dimes(int cents);

    int dimes = 0;
    //subtract the value of those dimes from remaining cents
    cents = cents - (dimes * 10);

    //calculate how many nickels you should give the customer
    int calculate_nickels(int nickels);

    int nickels = 0;
    //subtract the value of those nickels from remaining cents
    cents = cents - (nickels * 5);

    //calculate how many pennies you should give the customer
    int calculate_pennies(int pennies);

    int pennies = 0;
    //subtract the value of those pennies from remaining cents
    cents = cents - pennies;

    //sum the number of quarters, dimes, nickels and pennies used
    int answer = int add(quarters, dimes, nickels, pennies);

    //print that sum
    printf("%i\n", int answer);
}

int calculate_quarters(int cents)
{
    //calculate how many quarters you should give the customer
    int quarters = 0;
    while (cents >= 25)
    {
        quarters++;
        cents = cents - 25;
    }
    return quarters;
}

int calculate_dimes(int cents)
{
    int dimes = 0;
    while (cents >= 10)
    {
        dimes++;
        cents = cents - 10;
    }
    return dimes;
}

int calculate_nickels(int nickels)
{
    int nickels = 0;
    while (cents >= 5)
    {
        nickels++;
        cents = cents - 5;
    }
    return nickels;
}

int calculate_pennies(int pennies)
{
    int pennies = 0;
    while (cents >= 1)
    {
        pennies++;
        cents = cents -1;
    }
    return pennies;
}

int add(int a, int b, int c, int d)
{
    return a + b + c + d;
}

enter image description here

2
  • In the future, it's best to cut and paste code instead of posting screen capture images. Nobody wants to transcribe code from an image to test it. Run results are a different story, but even then, if it's a check50 run, it's also best to cut and paste the link to the full check50 results included in the output.
    – Cliff B
    Jan 11 at 1:47
  • hmm, I tried pasting the code in just now but it takes away all the formatting...
    – AmyG
    Jan 11 at 14:59

2 Answers 2

1

There are several errors in the portion of code that you posted, including missing parentheses, etc.

The biggest problem you're having seems to be how to use the type identifier int. On line 52, first use of the keyword int is incorrect. It's like you're trying to declare a variable there, where you should be using an already existing variable name of type int.

Additionally, the earlier use of int looks like it's going to cause other problems. Without seeing full code, it's difficult to understand exactly what you're really attempting here. My best guess is that you've declared vars earlier, but are under the misunderstanding that you need to use the type declaration every time you use a var. The type declaration for a variable should only be used when the var is first declared.

Based on what little code is posted, I strongly encourage you to rewatch the first week lectures.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Thank you for the detailed response! I realize without the full context of the code I was using it's hard to understand what I was getting at. On line 49 I'm referring to a function that I defined below and named at the top, but I understand what you are saying about declaring the variables previously and not needing to use "int" again after that. I fixed the parenthesis problem and changed a couple other things but am still having some trouble. I'm going to try and cut and paste the full code into a couple comments (too long for one)
    – AmyG
    Jan 11 at 14:48
  • Thanks again for your help. Your feedback along with some other feedback I got allowed me to figure out the issue(s). I was getting confused about how and when I need to identify the variables, and also when it's necessary to create a function and when it's not needed. In the future, if I wanted to cut and paste code into a question, do you have a suggestion to keep it from taking away all the formatting like above? Thanks again!!
    – AmyG
    Jan 11 at 19:58
  • Yes. Just paste the code into the question, then highlight all the code and click on the curly brace pair {}. This will mark it as code and keep the formatting. BUT, don't paste in the line numbers. The idea is so that the code can be cut and pasted into an IDE for testing.
    – Cliff B
    Jan 12 at 8:14
  • Got it, thank you!!
    – AmyG
    Jan 12 at 14:14
0

The problem is that you are not calling the functions properly that you have introduced.

for example

//calculate how many quarters you should give the customer
int calculate_quarters(int cents);

int quarters = 0;
//subtract the value of those quarters from cents
cents = cents - (quarters * 25);

Should be:

int quarters = calculate_quarters(cents);
//subtract the value of those quarters from cents
cents = cents - (quarters * 25);

I dont know how your code is compiling currently with what you have.

You defined the function prototypes properly above main. But when your usage of them is incorrect, you are repeatedly saying int FuntionName(int variableName); which is basically interpreted as an attempt to override the defintion again or redefine it. That will not invoke or call your function. I believe you have figured this out since you moved on to other lectures, but its disconcerting.

You must log in to answer this question.

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