execute the program further only after user hits ENTER

0 Gurpinder Singh · May 16, 2015
Hi Guys, 

I am just a beginner and i was making a simple program (a quiz).I want that when user answer 1 question and if its right  then the prompt will say "CONGRATULATIONS ! LETS GO AHEAD".
But when user gives the right answer what my program do is ..it says "Congratulations! LETS GO AHEAD" but then then it doesn't pause for user to hit ENTER and move to next question.
it just shows "Congratulations" and the next question all in once.


Following is the code! please spare some time and help me.



#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int main()
{
    int i;
    printf("WELCOME TO KON BANEGA CROREPATI\n");
    for (i=1 ; i<1000000000 ; i++) ; //*i have entered this for program to wait for sometime before executing the next instruction*//.
    printf("\n\nEvery question is of 10 lakh!!\n\n");
        for (i=1 ; i<100000000 ; i++) ;

    printf(" \n lets start!      \n");
    for (i=1 ; i<100000000 ; i++) ;
    printf("Press Enter to start...\n");
    scanf("/");//this worked here!,it paused the programmed and it waited for user to hit ENTER.

    printf("When did Maharaja ranjit singh became a king?\n");
    printf( "1. 1707\n");
    printf( "2. 1801\n");
    printf( "3. 1790\n");
    printf( "4. 1809\n");
    scanf(" %d",&i);

    if(i==2){
        printf("Congratulations! LETS GO AHEAD\n\n");
            for (i=1 ; i<1000000000 ; i++) ;
         printf("Now lets move to the next question! PRESS [ENTER] to continue...\n");
        getchar();       //* neither Scanf("/")  work here nor getchar()*//
    }else{
    printf("Your Journey ends here please try again!");}



    printf("Which city is know as city beautiful\n");
    printf( "1. CHANDIGARH\n");
    printf( "2. LUDHIANA\n");
    printf( "3. MUKATSAR\n");
    printf( "4. PATIALA\n");
    scanf(" %d",&i);
        if(i==1){
        printf("Congratulations!... its the right answer you have won 20 lakh \n\n");
        }


    }

Post a Reply

Replies

Oldest  Newest  Rating
0 Milan Todorovic · May 16, 2015
This is weird. When I put getchar(); and scanf("/"); in that order, it worked.
0 c student · May 17, 2015
your code seems to be quite confusing... you've placed in all of these for loops which increment to this arbitrarily large number
for (i=1 ; i < 100000000 ; i++) ;

what's the purpose of this?

if you wanted a user to press the enter character, that would imply a newline character

while (getchar() != '\n')    /* loop until user has pressed ENTER key */
0 Superman HC · May 18, 2015
Actually the getchar() will work if we don't mess up with the input stream using scanf.. that means the getchar would not work after we have used scanf in the program... I will surely try to find the solution.. till then the temporary solution is to use getch(); and ask the user to press any key to continue.. I m attaching the code.. its working..

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int main()
{
int i;
printf("WELCOME TO KON BANEGA CROREPATI\n");
for (i=1 ; i<1000000000 ; i++) ; //*i have entered this for program to wait for sometime before executing the next instruction*//.
printf("\n\nEvery question is of 10 lakh!!\n\n");
for (i=1 ; i<100000000 ; i++) ;

printf(" \n lets start! \n");
for (i=1 ; i<100000000 ; i++) ;
printf("Press Enter to start...\n");
getchar();//this worked here!,it paused the programmed and it waited for user to hit ENTER.

printf("When did Maharaja ranjit singh became a king?\n");
printf( "1. 1707\n");
printf( "2. 1801\n");
printf( "3. 1790\n");
printf( "4. 1809\n");
scanf(" %d",&i);

if(i==2){
printf("Congratulations! LETS GO AHEAD\n\n");
}
else{
printf("Your Journey ends here please try again!");
}

for (i=1 ; i<1000000000 ; i++);
printf("Now lets move to the next question! PRESS [ENTER] to continue...\n");
getch(); //* neither Scanf("/") work here nor getchar()*//

printf("Which city is know as city beautiful\n");
printf( "1. CHANDIGARH\n");
printf( "2. LUDHIANA\n");
printf( "3. MUKATSAR\n");
printf( "4. PATIALA\n");
scanf(" %d",&i);
if(i==1){
printf("Congratulations!... its the right answer you have won 20 lakh \n\n");
}
for (i=1 ; i<100000000 ; i++) ;
printf("Congrats right answer... press enter\n");
getch();


}
0 c student · May 18, 2015
Have you tried debugging your program?  If you step slowly through and pay attention to detail you'll realise that your call to scanf()
scanf(" %d",&i);

takes in a number (e.g. 42) and leaves everything else as is.  Here is your initial input from the keyboard into scanf():
42\n    /* 42 plus the enter key */

Your scanf() will read the 42 and leave the stdin as is, which will now be:
\n

When you call your next getchar(), it will read the next character from stdin which means it'll automatically read in the newline character and continue the rest of the program.

To fix this, simply modify what your scanf() reads so that it suppresses everything up to and including the newline character:
scanf ("%d%*[^\n]", &i);
  • 1

C

107,111 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator