Hi everybody,
Im a new member on this forum and I want ur help on C language. Im a new student on this language & mostly we study it by our-selves.

This is the question:

One of the academic staff is trying to keep track of the students and their
semester results in his class. Write a program to read the name, metric
number, and the semester average for some students in this class using
one dimension arrays. Your program should allow the user to enter the
related information about each student and show the results (in three
columns format) in such a way to relate each name with its metric
number and average.

And this is my tries:

#include <stdio.h>

main (){

    char stu_name[10];

    int stu_ID;

    double Avg;

    int c;

    for(int i=0; i<10; i++){

        printf ("Enter your Name");
        scanf ("%s", stu_name [10]);

        printf ("Enter your ID");

        scanf ("%d",& stu_ID);

        printf ("The Avreg is:");
        scanf ("%f",& Avg);
    }

    for(int j=0; j<10; j++){
        printf ("stu_name %s", stu_name[10]);
        printf ("stu_ID %d", stu_ID);
        printf ("Avg %f", Avg);
    }
    c=getchar();
    return 0;
}

2)

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

struct st{
    char* StName;
    char* StID;
    int mark;
};

int main(void){
    struct st stArr[2];
    stArr[0].StName="Ibtisam";
    stArr[0].StID="2005599574";
    stArr[0].mark=83;
    stArr[1].StName="Ikram";
    stArr[1].StID="2005599576";
    stArr[1].mark=88;
    int counter;
    for(counter=0;counter<2;counter++){
        printf("----------\nname :%s\nID :%s\nmark:%d\n-----------\n",stArr[counter].StName,stArr[counter].StID,stArr[counter].mark);
        getchar();
    }
}

Please I want ur help.. tomorrow I will submit it!!
A note: we r using Borland c++ 5.02

my regards..

No suggestion???????????

commented: I suggest you look up "ur" in a dictionary - and read the forum rules on "chat-speak". -4

calm down...having that stance may only make someone angry.

printf ("Enter your Name");
scanf ("%s", stu_name [10]);

scanf ("%s", stu_name); - this is correct way of reading a string (when using scanf). point is you dont give an element of array to scanf (unless you want to read a single character).

@ 2nd code
you have to input data inside loop not manually
dont forget to make your program able to edit existing entries.
you can do this by printing all current entries and then asking user to input which student he wants to edit and so on...

ok what about this answer:

#include <stdio.h>
#include<conio.h>
void main()
{
char stu_name[10];
int stu_ID[10];
float Avg[10];

int i,j;
clrscr();

for(i=0;i<10;i++){
printf("Enter Your name:\n");
scanf("%s",stu_name);
printf("Enter Your ID :\n");
scanf("%d",&stu_ID);
printf("Enter the Avreg is :\n");
scanf("%f",& Avg); }

clrscr();
printf("\tStudent Name:");
printf("\t");printf("Student ID:");
printf("\t");
printf("Average:");
printf("\n\t________________________________________\n");

for(j=0;j<10;j++){
printf("\n");
printf("\t");
printf("%s\t",stu_name[j]);
printf(" |");
printf("\t\t");
printf("%d",stu_ID[j]);
printf("\t");
printf(" | ");
printf("%f",Avg[j]);}
getch();
}

shall i remove the array [10] from scanf and write it like this:

scanf ("%s", stu_name);

thanx a lot for ur help my teacher..

is it important to write
return 0;
??

do u advice me to add code
system("pause");

char stu_name[10]; is defining just one string that can hold 9 characters. You should define it like char stu_names[10][NAME_LENGTH]; . And no, you should never add system("pause");

Hey death_oclock is right. Not the other guy.Your question says details about many students so it should be of format:

student_name[<number of students>][<max length you would expect of a name>];

And ya as per my suggestion you sincerely need to optimize your code.There are many redundant and waste lines too.Ex:

printf("\n");
printf("\t");
printf("%s\t",stu_name[j]);

Why are you simply increasing the length of the code and also reducing its readability???You could also write this as:

printf("\n\t%s\t",stu_name[j]);

Also right??? Of course this is not redundant but first two lines needn't be separate lines.You got to look through and optimize your code.

@csurfer
i thought that you cannot read a string like scanf ("%s", stu_name[j]); if you defined stu_name like char stu_name[MAX_LENGTH];

That's true. You don't seem to be getting the point here. A string is an array of chars. An array of strings (multiple names) must be an array of arrays of string. Therefore you have the two dimensional array csurfer described. You should really take a look at the code provided and try to understand it. You would benefit from going back to the basics as well. Pointers are a crucial part of C...

@csurfer
i thought that you cannot read a string like scanf ("%s", stu_name[j]); if you defined stu_name like char stu_name[MAX_LENGTH];

Yes you can, but you shouldn't. If your string has a SPACE in it, you only read up to that space. And reading a string with scanf() is identical to using gets() , so should be avoided at all costs. Use fgets() .

@csurfer
i thought that you cannot read a string like scanf ("%s", stu_name[j]); if you defined stu_name like char stu_name[MAX_LENGTH];

death_oclock has already answered your point I suppose.Right?
usage:

char stu_name[30];
scanf("%s",stu_name[<some number>]);

is definitely wrong as you need to pass a char pointer to the start of an allocated memory to scanf to read a string into it.( As allocating a char array and passing the array name )

But the usage here is :

char stu_name[30][30]; // An example
for(i=0;i<30;i++)
                    scanf("%s",stu_name[i]);

which is equal to passing &stu_name[0] which is perfectly right so that you can store "i" number of names where every ith name is in array stu_name[30].Now got the point?

thanks...i know what you are talking about...and i know that we need to use 2-dimensional array in this case...but i was just saying that you must pass a pointer to array and not just single cell in array to input string. stu_name[i] is still a pointer as far as you declare stu_name is as stu_name[i][j] - two dimensional array

scanf ("%s", stu_name); - this is correct way of reading a string (when using scanf). point is you dont give an element of array to scanf (unless you want to read a single character).

i was talking more generally, not just for this task.
(i guess i sad that because in first example there was only info for one student so i used 1-dim array for name)
i guess i was a bit confusing...sorry

Yes you can, but you shouldn't

you mean...you can if you make a 2dim array, not just one dimensional array, because in that case (case of 1dim array) stu_name is just a char variable, not a pointer and you cannot use scanf ("%s", one_dimension_string[i]);

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.