Thread: Structure, user input and printing out results

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Structure, user input and printing out results

    Hello

    well, I am trying to build a structure, on a self learning purpose, and while I know other primitive ways of filling the structures, I want it this way. Actually that the user is prompted to enter himself and then it prints as it goes along. However, it does nothing after the scanf.

    Of course, the code needs to be more evolved, so that we can get to fill as many structures as we want, such as putting them in a loop and asking the user how many he wants offered etc and also when to call it off, but so far I need to get the grasps of this.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct MiEstructura  // JUST DEFINING THE STRUCTURE
    {
     char FirstName[20];
     char LastName[30];
     char * Address[50]; // THIS WOULD ACTUALLY BE AN ARRAY OF 50 POINTERS TO TYPE CHAR, NOT CORRECT THEN...
     int  MyAge;        
    };
     
     
     struct MiEstructura Instancia[2]; // NOW 2 INSTANCES OF IT
    
     struct MiEstructura *ptr;
     int main () 
     {
          int i;
         
                   ptr = &Instancia[0];
    
                    printf("Enter First Name \n");// nothing happens after I enter a name
                    scanf("%s\n",ptr->FirstName);
                    printf("%s\n",ptr->FirstName);
    
                     printf("Enter Last Name \n");
                    scanf("%s\n",ptr->LastName);
                    printf("%s\n",ptr->LastName);
            
                    printf("Enter Address \n");
                    scanf("%s\n",ptr->Address);
                    printf("%s\n",ptr->Address);
                    
                
          getchar();
          
          return 0; 
                
           
      
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    |In function 'main':|
    |32|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char **' [-Wformat]|
    |33|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char **' [-Wformat]|
    |19|warning: unused variable 'i' [-Wunused-variable]|
    Correct these for starters.
    Devoted my life to programming...

  3. #3
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Get ride of the \n in your scanf's.


    Code:
    scanf("%s\n",ptr->FirstName);
    to...

    Code:
    scanf("%s",ptr->FirstName);

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    18
    Thank you very much "rmatze", and "Greaper" I appreciate the time you took. Definitively that did the trick. Now I ll continue on to my next step.

    best regards

    Al

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd results on user input array
    By deadrabbit in forum C Programming
    Replies: 4
    Last Post: 08-15-2011, 07:59 PM
  2. Printing a Solid Square based on user input
    By ThatBoy1124 in forum C Programming
    Replies: 3
    Last Post: 03-01-2011, 10:07 PM
  3. Printing asterisks using user input
    By celticpride in forum C Programming
    Replies: 4
    Last Post: 02-10-2011, 09:43 PM
  4. Setting a user's input as the structure name
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 05:23 AM
  5. Replies: 3
    Last Post: 11-05-2001, 05:00 PM

Tags for this Thread