I've been dealing with this problem for about 2 days now.

Write a program that accepts the price of an item and displays the discounted price. Accept also the amount given by the customer (assume the amount is greater than or equal to the discounted price) and display the change.

I'm having errors with the "Write a program that accepts the price of an item and displays the discounted price" part. Declaration of syntax error in function main.

And many other problems..

Help? (Cute Sad Face)

You must first help yourself by posting the code you're having trouble with.

I am having trouble with making the discount part:

main()
{
float price1, prod
clrsrc();
printf("Enter Item Price");
scanf("%f, &price1");
prod = price1 * 0.50;
printf(%.2f * %.2f\n", price1, prod);
getch();
}

I need to through this before i can go to the entering the amount part..

Never really learnt how to use the printf and scanf functions.

This looks more like it though.

int main()
{
    float price1, prod;
    clrsrc();
    printf("Enter Item Price");
    scanf("%f", &price1);
    prod = price1 * 0.50;
    printf("%.2f %.2f\n", price1, prod);
    getchar();

    return 0;
}

so i revised and here's what i did:

#include <stdio.h>
main()
float price, discount, change;
discount = 0.50;
printf("Enter Item Price: ");
scanf("%f",&price);
printf("The discounted price is: %.2f\n", price -price*discount);
return 0;
}

The problem is when i enter a value when i'm running the program, it goes back to Turbo C and then i need to run it again to view the result.

And you are using Turbo-C because? It is old, outdated, non-standard, and definitely not complient with current standards. Stuff you write with Turbo-C won't work on anything but a Windows system - and I'm not sure about current Win7/8/8.1 systems either.

All that aside, try this:

    #include <stdio.h>
    int main(void)
    {
        float price = 0.0;
        float discount = 0.50;
        printf("Enter Item Price: ");
        fflush(stdout);
        scanf("%f",&price);
        printf("The discounted price is: %.2f\n", price - (price*discount));
        return 0;
    }

At least it works with gcc on Linux without problems.

Member Avatar for atulgupta9

Earhawk if you are unable to view the output and the compiler just quits after you type in the last input..
You can try adding
Header files
conio.h

and at the bottom of the code before return 0;
type in
getch();
Hope this solves your problem CHEERS!!!

conio.h is not standard. Furthermore it's old and unsupported, and it's funtionality varies between platforms and operating systems. People stopped using it 20 years ago, and it should no longer be taught.

The best solution would to run it from a command line in a dos shell, powershell or unix shell, like people would traditionally do. You can also use the standard getchar function if you must.

Furthermore it's old and unsupported,

Where available it's supported, the problem is that it's not always available due to not being standard.

People stopped using it 20 years ago

Clearly that's not the case given how often we see it used. ;)

You can also use the standard getchar function if you must.

Which unfortunately is an incomplete solution. This goes into great detail about the issues involved, from a C++-centric perspective. In C, you're SOL without dropping to a non-standard solution (ie. non-blocking I/O).

Sorry for disturbing . . .

i never use conio.h because i'm learning C++(and hopefully ,there isn't like conio.h).But i often see some threads indirectly focused on conio.h.All the exprets criticise for using conio.h.

Furthermore it's old and unsupported,

why is it unsupported ?i want to know how conio.h was helpful and why did they discontinue conio.h.

it goes back to Turbo C and then i need to run it again to view the result.

i read similar threads. As a temporary solution , you can either use
getchar();
or
you can use system("pause");. But be aware , its horrible , its Operating system dependent and therefore may not work on other OS.this statement actually run DOS command. before using this , you have to add stdlib.h in which system is defined.

All the exprets criticise for using conio.h.

I can't speak for all experts, but I only criticize unnecessary use of conio.h. If it's used with purpose rather than as mindless boilerplate, it's all good. The conio library has useful stuff, after all. I've even included parts of it in my implementation of the standard C library because it's useful.

i want to know how conio.h was helpful and why did they discontinue conio.h.

Once again, if the compiler supports the library, it's safe to use. The problem is that not all compilers support it, which means code that uses it is inherently non-portable. Is that a problem? Not necessarily, but portable code should be favored where practical.

Also, it's behaviour and interface may vary between implementations when it is provided. for example, how do you know to use "cgets" or "_cgets"? (most newer compilers use the underscore to indicate these legacy functions, and expect people to port their old programs by using a regex if they need too.)

Consider that there are better alternatives, like ncurses for example, for which you'll find more code examples, and a larger community, as well as a guerentee on which platforms it will work on, and a stronger guerentee on behaviour and interface. Not to mention it has more features. Or you could use escape characters if you're writting a program for VT100-compatible terminals (ie, your program is for Unix).

There's really no reason to use conio over more modern alternatives, and I wouldn't recommend in any fresh project simply because there is no advantage, and there are a lot of disadvantages you'll first need to consider.

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.