Thread: weird problem with gets();...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    weird problem with gets();...

    the problem is that when i only put 1 gets(); in the "Enter Student Number' portion,
    it skips right away and proceeds to getting the Students name:
    u can run it if ud like to see the problem....
    P.S.... the for loop to fine empty slot,.... i dont know what im doing there ! xD haha sorry

    Code:
    void add_student(student accounts[20], FILE *file)
    {
    	int free;
    	int i;
    
    	for(i = 0; i < 20; i++)
    	{
    		if(accounts[i].stud_num == NULL)
    		{
    			free = i;
    		}
    	}
    
    	printf("--------------------------------------------------------");
    	printf("\n\nEnter student information:\n\n");
    
    	printf("\nStudent Number [which will be the accounts username]:\n\n");
    	gets(accounts[free].stud_num);  //i dont know why putting 2 gets(); here works, while only 1 doesnt...
    	gets(accounts[free].stud_num);
    	fprintf(file, "%s", accounts[free].stud_num);
    	fputs("\n", file);
    
    	printf("\nName:\n\n");
    	gets(accounts[free].name);
    	fprintf(file, "%s", accounts[free].name);
    	fputs("\n", file);
    
    	printf("\nNickname:\n\n");
    	gets(accounts[free].nick);
    	fprintf(file, "%s", accounts[free].nick);
    	fputs("\n", file);
    
    	printf("\nBirthdate:\n\n");
    	gets(accounts[free].bday);
    	fprintf(file, "%s", accounts[free].bday);
    	fputs("\n", file);
    
    	printf("\nCourse:\n\n");
    	gets(accounts[free].course);
    	fprintf(file, "%s", accounts[free].course);
    	fputs("\n", file);
    
    	printf("\n\nAccount creation successful.\n");
    }

  2. #2
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.(or maybe Lopado..temakho..selakho..galeo..kranio..leipsano. .drim..hypo..trimmato..silphio..karabo..melito..ka takekhy..meno..kikhl..epi..kossypho..phatto..peris ter..alektryon..opto..kephallio..kigklo..peleio..l agōio..siraio..baphē..tragano..pterýgōn?)
    As demonstrated by the deeds of the Great Worm, a consequence of this commandment is that robust production software should never make use of gets(), for it is truly a tool of the Devil. Thy interfaces should always inform thy servants of the bounds of thy arrays, and servants who spurn such advice or quietly fail to follow it should be dispatched forthwith to the Land Of Rm, where they can do no further harm to thee.

    Stdio
    Last edited by creeping death; 03-29-2009 at 11:09 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Huskar View Post
    the problem is that when i only put 1 gets(); in the "Enter Student Number' portion,
    it skips right away and proceeds to getting the Students name:
    try using fgets instead of gets.gets might be causing buffer overrun.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Problem With Pointer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 07:50 PM
  2. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  5. Weird class problem!
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 06:12 AM