Thread: Please help, struct problem.

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    21

    Please help, struct problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NAMESIZE 20
    #define STD 36
    typedef struct
    {
    	char studentName[NAMESIZE];
    	int classNumber;
    	int gradeOne;
    	int gradeTwo;
    	int gradeYear;
    }students;
    
    
    typedef students classes[STD];  // This is type of 
    
    
    
    
    void scanStudent(classes s);  //CLASSES 
    int main()
    {
    	classes cls;
    	scanStudent(cls);
    }
    void scanStudent(classes cls)   //TRANSFER CLASSES
    {
    	int;
    	for (int i = 0; i < STD; i++)
    	{
    
    
    		printf("Please enter student name\n");
    		gets(cls[i].studentName);
    		printf("Please enter the CLASS NUMBER in range between 5-1 *****\n");
    		_flushall();
    		do
    		{
    			scanf("%d", &cls[i].classNumber);
    		} while (cls[i].classNumber>5 || cls[i].classNumber<0);
    		printf("Please enter the grade of first semester\n");
    		_flushall();
    		do
    		{
    			scanf("%d",&cls[i].gradeOne);
    			if (cls[i].gradeOne > 100 || cls[i].gradeOne < 0)
    				printf("Wrong value please reenter\n");
    		} while (cls[i].gradeOne> 100 || cls[i].gradeOne < 0);
    		_flushall();
    		printf("Please enter the grade of Second semester\n");
    		do
    		{
    			scanf("%d",&cls[i].gradeTwo);
    			if (cls[i].gradeTwo>100 || cls[i].gradeTwo < 0)
    			printf("Wrong value please reenter\n");
    		} while (cls[i].gradeTwo>100 || cls[i].gradeTwo < 0);
    
    
    
    
    		cls[i].gradeYear = (cls[i].gradeOne + cls[i].gradeTwo) / 2;
    	}
    
    
    }
    The first scan works perfect on the next scan its skips name and scan continue to class number, can't find a problem in debugger please need advise thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first problem is you're using gets()
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Perhaps sprinkle in a few more _flushall(); calls.
    It's the wrong way to go about it, but hey, if it works for you, go for it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct struct struct problem....please....help!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  2. problem with struct
    By nevrax in forum C Programming
    Replies: 13
    Last Post: 05-02-2007, 11:38 AM
  3. Struct problem
    By chr15 in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 02:37 PM
  4. Help with a Struct problem :)
    By yrostran in forum C Programming
    Replies: 11
    Last Post: 05-09-2006, 02:14 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread