Getchar & Structure Problems

This is a discussion on Getchar & Structure Problems within the C Programming forums, part of the General Programming Boards category; Hi, having a problem with something I am trying to do in C. This is the code I have: #include ...

  1. #1
    Unregistered
    Guest

    Question Getchar & Structure Problems

    Hi, having a problem with something I am trying to do in C. This is the code I have:

    #include <stdio.h>

    int main()
    {

    #define MAX 10

    int i;

    struct list_details
    {
    char make[45];
    char model[45];
    } carList[10];

    for(i=0; i < MAX;i++)
    {
    printf("Enter the make : \n");
    scanf("%s", &List[i].make);
    printf("Enter the model : \n");
    scanf(%s", &List[i].model);
    }

    return 0;
    }


    This keeps asking for a make and model for up to 10 times, however I want it so that you can input up to 10 times but can also quit before if needed. E.g. if you only want to put 3 records in then you can quit after entering the third record. I have been trying this using getchar() but just can't seem to get it to work.

    Please help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > . E.g. if you only want to put 3 records in then you can quit after entering the third record.
    You need to build this test into your code

    Eg.
    Code:
    for(i=0; i < MAX;i++)  { 
       printf("Enter the make (or QUIT) : \n"); 
       scanf("%s", List[i].make);  // & not needed for char arrays
       if ( strcmp(List[i].make,"QUIT") == 0 ) break;   // all done
       printf("Enter the model : \n"); 
       scanf(%s", List[i].model); 
    }
    Also
    Code:
    struct list_details { 
    char make[45]; 
    char model[45]; 
    } carList[MAX];   // use the #define

  3. #3
    Unregistered
    Guest

    Cool

    Thanks a lot!, that works just great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 10:20 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  3. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 01:49 PM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 05:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21