Thread: Going Back to a Previous Field

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    36

    Lightbulb Going Back to a Previous Field

    Well, when we have two or more instances of the scanf() or of the gets() functions, at any level we may need to go back to a previous one at run time to edit what we entered before as we do it on any Windows Form or Webpage Form. So, how to do that in C?


    Consider the following codes segment:

    Code:
    printf("Name: ");
    scanf("%s", userName);
    
    printf("E-Mail: ");
    scanf("%s", eMail);
    
    printf("Phone: ");
    scanf("%ld", &phone);


    Suppose that at run time the user has already entered a value for Name and now is entering a value for E-Mail and presses the Enter/Return key to enter a value for Phone, and then decides to go back to correct the value entered for Name or for E-Mail because it is misspelt.

    So, how can we provide such a user friendly functionality in a C program?

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You can't. At least, not with a console application.

    You could ask your user if the information he entered is correct before processing.
    I hate real numbers.

  3. #3
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    enum field = {
      NAME = 0,
      EMAIL,
      PHONE,
      ADDRESS_LINE1,
      ADDRESS_LINE2,
      END
    };
    
    const char *field_names[] =
    {
      "Name",
      "E-Mail",
      "Phone Number",
      "Address (line 1)",
      "Address (line 2)",
    };
    
    int main(void)
    {
      char fields[END][500];
      enum field field;
      int done = 0;
    
      while(!done)
      {
        for(field = NAME; field < END; field++)
        {
          printf("Please input your &#37;d (or B to go back): ", field_names[fields]);
          fgets(fields[field], 500, stdin);
          strtok(fields[field], "\n"); // ditch the trailing \n char.
    
          if(strcmp(fields[field], "B");
            field -= 2;
    
          if(fields < NAME)
            fields = NAME;
        }
    
        puts("You entered the following:");
    
        for(field = NAME; field < END; field++)
          printf(fields[field]);
    
        fprintf("Is this correct? (Y/N)", stdout);
    
        done = (toupper(getchar()) == 'Y');
      }
    
      // NOW do your parsing with those lines
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    36
    Thanks for your help! But this program has got many errors when I compile it. You might have forgotten to try it.


    Quote Originally Posted by c++0x View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    enum field = {
      NAME = 0,
      EMAIL,
      PHONE,
      ADDRESS_LINE1,
      ADDRESS_LINE2,
      END
    };
    
    const char *field_names[] =
    {
      "Name",
      "E-Mail",
      "Phone Number",
      "Address (line 1)",
      "Address (line 2)",
    };
    
    int main(void)
    {
      char fields[END][500];
      enum field field;
      int done = 0;
    
      while(!done)
      {
        for(field = NAME; field < END; field++)
        {
          printf("Please input your %d (or B to go back): ", field_names[fields]);
          fgets(fields[field], 500, stdin);
          strtok(fields[field], "\n"); // ditch the trailing \n char.
    
          if(strcmp(fields[field], "B");
            field -= 2;
    
          if(fields < NAME)
            fields = NAME;
        }
    
        puts("You entered the following:");
    
        for(field = NAME; field < END; field++)
          printf(fields[field]);
    
        fprintf("Is this correct? (Y/N)", stdout);
    
        done = (toupper(getchar()) == 'Y');
      }
    
      // NOW do your parsing with those lines
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Solicit feedback from the user and based on it either process the next step or loopback over the choices presented to the user.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We had on-screen field editing in DOS - we had windows also, in DOS. It wasn't as polished as MS's Windows, but we had 'em.

    One that's pretty easy and handles the problem OK in general, is the aforementioned loop back technique. ie., in my Sudoku program, there is a way for the user to manually set up the puzzle's initial squares. It's easy to make an error though when a common puzzle is given as an 81 digit string: 00020000300402006001050007008, etc.

    So after initially having the user either press enter for an empty square, or enter a value, it prompts whether everything looks right on the display board? If not, another function asks them what row and column number square they would like to change? Prompts for the new value are given, and again, the program asks if they'd like to edit another square [y/n] ? This continues until they answer no, they don't want to edit another square.

    That's one reason the header file "conio.h" (or ncurses) was great to have. That made on-screen field code, quite do-able, including going backward through the fields.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Console Font Size
    By bradszy in forum Windows Programming
    Replies: 34
    Last Post: 04-26-2008, 07:09 AM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM