Hello, there.
I'm trying to write a program which reads student information from keyboard and does some calculations. I've just started it and I dont know much about this "Struct" topic. As far as I know, I've tried to do something and here is the result.

#include <stdio.h>
#define N 2
#define ISIMUZUN 15

  struct student{
  char name[ISIMUZUN];
  char surname[ISIMUZUN];
  char number[10];
  int final;
  int midterm;
  int hw1;
  int hw2;
                };
int main(int argc, char *argv[])
{
  struct student i;

   for(i=0; i<N; i++)

   { printf("Please enter student number %d information with blanks between them\n", i+1);
     scanf( "%s %s %s %d %d %d %d", i.number, i.name, i.surname, i.midterm, i.final, i.hw1, i.hw2   );

     printf("%s %s %s %d %d %d %d\n", i.number, i.name[ISIMUZUN], i.surname[ISIMUZUN], i.midterm, i.final, i.hw1, i.hw2 );
      }
     system("pause");
  return 0;
}

I'm getting several errors while compiling this. Does anyone have an idea about whats wrong with it?
Regards..

Recommended Answers

All 15 Replies

Hello, there.
I'm trying to write a program which reads student information from keyboard and does some calculations. I've just started it and I dont know much about this "Struct" topic. As far as I know, I've tried to do something and here is the result.

#include <stdio.h>
#define N 2
#define ISIMUZUN 15

  struct student{
  char name[ISIMUZUN];
  char surname[ISIMUZUN];
  char number[10];
  int final;
  int midterm;
  int hw1;
  int hw2;
                };
int main(int argc, char *argv[])
{
  struct student i;

   for(i=0; i<N; i++)

   { printf("Please enter student number %d information with blanks between them\n", i+1);
     scanf( "%s %s %s %d %d %d %d", i.number, i.name, i.surname, i.midterm, i.final, i.hw1, i.hw2   );

     printf("%s %s %s %d %d %d %d\n", i.number, i.name[ISIMUZUN], i.surname[ISIMUZUN], i.midterm, i.final, i.hw1, i.hw2 );
      }
     system("pause");
  return 0;
}

I'm getting several errors while compiling this. Does anyone have an idea about whats wrong with it?
Regards..

Why are you using your struct variable as a counter in your for loop?

Why are you using your struct variable as a counter in your for loop?

I couldn't figure out how to do it with multiple students. For example, for 6 students how can i organize my struct?

struct student 0
....
....
....

struct student 1
....
....
....
.
.
.
struct student 5
....
....
....

That's why I used struct variable inside for loop. I know it is so amateur but i could not figure out how to get what I desire.

I couldn't figure out how to do it with multiple students. For example, for 6 students how can i organize my struct?

struct student 0
....
....
....

struct student 1
....
....
....
.
.
.
struct student 5
....
....
....

That's why I used struct variable inside for loop. I know it is so amateur but i could not figure out how to get what I desire.

If you want an array of structures then define your variable like this:

struct student i[6];

Here's a link on structures in C

http://www.exforsys.com/tutorials/c-language/c-structures-and-unions.html

Thank you for your instant replies. I did as you said and altered my code as below:

#include <stdio.h>
#define N 2
#define ISIMUZUN 15

  struct student{
  char name[ISIMUZUN];
  char surname[ISIMUZUN];
  int number;
  int final;
  int midterm;
  int hw1;
  int hw2;
                };
int main(int argc, char *argv[])
{
  int i=0;
  int studentcounter=0;
  struct student info[N];

   for(i=0;i<N;i++){
    while (info[i].number != -1){
    studentcounter = studentcounter + 1;
    printf("Please enter student's information with blanks between them\n");
    scanf( "%d %s %s %d %d %d %d", info[i].number, info[i].name, info[i].surname, info[i].midterm, info[i].final, info[i].hw1, info[i].hw2   );
                                }
                    }


     system("pause");
  return 0;
}

But when i entered the first student information my application crashes. I wonder why this is happening?

Your scanf should be reading the data into the address of the structure members..

&info.number

commented: good job. you are all over this. +7

Thank you for your support. I am almost done with my program yet there are some new issues to be solved. My final program looks like this:

