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

Re: how do you go back to the previous line if user enters a wrong input 80 80

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
Re: how do you go back to the previous line if user enters a wrong input 80 80

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.

Re: how do you go back to the previous line if user enters a wrong input 80 80
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

Re: how do you go back to the previous line if user enters a wrong input 80 80

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.

Re: how do you go back to the previous line if user enters a wrong input 80 80

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
Re: how do you go back to the previous line if user enters a wrong input 80 80
do{
   year = getInput();
   if(year < 0 || year > 2012)
      inputIsGood = 1;
   else
      inputIsGood = 0;
}while(inputIsGood);
Re: how do you go back to the previous line if user enters a wrong input 80 80

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

Re: how do you go back to the previous line if user enters a wrong input 80 80

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
Re: how do you go back to the previous line if user enters a wrong input 80 80

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?

Re: how do you go back to the previous line if user enters a wrong input 80 80

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.

Re: how do you go back to the previous line if user enters a wrong input 80 80

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:

Re: how do you go back to the previous line if user enters a wrong input 80 80

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. ;)

Re: how do you go back to the previous line if user enters a wrong input 80 80

>> kingsonprisonic.

???

Re: how do you go back to the previous line if user enters a wrong input 80 80

>> 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?

Re: how do you go back to the previous line if user enters a wrong input 80 80

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.

Re: how do you go back to the previous line if user enters a wrong input 80 80

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:

Re: how do you go back to the previous line if user enters a wrong input 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.