A mechanical engineer confused if IF ELSE statement can do this

Hi

i have confusion. i am writing a C program for a quiz. i wanna know if we can give the user 2 tries to enter the answer. here is the simple program. please have a look.

#include 
int main()
{
	char ans, ans2;
	printf("What is the equivalent value of 0 Farenheit in Celcius?\na.) 0\nb.) -40\nc.) -17.78\n");
	scanf("%c",&ans);
	
	if (ans=='c')
	{
		printf("You are Smart!!!!!\n");
		
	}
	else
	{
		printf("You are Dumb!!!\n");
		printf("Give it another try\n");
		scanf("%c",&ans2);
		
		if (ans2=='c')
		{
			printf("Thank God!!!!......Finally you are correct\n");
		}
		else
		{
			printf("you are the Dumbest person on Earth\n");
		}}
	return 0;
}

Replies

  • HirenBarbhaya
    HirenBarbhaya
    I am little confused about the question itself...

    If you wanna give second try then what you have written is correct... Seems fine to me... nothing wrong in it..
  • Manish Goyal
    Manish Goyal
    very nice question vipan. Just add this function "flushall()" after else .

    Here is the reason when compiler encounters first scanf("%c",&ans);
    you will press say 'a' and then press enter ,compiler will consider your input as "a\n" ,thus here second scanf will take "\n" as its input and hence it will not wait for second input, if you put answer wrong ,so in order to ensure fine working of program ,you have to clear your input buffer and for that use flushall().
  • vipandeep
    vipandeep
    Thanks for the answers.

    I am using Xcode in mac. when i added flushall() it comes up with an error "warning: implicit declaration of function 'flushall'
    I dont trust Xcode because it shows same warning for system("PAUSE").
    any idea?
  • Leo
    Leo
    Hi Vipin try using fflush(stdin) or you can create your own flush function like this,

    void flush()
    {
    char *str;
    str=(stdin);
    return;
    }

    And system("PAUSE") is not error, pause is not system command for mac it is system command for windows. Xcode is perfectly fine standard compiler, you need little practice to differentiate between Linux, UNIX and Windows system commands, thats it nothing else.
  • vipandeep
    vipandeep
    Thanks Leo.... fflush(stdin) works well 😀

You are reading an archived discussion.

Related Posts

I've often noticed during my cardio workouts that the cycle needs external supply. I was wondering whether it would be possible to cut down the power supply and make the...
We've reviewed the Google's latest Nexus Phone "Nexus S" through VoiCE. Give it a look: Google Nexus S Price In India, Launch Date & Reviews - CrazyEngineers
Toyota have unveiled a new robot that can play the violin. But not particularly well. See their video: [video=youtube;EzjkBwZtxp4]https://www.youtube.com/watch?v=EzjkBwZtxp4&feature=fvwk[/video]
Researchers at the Max Planck Institute for Biological Cybernetics, in Tübingen, Germany, transformed an industrial robot arm into a Formula 1 simulator. [video=youtube;oMLarxR-q08]https://www.youtube.com/watch?v=oMLarxR-q08&playnext=1&list=PL4DFEDAFF4A908BAE&index=15[/video] The video above shows what it's like...
Thank an Engineer: Fun video that spoofs what the world would be like without engineers. Texas Instruments sends a shout out to today's engineers, thanking them for their ingenuity and...