2013-11-05 113 views
0

當我編譯它說「使用警告C4700:未初始化的本地變量」計數「。 我不知道爲什麼這麼說,我沒有來這裏,所以有人可以做我的功課。 只是尋找這一個錯誤的幫助,我知道它與函數定義ReadStudentData或主要。警告C4700:未初始化的本地變量

感謝

#include<iostream> 
#include<fstream> 
#include<string> 

using namespace std; 

struct StudentType 
{string studentName; 
int testScore;//Between 0 and 100 
char grade; 


}student[20]; 

void PrintNameHeader(ostream& out); 
bool OpenInputFile(ifstream& inFile, string& infilename); //OPEN input file 
void Pause();// Pause 
void ReadStudentData(ifstream& infile, StudentType student[], int&);// Read student infp including first and last name and test score 
void AssignGrades(StudentType student[], int);//assign grades to each student 
int HighestScore(const StudentType student[], int);//Get the highest scores 
void PrintNamesWithHighestScore(const StudentType student[], int);//Print name with highest Scores 
void DisplayAllStudents(const StudentType student[], int);//Display all students 
void GetLowHighRangeValues(int& , int&);//for example a student types 50 100 , it will get all students within that range 
void DisplayStudentsInRange(const StudentType student[], int, int, int);// display students in that range 
void SortStudentsByName(StudentType student[], int);// sort students by name 
void SortStudentsByScore(StudentType student[], int);// sort students by test score highest to lowest 


const int NUM_STUDENTS = 20; 

int main() 
{ 

    ifstream infile; 
    string inFilename; 

    int count = 0; 

    StudentType student[NUM_STUDENTS]; 

    int numStudents; 


    PrintNameHeader(cout); 

    OpenInputFile(infile,inFilename); 

    ReadStudentData(infile, student, numStudents); 

    AssignGrades(student, numStudents); 

    return 0; 
} 

//Function definitions 

void PrintNameHeader(ostream& out) 
{ 
    //Display name header on screen 
    cout << "name" << endl; 



} 
bool OpenInputFile(ifstream& inFile, string& infilename) 
{ 



    cout << "Enter the name of the .txt file that you want to open for input.\n"; 
    cout << "Do not put spaces in the file name "; 
    cin >> infilename; 
    cout << endl; 


    inFile.open(infilename.c_str()); 
    if (inFile.fail()) 
    { 
     cout << "Sorry, the input file " << infilename <<" was not found"<< endl;\ 
      return false; 
    } 

    cout << "Input file " << infilename << " is open for reading.\n\n"; 
    return true;  
} 

void Pause() 
{ 
    cout << endl; 

    cin.ignore(80, '\n'); 

    cout<<"Please hit the enter key to continue...\n"; 

    cin.get(); 

} 

void ReadStudentData(ifstream& infile, StudentType student[], int& numstudents) 
{ 
    string firstName, 
     LastName, 
     testScore; 

    int count = 0; 


    if(infile) 

     for (int count; count < NUM_STUDENTS; count++) 
     { 
      cin >> firstName[count] >> LastName[count] >> testScore[count]; 

      student[count].studentName = firstName + ", " + LastName; 

     } 




     numstudents = count; 
     cout << numstudents << endl; 




} 
void AssignGrades(StudentType student[], int numstudents) 
{ 

    int i; 
    for(i=0;i< NUM_STUDENTS;i++) 
     switch((int)(student[i].testScore/10)) 
    {case 10: 
    case 9: student[i].grade='A'; 
     break; 
    case 8: student[i].grade='B'; 
     break; 
    case 7: student[i].grade='C'; 
     break; 
    case 6: student[i].grade='D'; 
     break; 
    default: student[i].grade='F'; 
     break; 
    } 

} 
int HighestScore(const StudentType student[], int numstudents) 
{ 
    int max=0,i; 

    for(i=1;i<numstudents;i++) 
    { 
     if(student[i].testScore>student[max].testScore) 
      max=i; 
    } 

    return max; 

} 
void PrintNamesWithHighestScore(const StudentType student[], int numstudents) 
{ 

} 
void DisplayAllStudents(const StudentType student[], int numstudents) 
{ 

} 
void GetLowHighRangeValues(int& lowRange, int& highRange) 
{ 

} 

void DisplayStudentsInRange(const StudentType student[], int numStudents, int lownum, int highNum) 
{ 

} 

void SortStudentsByName(StudentType student[], int numStudents) 
{ 

} 

void SortStudentsByScore(StudentType student[], int numstudents) 
{ 

} 

回答

3

它指的是這樣的:

for (int count; count < NUM_STUDENTS; count++) 
// ^^^^^^^^^ 

您可能需要初始化count,大概是爲了0

for (int count = 0; count < NUM_STUDENTS; count++) 

也許你的意思是使用相同的count在外部塊中聲明:

for (; count < NUM_STUDENTS; count++) 
+0

哦,是啊,杜不能相信我錯過了,謝謝你們倆! –

2

您可以在for中定義另一個計數變量,該變量未被初始化。您應該初始化爲:

for (int count = 0; count < NUM_STUDENTS; count++) 

或刪除的聲明(如果需要)在外面的範圍:

for (; count < NUM_STUDENTS; count++) 
0

C和C++有製備用於使用可變/對象的兩個不同的組件。聲明和初始化。

這些聲明:

int i; 
Object o; 
struct Foo; 

這些聲明的東西,他們不告訴他們應該開始什麼值的編譯器。以下是合法的:

int meaning; 
if (day == "Thursday") 
    meaning = 42; 
else 
    meaning = 0; 

這段代碼確保意味着總是有一個值。

不幸的是,在C和C++基元類型中,如int等,沒有很好的,安全的默認值。所以如果你寫道:

int meaning; 
if (day == "Thursday") 
    meaning = 42; 
else if (day == "Friday") 
    meaning = 0; 

當day是「Monday」時,「意義」的值是什麼?如果你想要一個數字,你錯了。答案是:沒有定義。

編譯器會讓你建立這樣的代碼,但它會提供你看到的錯誤來幫助你保護自己。

聲明並同時初始化變量,簡單地做:

int meaning = 0; 
if (day == "Thursday") 
    meaning = 42; 

在你的代碼,這個問題是這樣的:

for (int count; count < NUM_STUDENTS; ++count) 

這應該是

for (int count = 0; count < NUM_STUDENTS; ++count) 

否則,程序可能會編譯,但「count」的初始值可能是-2^31到+ 2^31之間的任何數字。