Switching Between Two or More Fields

First, let me give you a sample C program to ask you what I want to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
void main() {

	int rollNo = 0;
	char stdName[26];
	char section[2];

	clrscr();

	printf("Roll No.: ");
	flushall();
	scanf("%d", &rollNo);

	printf("Section [A, B, or C]: ");
	flushall();
	scanf("%c",&section[0]);
	section[1] = '\0';

	printf("Student's Name:" );
	flushall();
	scanf("%s",stdName);

	stdName[25] = '\0';

	

	getch();
}



Now when we run the above program it works fine. But, well, suppose you have provided an input A or B or for the field Section. And you have pressed the Enter / Return key which takes you on to the next field which prompts for the Student's Name. But then you realize your mistake that you had to type in A but you typed C instead. Now how can you move back to the previous field or even to the top one or back to the bottom one?

So, the problem is how to move back and forth from one field to another when you are supplying values for a number of variables and arrays or just suppose they are contained in a big structure...? You want to make the program userfriendly, so, how can you do that?
You need to write a lot of code. Depending on how sophisticated you want your interface to be. You could use a while loop around the section and name input code, then verify the information before exiting the loop or else it asks again. By the way, you are storing your information in c-style strings (character arrays) and there is nothing stopping the user from overrunning the size of your arrays... they can enter a 50 character name, for example. This is a serious bug.

~psault
Or you could put every field into a function and at the end of every function put someting like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
secondfield()
{
//...
char answer;
do
{
cout<<"[1] Go back to the previous field\n[2] Go on to the next field";
cin>>answer;
if (answer=='1')
firstfield(); //go back to the first field
else if (answer=='2')
tirdfield(); //go on to next field
else
cout<<"Not an option!";
} while (answer!='1' && answer!='2')
}


Sorry that its c++ style, but i guess you can easily translate this into c
Last edited on
Personally. I would put a GUI ontop of your console application. You'll notice most console apps do not provide the functionality you are asking for because of the difficulty required in doing so.

The easiest way is to have them answer all questions. Then ask them if they wish to confirm, or amend what they have entered. If they pick amend, go back through all the options using what they entered previously as the default.
Topic archived. No new replies allowed.