how can i go back to the previous line if user enters a wrong input, im using C programming

Recommended Answers

All 17 Replies

If you need that kind of control over the user experience, I'd say write a GUI-based program instead of a console program.

commented: Terrible suggestion! GIU is not a solution. -4
commented: Its a bad solution. -1

If user enters wrong data then display an appropriate error message then re-display the original question. Best way to do that is to put all that in a loop and break out of the loop only when user enters correct data.

char inputIsGood = 1;//<== this should be any number, NOT 0
do{
   input = getInput();
   if(intput == 9)
      inputIsGood = 0;
}while(inputIsGood);

this is an example,. there are many ways,
not sure what type of imput u r trying to get,
however, use if statement, and if you get the right input, then set inputIsGood to 0 will make you exit the loop
if input is not what u like, then this will keep looping

ok for example if i want to i want user to enter present year, and the user enter number that is less than zero, or year=0 , so i want to re-execute the enter the present year line, ive been trying but it didnt work.

Create a loop.
If they enter an invalid value, stay in the loop.
If they enter a valid value, exit the loop.

commented: Not what the OP asked for. -1
do{
   year = getInput();
   if(year < 0 || year > 2012)
      inputIsGood = 1;
   else
      inputIsGood = 0;
}while(inputIsGood);

i dont think you understand anything we reply here.
which mean you cant get what loop mean,

its the only way to go back,
and if you dont like this, then you should't stay here, expecting another answer

I think goto Statement will solve your problem..

Like

here:

printf("Enter a number");
scanf("%d",&num);
if (num<10) // Checking whether the input is correct. In this case >=10
{
printf("Sorry Wrong Input\nPress any key to input it again!");
getch();
clrscr();
goto here;
}
commented: Faking a loop with goto is very rarely the right thing to do. -1

Since two people have deigned to give me negative rep, I'll clarify my previous post.

It's impossible to do what the OP asked without taking full control over the console with something like curses, or a manual solution using some variant of gotoxy() or clrscr(). If that kind of control is needed, it's probably wise to consider a GUI instead of a console program in this day and age since you're going to hurt portability regardless of how the goal is accomplished. Why? Because once you accomplish jumping back by one line, you'll probably discover other irritants of the console's lack of flexibility and need to hack in more "fixes".

Not to mention that overriding how the console works is confusing to users, while GUIs are much more intuitive when it comes to non-linear non-sequential I/O.

That's not to say a GUI should be used if a you can live with default console behavior. In that case, a loop and reprint is sufficient. But if that were sufficient, would the OP have asked how to jump back one line?

The negative rep probably was for your use of the goto statement instead of the more standard loop. The code you posted does not even attempt to do what you just described.

a lot of guys got negative rep from someone, I really dont know why. plus I dont know if the guy who need this is even looking at solutions in here :yawn:

The negative rep probably was for your use of the goto statement instead of the more standard loop. The code you posted does not even attempt to do what you just described.

If you're replying to me, I'm not kingsonprisonic. ;)

>> kingsonprisonic.

???

>> kingsonprisonic.

???

The guy who posted using goto, once again assuming you're replying to me. Could you be more clear with whom you're replying to and why, please?

The guy who posted using goto, once again assuming you're replying to me. Could you be more clear with whom you're replying to and why, please?

I was replying to your post that has 2 negative reps. But after re-reading this thread I realize you did not post the goto statement and I apologize for accusing you of doing that.

>>Since two people have deigned to give me negative rep, I

You don't have two negative reps, the poster of the goto code has it.

You got neg rep from me because you are telling a student uncomfortable with
1) programming -- he's NEW!!
2) English -- he's from Malaysia
to create a GUI app. He just want's to know how to ask a question again when an improper answer was entered, not to take over the screen and bounce all around it!
Sheesh! :icon_rolleyes:

You got neg rep from me because you are telling a student uncomfortable with
1) programming -- he's NEW!!
2) English -- he's from Malaysia

I'm sorry, I didn't realize I was supposed to read his mind and determine that what he asked for wasn't what he really wanted. :icon_rolleyes:

He just want's to know how to ask a question again when an improper answer was entered, not to take over the screen and bounce all around it!

Read the first post again. It's not clear that's what he wanted to know, and what he *asked* for was how to move the input cursor to the previous line. Maybe you should take such things into account before whipping out your bad attitude.

inb4 more negative rep from WaltP.

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.