Question

#include <stdio.h>
#include <math.h>
#include <string.h>
int main(void)
{
    int menuswitch=1;
    int amountofstudents;
    int fname[50];
    int lname[50];
    int grade[50];
    int i;
    char studentinfo[100];
    printf("Enter Amount of Students: ");
    scanf("%d", &amountofstudents);
    for (i=0;i<amountofstudents;i++)
    {
        gets(studentinfo);
        strcpy(fname[i], strtok(studentinfo, " "));
        strcpy(lname[i], strtok(NULL, " "));
        strcpy(grade[i], strtok(NULL, " "));
    }

Alright need a little using strtok. I am trying to store pieces of an input string to sort later. I was thinking of using strtok to break the string then placing each piece in the corresponding array. Yet, every time I try I get an error in Visual Studios saying Access Violation. Thanks for the help ahead of time

The error is

First-chance exception at 0x5120F7B3 (msvcr110d.dll) in Lab 2.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x5120F7B3 (msvcr110d.dll) in Lab 2.exe: 0xC0000005: Access violation reading location 0x00000000.

The input would be

FirstName Lastname 80(Grade)
Was it helpful?

Solution

One major problem is that you try to copy into integer values and not strings. Change the integer arrays to arrays of strings:

...
char fname[50][100];
char lname[50][100];
char grade[50][100];
...

You also have a problem with the gets function (besides it being obseleted and should not be used), namely that the previous scanf doesn't remove the newline from the input buffer so the first gets call will see this empty newline and give you an empty line (which you do not check for).

This is simply solved by telling scanf to discard trailing whitespace by adding a space in the format string after the "%d":

scanf("%d ", &amountofstudents);
/*       ^    */
/*       |    */
/* Note space */

Instead of gets, you should be using fgets:

fgets(studentinfo, sizeof(studentinfo), stdin);

And finally, always check for errors!

OTHER TIPS

a potential issue is the scanf/gets combo. use instead fgets() and convert when appropriate to integer using atoi() it is also good to do a sanity check on what is returned from strtok (it is never good to assume anything about input)

char* token = strtok(studentinfo, " ");
if ( strlen(token) < sizeof(fname[i]) )
{
  strcpy(fname[i], token);
...

you have also declared your strings as integer arrays, they should be char e.g. char fname[50];

The problem you have is that you have declared three variables (fname, lname, and grade) as char[] (arrays) (well, that is the type you meant to use), but you want to prompt for and keep around a bunch of students information. And you later try to copy from strtok() into what you want to be a char[], but since you dereferenced fname[i] (lname[i], grade[i]), they are of type char, rather than char[].

You will need stdlib.h for exit,

#include <stdio.h>
#include <stdlib.h> //for exit
#include <string.h>
//#include <math.h> //you don't need this, yet
#define STUDLIMIT (100)

You can either create an array of fname[], lname[], grade[], (see here: http://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html),

int main(void)
{
    //int menuswitch=1; //you aren't using this
    int amountofstudents;
    char studentinfo[100];
    char fname[STUDLIMIT][50];
    char lname[STUDLIMIT][50];
    char grade[STUDLIMIT][50];
    int ndx;
    printf("Enter Amount of Students: ");
    if( (fscanf(stdin,"%d ", &amountofstudents)<=0)
    || (amountofstudents<1) || (amountofstudents>STUDLIMIT) )
    {
        printf("need %d to %d studends\n",1,STUDLIMIT); exit(0);
    }
    for (ndx=0;ndx<amountofstudents;ndx++)
    {
        printf("Student: "); fflush(stdout);
        fgets(studentinfo,sizeof(studentinfo),stdin);
        strcpy(fname[ndx], strtok(studentinfo, " "));
        strcpy(lname[ndx], strtok(NULL, " "));
        strcpy(grade[ndx], strtok(NULL, " "));
    }
}

Or you can create a struct(ure) to hold the entered student information, and instantiate an array of these student records, one for each student you enter and store,

typedef struct student
{
    char fname[50];
    char lname[50];
    char grade[50];
} StudentObj;
int StudentCopy(StudentObj*sp,char*fname,char*lname,char*grade)
{
    if(!sp || !fname || !lname || !grade ) return -1;
    strcpy(sp->fname, fname);
    strcpy(sp->fname, lname);
    strcpy(sp->fname, grade);
}
StudentObj students[STUDLIMIT];
int main(void)
{
    //int menuswitch=1; //you aren't using this
    int amountofstudents;
    char studentinfo[100];
    char fname[50];
    char lname[50];
    char grade[50];
    int ndx;
    printf("Enter Amount of Students: ");
    if( (fscanf(stdin,"%d ",&amountofstudents)<=0)
    || (amountofstudents<1) || (amountofstudents>STUDLIMIT) )
    {
        printf("need %d to %d studends\n",1,STUDLIMIT); exit(0);
    }
    for (ndx=0;ndx<amountofstudents;ndx++)
    {
        printf("Student: "); fflush(stdout);
        fgets(studentinfo,sizeof(studentinfo),stdin);
        strcpy(fname, strtok(studentinfo, " "));
        strcpy(lname, strtok(NULL, " "));
        strcpy(grade, strtok(NULL, " \n\r"));
        StudentCopy(&(students[ndx]),fname,lname,grade);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top