#include <stdio.h>
#include <math.h>
#define N 100
#define ISIMUZUN 15

  struct student{
  char name[ISIMUZUN];
  char surname[ISIMUZUN];
  int number;
  int final;
  int midterm;
  int hw1;
  int hw2;
  int grade;
  char letter[3];
                };
int main(int argc, char *argv[])
{

  struct student info[N];
  int i=0, total=0,total2=0;
  float mean=0, stddvt=0;
  int studentcounter=0;

   for(i=0;i<N;i++){
    while (info[i].number != -1){
    studentcounter = studentcounter + 1;
    printf("Please enter student's information with blanks between them\n");
    scanf( "%d %s %s %d %d %d %d", &info[i].number, info[i].name, info[i].surname, &info[i].midterm, &info[i].final, &info[i].hw1, &info[i].hw2   );
    info[i].grade = (info[i].final*(2/5))+ (info[i].midterm*(1/4))+ (info[i].hw1*(3/20)) + (info[i].hw2*(1/5));
                                }
                    }
    for(i=0;i<N;i++){
    if((info[i].midterm==0)&&(info[i].final==0)&&(info[i].hw1==0)&&(info[i].hw2==0))
    studentcounter=studentcounter-1;
    info[i].letter= "VF";
    }

    /* Calculating Mean*/
    for(i=0;i<studentcounter;i++){
    total= total + info[i].grade;
    }
    mean= (total/studentcounter);
    /**********/
    /*Calculating Standart Deviation*/
    for(i=0;i<studentcounter;i++){
    total2= total2 + pow((info[i].grade-mean),2);
                                 }
    stddvt= sqrt(total2/studentcounter);

    /**********/

    /*Assigning Letters*/
    for(i=0;i<studentcounter;i++){
    if(info[i].grade >= (mean + 1.5*stddvt))
    info[i].letter= "AA";

    if(((mean + 1.0*stddvt)<=info[i].grade)&& (info[i].grade < (mean + 1.5*stddvt)))
    info[i].letter= "BA";

    if(((mean + 0.5*stddvt)<=info[i].grade)&& (info[i].grade < (mean + 1.0*stddvt)))
    info[i].letter= "BB";

    if((mean<=info[i].grade)&& (info[i].grade < (mean + 0.5*stddvt)))
    info[i].letter= "CB";

    if(((mean - 0.5*stddvt)<=info[i].grade)&& (info[i].grade < mean))
    info[i].letter= "CC";

    if(((mean - 1.0*stddvt)<=info[i].grade)&& (info[i].grade < (mean - 0.5*stddvt)))
    info[i].letter= "DC";

    if(((mean - 1.5*stddvt)<=info[i].grade)&& (info[i].grade < (mean - 1.0*stddvt)))
    info[i].letter= "DD";

    if(info[i].grade < (mean - 1.5*stddvt))
    info[i].letter= "DD";
                     }
    /**********/



    for(i=0;i<N;i++){
    printf ( "%d %s %s %d %d %d %d", &info[i].number, info[i].name, info[i].surname, &info[i].midterm, &info[i].final, &info[i].hw1, &info[i].hw2   );
    }
     system("pause");
  return 0;
}

I am not sure where i am doing a mistake but when i try to compile my compiler says "incompatible types in assignment" for letters AA BA BB...
And also in the end nothing is written even though i have a printf. Besides these everything is OK i believe.

Your trying to assign a const string pointer to a char [3] with this line which is a no no.

info[i].letter= "DD";

Try something like

info[i].letter[0] = 'A';
info[i].letter[1] = 'A';

Now I'm getting a warning from my compiler "assignment makes integer from a pointer without a cast" for the lines

info[i].letter[0] = 'A';
  
      info[i].letter[1] = 'A';

and B C D and F also. What's more, i can not print the information by this command:

printf ( "%d %s %s %d %d %d %d", &info[i].number, info[i].name, info[i].surname, &info[i].midterm, &info[i].final, &info[i].hw1, &info[i].hw2   );

Where may the problem be?

What's more, i can not print the information by this command:

printf ( "%d %s %s %d %d %d %d", &info[i].number, info[i].name, info[i].surname, &info[i].midterm, &info[i].final, &info[i].hw1, &info[i].hw2   );

