I am receiving this error
markup.c: In function ‘GetPrice’:
markup.c:16:23: error: ‘markup’ undeclared (first use in this function)
markup.c:16:23: note: each undeclared identifier is reported only once for each function it appears in
markup.c:17:1: error: ‘output’ undeclared (first use in this function)

here is my codes

    // File: markup.c



        #include<stdio.h>
        // define the value for constant MARKUP


        #define MARKUP 0.1
        float GetPrice(float finalvalue)
        {
        finalvalue=finalvalue-markup*100;
        output=finalvalue;
        return (float)output;
        }



    int main()
    {
    // declare variables

    char proceed;

    float markupvalue;

    float value;

    float finalvalue; 

    float markup=0.1;
    // ask user if they want to use markup calculator
    printf("Would you like to markup an item? y/n:");
        scanf("%c", &proceed);

        // if yes, proceed to next step
        if(proceed == 'y')
        {

    // ask user what item they want to markup
    printf("please enter item value you would like to markup:");
    scanf("%f", &value);

    // print current of an item
    printf("The value you entered is: %c%.2f\n",'$',value);

    // display current markup rate
    printf("The current markup rate is %.1f%c.\n",markup*100,'%');

    finalvalue=(value-markup*100);
    // final price of an item
    printf("The price for your item after markup is: %c%.2f\n",'$',(float)GetPrice(finalvalue));

    }else
    // display this message if user don't want to use markup calculator
    printf("If you didn't want to markup an item you can leave. \n\n");

    // display end message
     printf("Thank you for using Markup Calculator. \n");
    return 0;
    }

Dani AI

Generated

Quick diagnosis: the compiler errors come from two undeclared identifiers inside GetPricemarkup and output. C is case sensitive, so the macro MARKUP is not the same as markup, and any local variable (like output) must be declared before use. , and are correct to point out the case-sensitivity and missing declarations, but there are a few other logic and I/O issues worth fixing at the same time.

Keep the function simple and explicit. Either use the macro inside the function or, better, pass the markup rate into the function so the function is self-contained and testable. Also compute markup as a percentage (add the markup), not value - markup * 100 which subtracts 100 times the rate. Example of a concise, corrected approach:

#include <stdio.h>

float price_with_markup(float price, float rate) {
    return price * (1.0f + rate);   /* rate = 0.10f for 10% */
}

int main(void) {
    char proceed;
    float price;
    const float rate = 0.10f;

    printf("Markup? (y/n): ");
    scanf(" %c", &proceed);           /* leading space skips leftover newline */
    if (proceed == 'y') {
        printf("Item value: ");
        scanf("%f", &price);
        printf("After markup: $%.2f\n", price_with_markup(price, rate));
    }
    return 0;
}

Extra tips and checklist:

  • Declare every variable you use (or pass it as a parameter). That fixes the undeclared errors.
  • Use scanf(" %c", &ch) (note the leading space) to avoid reading a leftover newline.
  • Prefer printf("$%.2f\n", value) instead of printing a char for the dollar sign.
  • Remove unnecessary casts like (float) on a float, and prefer clear names (price, rate, price_with_markup).
  • Compile with warnings enabled (gcc -Wall -Wextra) — the compiler will point out most of these mistakes.

These changes address the compile errors and make the markup calculation correct and maintainable.

Recommended Answers

All 3 Replies

where is markup defined in getprice() ? if you trying to use the macro defined, then it is MARKUP (in capitals) not markup. change it to capitals or define another markup in getprice() as shown:

int markup=9;

or any value which you want. try it and thanks.

Take note to what nitin1 said, however, there are other issues.

  1. In your Getprice() function you didn't variable output was not declared. It just came out of the blues. These I did to make it work:

        #define MARKUP 0.1
    
        float GetPrice(float finalvalue)
        {
        finalvalue = finalvalue - MARKUP * 100;  // markup was used
        float output = finalvalue;  // you don't really need output
        return (float)output;
        }
    

But really, I don't know why you need to still have to use another variable, when all you can do it just return the "finalvalue".

  1. In your main() function, the variable "markupvalue" was not also used. Though, your code will complie and run, but you might what to also check that.

Hope this help

C is Case Sensitive it means you must use all variables, macros, functions, ets as is in decleration.
You also have to declare all variables before you are using them.
I recommend you to use Camel Notation in naming of everythings except macros.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.