Thread: Problem with do loop in for in my structure in structure program

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    Problem with do loop in for in my structure in structure program

    it doesn't give me syntax error but compile error... i notice it's from my for loop "specifically the do to input a name " i want to know how i can fix this problem in my do if anyone can help me..

    Code:
    #include <stdio.h>
    // This program is a structure which in structure task im doing in class
    //THe problem i see in this program lies which in the do loop
    //this program suppose to record the names of people and their date, and time..
    
    
       /*structure of date*/
         struct date
    	{
    		int year;
    		int month;
    		int day; 
         };
    	/*structure of time*/
        struct time
    	{  int hour;
    	   int minute;
    	   int second;
        };
        /*structure of the records*/
        struct record
        { char Names[30];
        };
        
        /*structures with structures inside of it*/
        struct info
        { struct date record1[30];
          struct time record2[30];
          struct record people[30];
        };
    
          int main(void)
      {
          struct info event;
          int temp,num,i,x=0;
          char letter;
    	  printf("How many people would u wish to log?\n");
                           scanf("%d",&num);
                 if( num > 30){
                      printf("ERROR\n");
                      printf("How many people would u wish to log?\n");
                           scanf("%d",&num); }
                 for( i = 0; i < num; ++ i) 
                 {
    
                    /* the do is my primary problem in this program*/
                     do
                       {    
                            printf("Enter  name\n");
                            letter = getchar();
                            event.people[i].Names[x] = letter;
                            ++x;
                       }while(letter != '\n');
                            
                            event.people[i].Names[x-1] = '\0';
                            
                     printf("Enter year\n");
                             scanf("%d",&temp);
                     
                     event.record1[i].year = temp;
                     
                     printf("Enter month\n");
                             scanf("%d",&temp);
                     
                     event.record1[i].month = temp;
                     
                     printf("Enter day\n");
                              scanf("%d",&temp);
                     
                     
                     event.record1[i].day = temp;
                
                
                 printf("------------------------------\n");
                
                
                 printf("NOW ENTER THE hour!!!!!\n");
                             scanf("%d",&temp);
                     event.record2[i].hour = temp;
                 
                 
                 
                 printf("ENTER MINUTES!!!!!!\n");
                               scanf("%d",&temp);
                 
                  event.record2[i].minute = temp;
                 
                 
                 printf("ENTER SECONDS!!!!\n");
                              
                               scanf("%d",&temp);
                 
                 event.record2[i].second = temp; 
    			 
           printf("%s",event.people[i].Names);
         
        }
                
                getchar();
    }

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What is the error?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    it doesn't print out the words i input from getchar, and instead and prints out the printf("Enter world"); and also it print's enter world a certain nubmer of types when u enter a number after it askes u to log....

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Darkinyuasha1
    it doesn't print out the words i input from getchar, and instead and prints out the printf("Enter world"); and also it print's enter world a certain nubmer of types when u enter a number after it askes u to log....
    getchar() functionn returns an int. Better change the letter to int type. If u wanted to read a string why dont u use fgets function rather than looping around manking it messy

    Code:
    char str[10];
    
    fgets(str,strlen(str),stdin);  // this would read the string from the key board and store that in the str
    ssharish2005

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    As for your original code, it would help to set x to zero right before the do-while loop.

    What happens if the user enters a number > 30 twice in a row for the number of people to log? A loop is in order here.

    Rather than doing this all the time:
    Code:
                             scanf("%d",&temp);
                     
                     event.record1[i].year = temp;
    you can bypass temp and pass the address of event.record1[i].year directly to scanf():
    Code:
                             scanf("%d",&event.record1[i].year);

    BTW,
    Code:
    printf("Enter year\n");
    could be written as
    Code:
    puts("Enter year");
    http://www.cprogramming.com/fod/puts.html

    Code:
    char str[10];
    
    fgets(str,strlen(str),stdin);  // this would read the string from the key board and store that in the str
    That is a very bad idea -- using strlen() on an uninitialized string. I'm guessing you meant sizeof().
    Code:
    char str[10];
    
    fgets(str,sizeof(str),stdin);  // this would read the string from the key board and store that in the str
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  4. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM
  5. loop problem in program
    By Wjahvces in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2001, 04:01 AM