Where may the problem be?

printf expects parameters, not addresses to them,- so remove '&' from arguments to printf.

Oooppss... This was so obvious, wasn't it? But this has not been my problem since i've changed my printf line. Nothing is changed. I'm suspecting about my sentinel data "-1". Whenever I entered "-1" from my keyboard my software does nothing. It shows nothing and i'm afraid does nothing. I'm really ashamed to ask so many questions about this but i'm getting the best help from you guys. So if i may, may i ask where the problem is? Thank you very much..

Here's a working copy of part of your program...You really should write your programs in small sections. That way you can get one section working and then move onto another..Note I went with char pointer instead of char array for letter.

Please also note the formating of the program...It much easier to read.
Things like a for statement for(i=0;i<N;i++) with no spaces should be avoided.

#include <stdio.h>
#include <math.h>
#define N 2
#define ISIMUZUN 15

struct student
{
	char name[ISIMUZUN];
	char surname[ISIMUZUN];
	int number;
	int final;
	int midterm;
	int hw1;
	int hw2;
	int grade;
	char *letter;
};

int main(int argc, char *argv[])
{
	struct student info[N];
	int i = 0;

	for(i = 0; i < N; i++)
	{
		printf("Please enter student's information with blanks between them\n");
		scanf( "%d %s %s %d %d %d %d", &info[i].number, \
		info[i].name, info[i].surname, &info[i].midterm, \
		&info[i].final, &info[i].hw1, &info[i].hw2);
		info[i].letter = "AA";
	}


	for(i = 0; i < N; i++)
	{
		printf ( "%d %s %s %d %d %d %d %s\n", info[i].number, \
		info[i].name, info[i].surname, info[i].midterm, \
		info[i].final, info[i].hw1, info[i].hw2, info[i].letter);
	}
	return 0;
}

There is something wrong with this particular code.

for(i=0;i<N;i++){
    while (info[i].number != -1){
    
studentcounter = studentcounter + 1;
    
printf("Please enter student's information with blanks between them\n");
    
scanf( "%d %s %s %d %d %d %d", &info[i].number, info[i].name, info[i].surname, 
&info[i].midterm, &info[i].final, &info[i].hw1, &info[i].hw2   );
    
info[i].grade = (info[i].final*(2/5))+ (info[i].midterm*(1/4))+ (info[i].hw1*(3/20)) + (info[i].hw2*(1/5));
                                }
                    }

I've written it so bizarre. I just simply want the code to stop what it was doing and exit from the loop when user enters "-1" from keyboard. I modified the code like this yet when i entered "-1" nothing happens. No results displayed no nothing happens. Just a blinking text marker.

do{
    
printf("Please enter student's information with blanks between them\n");
    
scanf( "%d %s %s %d %d %d %d", &info[i].number, info[i].name, 
info[i].surname, &info[i].midterm, &info[i].final, &info[i].hw1, &info[i].hw2   );
    
info[i].grade = (info[i].final*(2/5))+ (info[i].midterm*(1/4))+ (info[i].hw1*(3/20)) + (info[i].hw2*(1/5));

    studentcounter = studentcounter + 1;
      
 } while (info[i].number != -1);

Still can't figure out what the problem is... Tried everything but when i write -1 and press enter nothing happens..

It is best not write additional while loop, but instead on required condition - exit for loop. Something like that:

for(i = 0; i < N; i++)
	{
		printf("Please enter: number name surname midterm final hw1 hw2\n");
		scanf( "%d %s %s %d %d %d %d", 
			  &info[i].number, 
			  info[i].name, 
			  info[i].surname, 
			  &info[i].midterm, 
			  &info[i].final, 
			  &info[i].hw1, 
			  &info[i].hw2);
		// terminate input of students on some condition
		if (info[i].number==-1) {
			break;
		}
	}

Nothing happens after you enter -1, possibly because scanf function waits for other parameters for input. You should enter WHOLE line to terminate loop,- something like=> -1 1 1 1 1 1 1

Thanks for your answer 0x69. I solved it by puttin 2 scanf's in my code. And Gerard thank you so much for your answers, you really helped me to get somewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.