I'm trying to write a program to calculate a bill for a user after they enter in some details.
It doesn't allow the user to enter in a value for the 'use code', instead skipping that step. Below is only a rough draft of the program, there's plenty still to be done but i'm having real difficult in getting getchat to work the way I want it. Any help would be greatly appreciated

#include <stdio.h>

int main()
{
	char code;
	int account,consump;
	double bill;

	
	printf("Enter account no.: ");
	scanf("%d", &account);
	printf("Enter consumption in kwh: ");
	scanf("%d", &consump);

	printf("Enter your use code: ");
                code=getchar();
	
	if(code=='R' || code=='r')
	{
		bill = 6.052;
		printf("Bill %f\n", bill);
		
	}
	else if(code=='C' || code=='c')
	{
		bill = 60;
		printf("Bill %f\n", bill);
	}
	else if(code=='I' || code=='i')
	{
		bill = 70.00078;
		printf("Bill %f\n", bill);
	}
	
		printf("Account no: %d\n", account);
	return 0;
}

Recommended Answers

All 11 Replies

after entering an integer the program leaves the '\n' in the keyboard buffer. When the next getchar() is called it gets that '\n' from the buffer instead of waiting for user input. To correct this add getchar() after scanf("%d", &consump);

You can clear the input buffer also... use something like this...

char ch;
while((ch = getchar())!= EOF && ch != '\n');

I hope Aia doesn't shout at me for the code I have given here... :D... cause you all know there was a small talk over this code... who comes first, EOF or \n...
The bottom line is, it always works for me for clearing the input buffer...

H!

>>>>As your having a problem in getting the character input that it's not accepting it correctly.

See the problem is due to the 'New line' character. All your character variables get stored in the stack if we are taking one variable input and after that generally we press Enter so the 'new line' character get stored next in the stack.(new line is considered as one of the character '\n')
For the next character input it will take that previous 'new line' character as input by default and will not wait for taking the input for current variable.
For that you just use fflush(stdin) after each input line. so that it will clear(flushes) the previous 'new line' character and take the input correctly.
Try out it!!

H!

//everything good till this:
For that you just use fflush(stdin) after each input line. so that it will clear(flushes) the previous 'new line' character and take the input correctly.
Try out it!!

In C standard fflush accepts only output buffers, not input.
Although most compilers do clear std buffer if you write fflush(stdin) it's not a good idea to do that, because generaly, there is no rule to fflush(stdin). So you never know what will compiler do with it.
The best would be to write your own flush function, and code for it is provided by ahamed101 in previous post, as many times before :)

Only plain wrong if you're not using a compiler that has defined it. Given that that's not the case most of the time: check your compiler's documentation and see if you can use it, then, bare in mind that this code is not 100% portable and SHOULD input not work or the program simply crash you should first look at fflush(stdin);.

Only plain wrong if you're not using a compiler that has defined it. Given that that's not the case most of the time: check your compiler's documentation and see if you can use it, then, bare in mind that this code is not 100% portable and SHOULD input not work or the program simply crash you should first look at fflush(stdin);.

Yeah, right. That's like suggesting flying in a 747 with a small leak in the fuel tank .... it may seem OK at first, but the results you get in the future are better avoided in the first place.

The majority of compilers (or libraries and the targeted systems), in my experience, do not support fflush(stdin) to flush input. It just happens that one or two from large vendors do.

The more common result if fflush(stdin) doesn't work, incidentally, is not a program crash. It is usually intermittent occurrences of strange behaviours in code run after the fflush() call, with results subtly different from what is expected given previous input. Such things tend to be a nightmare to track down, because the symptoms are encountered months after the code is written.

As to the original question: it is usually a good idea to avoid mixing scanf() and getchar() calls, because of interactions like you're seeing between them. Use one or the other, and keep your code behaving consistently.

> check your compiler's documentation and see if you can use it, then, bare in mind that this code is not 100% portable
a) how is the noob supposed to know what is portable, and what isn't. Half the questions here are basically RTFM or STFW questions, so they've already demonstrated that isn't an option.
b) there's no reason to make an unportable solution when a perfectly valid portable one exists.

It's not just the code which needs to be portable, it's also that grey mush between your ears as well. If you think learning a bunch of implementation-specific hacks is hard, try unlearning them when you change compiler (which you will, many times, if you persue a career in programming).

Writing portable answers here means you:
a) don't have to care what compiler they're using,
b) don't have to write disclaimers all over your posts saying "works with the foo compiler" (not that that ever happens, even when it should).

Otherwise, years later, someone bumps the thread with some barely literate "hey, i tried ur cde n it dn wek".

> For that you just use fflush(stdin) after each input line.
Just plain wrong I'm afraid.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351

Hye See...

As usual, there are some exceptions, check your compiler's documentation to see if it has a (non-portable) method for flushing input.
Ya its right that using fflush(stdin) is not 100% correct or what we can say, not a perfect coding. Also, remember one thing there are too may ways of writing a code for the same purpose. Consider an example of for and while loop. They are nearly same but used as the choice of programmer. Same like for some special application we need to write a separate function for flushing input. But if it seems that using fflush(stdin) in this required program doesn't make anything wrong but, gives correct output so its correct...
But for a small & simple program your writing a flush function(which may make code complex unnecessarily/tedious) as your having standard functions available to use. Then use them as they made available for u and if correct to use in your program. Otherwise you have to write a special code to clear the input.

> gives correct output so its correct...
Circular reasoning.
"correct output" just makes you lucky, not good.

> But if it seems that using fflush(stdin) in this required program doesn't make anything wrong
Only the requirement to use a specific compiler.
Like I said, your career will outstrip the lifetime of any single compiler.

If you want to learn a bunch of cheap tricks which "work for me", then go ahead, knock yourself out. But if you post it here as your "best advice", then expect people to crawl all over it and point out the problems with it.

In a 100 line program, you're going to get away with it.

In a 100,000+ line program, every time I've seen something of this scale ported to a new compiler, it's always a disaster area. All manner of sloppy "works for me" attitude all of a sudden generates errors by the boat-load. And even when you've got past all the compilation errors, there's another wave of far harder to find run-time problems just waiting to make a mess of your day.

> But for a small & simple program your writing a flush function(which may make code complex unnecessarily/tedious)
How is a function called "myflush", with ahamed's code in post #3 a vast burden?
It's 2 simple lines, and a function call, and your code is golden.

commented: Absolutely! +15

after entering an integer the program leaves the '\n' in the keyboard buffer. When the next getchar() is called it gets that '\n' from the buffer instead of waiting for user input. To correct this add getchar() after scanf("%d", &consump);

See the problem is due to the 'New line' character...

Gee, didn't AD already say all this? :icon_rolleyes:

Only plain wrong if you're not using a compiler that has defined it.

:icon_rolleyes:

As usual, there are some exceptions, check your compiler's documentation to see if it has a (non-portable) method for flushing input.
Ya its right that using fflush(stdin) is not 100% correct or what we can say, not a perfect coding.

"OK, I used your suggestion on my Lame-C++ compiler and my computer crashed! All my data is now gone! Maybe you should have told me it only works in your compiler! :icon_mad:"

Also, remember one thing there are too may ways of writing a code for the same purpose. Consider an example of for and while loop. They are nearly same but used as the choice of programmer.

Not to the professional -- or someone that understands how to code. They are both different and should be used for the appropriate job.

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.