Hi guys i have a problem with my code :

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <conio.h>

struct blahblah {
	char id[11];
	char name[30];
	float gpa;
}lol[100];

int main(){
	int count = 0;
	char name [50][100]  ;
	for(int i =0;i<=4;i++){
		printf ("Student  %d\n",i+1);
		printf ("ID: ");
		gets(lol[i].id);
		printf("Name: ");
		gets(lol[i].name);
		printf ("GPA : ");
		scanf("%f",&lol[i].gpa);
		fflush(stdin);
		if (lol[i].gpa >= 3){
		strcpy(name[i],lol[i].name);
		count++;
		}
	}
		if(count>0){
			printf ("GPA ABOVE 3 :\n");
			for(int i =0;i<count;i++){
				printf ("%s\n",name[i]);
			}
			
		}
		else{
			for (int i =0;i<=4;i++){
				printf ("Student %d\n",i+1);
				printf ("Name : %s\t",lol[i].name);
				printf ("ID  : %s\t",lol[i].id);
				printf ("GPA  :%.2f\n",lol[i].gpa);
			}
		}
		getch();
	}

this program works fine as long as there's less than 4 people with 3 or above gpas, but it prints really weird lines once theres 4 or more people, i think the problem lies in line 25...

strcpy(name[i],lol[i].name);

any ideas??

When the problem description includes "prints really weird lines" then the problem is very likely to be uninitialized strings. I didn't run your code, but I would guess that it stems from writing sporadically to name with the index i . i will increment whether you copy to name or not, which means you'll have holes. Try this instead:

strcpy(name[count],lol[i].name);

When the problem description includes "prints really weird lines" then the problem is very likely to be uninitialized strings. I didn't run your code, but I would guess that it stems from writing sporadically to name with the index i . i will increment whether you copy to name or not, which means you'll have holes. Try this instead:

strcpy(name[count],lol[i].name);

Thanks it's working properly now. btw if you don't mind me asking is there a better way to store strings into an array than using strcpy()?

is there a better way to store strings into an array than using strcpy()?

You could write directly to the array, but if you have an existing string in memory and need to copy it to an array then strcpy() is one of the better options. Though keep in mind that if the source string exceeds the length of your destination array, strcpy() doesn't stop you and you'll introduce a bug. You'll want to check lengths first:

char dst[MAX_LEN];

if (strlen(src) > sizeof dst) {
    /* Handle the error, usually by extending memory or truncating the source string */
}

strcpy(dst, src);

If you don't care about potentially losing data on long source strings, the conventional method for safely "storing" a string is strncat():

dst[0] = '\0' /* This part is important! */
strncat(dst, src, sizeof dst);

There are two interesting questions that arise from this convention:

  1. Why set the first element of dst to '\0'? Because strncat() concatenates strings, and because of this expects dst to be a valid string (unlike strcpy()). The definition of a valid string is a potentially empty sequence of characters terminated with '\0'. Thus setting dst[0] to '\0' makes dst an empty string, which forces strncat() to start copying at the beginning. Failure to do this could potentially result the serious bug of calling strncat() with an uninitialized destination string.
  2. Why use strncat() instead of strncpy()? If you go by names then strncpy() makes more sense because you want strcpy() with a limiter. However, strncpy() doesn't work like most people expect, while strncat() on an empty destination works exactly like you would expect strncpy() to work. Thus strncat() is the better option. This is an artifact of history where strncpy() was designed for a slightly different purpose than if it were designed nowadays.

Thanks for the detailed explanation.

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.