2013-10-15 92 views
0
#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, " ")); 
    } 

好吧需要一點點使用strtok。我試圖存儲輸入字符串的片斷以便稍後排序。我正在考慮使用strtok來打破字符串,然後將每個片放入相應的數組中。但是,每次嘗試時,我都會在Visual Studios中看到一個錯誤,指出訪問衝突。感謝提前使用Strtok存儲一串字符串

幫助

的錯誤是

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. 

輸入將

FirstName Lastname 80(Grade) 
+0

能否請您提供導致此部分樣品的輸入?也許從調試器的調用堆棧? –

+2

你爲什麼試圖將'char *'複製到'int *'vis strcpy中? –

回答

1

一個主要問題是您嘗試複製到整數值而不是字符串。更改 整數數組到字符串數組:

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

您還可以與gets功能的問題(除了它正在obseleted,不應該被使用),即以前scanf不會刪除來自輸入緩衝區的換行符,所以第一個gets調用將看到這個空的換行符,並給你一個空行(你不檢查)。

這簡直是告訴scanf"%d"後加入格式字符串空間丟棄尾隨空白解決:

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

相反的gets,你應該用fgets

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

最後,總是檢查錯誤!

+0

對不起,這是一個菜鳥的錯誤。 Fgets的確解決了這個問題,因爲我從現在開始使用它 –

0

一個潛在的問題是scanf/gets combo。改用fgets()並轉換適當的時候使用atoi()它也是好做什麼是由strtok的返回一個全面的檢查,以整數(這是從來沒有很好的承擔約輸入任何東西)

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

你也宣佈你的字符串作爲整數數組,它們應該是char 例如char fname[50];

0

你的問題是你已經聲明瞭三個變量(fname,lname和grade)作爲char [](數組)(嗯,這是你打算使用的類型),但是你想要提示和圍繞一堆學生信息。你後來試圖從strtok()複製到你想成爲char []的地方,但是因爲你取消了fname [i](lname [i],grade [i])的引用,所以它們是char類型的,而不是char []。

您將需要stdlib。出境小時,

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

您可以創建FNAME的[]數組,LNAME [],等級[],(在這裏看到: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, " ")); 
    } 
} 

或者你可以創建一個struct (URE)持有輸入的學生信息,並實例的這些學生記錄的數組,一個給你輸入每一位學生和存儲,

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); 
    } 
}