Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

derp

Name: derp 2012-04-25 16:03

Anyone have any idea why this won't run? I've been trying to fix it for hours and can't find the issue. I created a different version utilizing functions from <string.h>, but I'd like to see if it can be built to run without it, while still having the readArray function I've built using getchar

     1  //Structures. Contain members.
     2
     3  #include <stdio.h>
     4
     5  void readArray(char []);
     6
     7  struct Student
     8  {
     9          int id;
    10          char name[30];
    11          float final;
    12  }
    13
    14  main()
    15  {
    16          struct Student st;
    17
    18          printf("Enter student id: ");
    19          scanf("%d", &st.id);
    20          printf("Enter name: ");
    21          readArray(st.name)
    22          printf("Enter final score: ");
    23          scanf("%f", &st.final);
    24
    25          printf("The ID is: %d\n", st.id);
    26          printf("The name is: %s\n", st.name);
    27          printf("The final score was: %.2f\n", st.final);
    28  }
    29
    30  void readArray(char st.name[])
    31  {
    32          char c;
    33          int i;
    34
    35          for(i=0; (c=getchar()) != '\n'; i++)
    36          {
    37          st.name[i] = c;
    38          }
    39          st.name[i] = '\0';
    40  }

THE ERROR:


studentstruc.c: In function âmainâ:
studentstruc.c:22:2: error: expected â;â before âprintfâ
studentstruc.c: At top level:
studentstruc.c:30:23: error: expected â;â, â,â or â)â before â.â token

Name: Anonymous 2012-04-25 16:05

I realize I just tried to use noko on a text board. oops.

Name: Anonymous 2012-04-25 16:06

Learn to read.

Name: Anonymous 2012-04-25 16:13

>>3
adding a ; suffix to the call to the readArray function worked... Sorry, I didn't think that you normally needed that for function calls. However I'm still having an error on the function itself, the same one as before.

Name: Anonymous 2012-04-25 16:16

>>4
Again, learn to read. You can't pass arguments like that.

Name: Anonymous 2012-04-25 16:19

>>5
Well, mind giving me a pointer on how to pass an argument for the name that would still belong to the Student structure?

Name: Anonymous 2012-04-25 16:25

>>1 You might want to create struct with malloc

Name: Anonymous 2012-04-25 16:41

>>6
Read K&R.

Name: Anonymous 2012-04-25 16:44

>>7
No reason for it. He does need to provide space for the strings themselves in some way, statically or otherwise.

Name: >>9 2012-04-25 16:45

Oh wait, I misread what he was trying to do. Yeah, no reason for malloc at all.

Name: Anonymous 2012-04-25 17:54

Apart from basic fucking syntax, you should probably ensure your reading loop doesn't run off the end of your array.

Name: Anonymous 2012-04-25 18:11

Why not use scanf for st.name?

Name: Anonymous 2012-04-25 23:30

your variable for readArray function is wrong

Name: Anonymous 2012-04-26 0:14

#include <stdio.h>
void readArray(Student);
struct Student{
    int id;
    char name[30];
    float final;
}
main(){
    struct Student st;
    printf("Enter student id: ");
    scanf("%d", &st.id);
    printf("Enter name: ");
    readArray(st.name)
    printf("Enter final score: ");
    scanf("%f", &st.final);
    printf("The ID is: %d\n", st.id);
    printf("The name is: %s\n", st.name);
    printf("The final score was: %.2f\n", st.final);
}
void readArray(Student st){
   char c;                    
   int i;
   for(i=0;( (c=getchar())!='\n' ) || (i<30); i++){
      st.name[i] = c;
   }   
   st.name[i] = '\0';
}

Name: Anonymous 2012-04-26 14:25

>>14
What's the type of &st.name? What's the type of st? Do you see your error? Because the compiler does.

Name: bampu pantsu 2012-05-29 4:38

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List