警告 C4700:未初始化的局部变量
warning C4700: uninitialized local variable
当我编译时,它说"警告C4700:使用了未初始化的局部变量'count'"。我不知道它为什么这么说,我没有来这里,所以有人可以做我的功课。只是寻求有关此错误的帮助,我知道它与函数定义有关 ReadStudentData 或 Main 中。
谢谢
#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.nn";
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)
{
}
它指的是这个:
for (int count; count < NUM_STUDENTS; count++)
// ^^^^^^^^^
您可能需要初始化count
,大概是为了0
:
for (int count = 0; count < NUM_STUDENTS; count++)
或者,也许您的意思是使用在外部块中声明的相同count
:
for (; count < NUM_STUDENTS; count++)
在 for 中定义另一个未初始化的计数变量。 您应该在以下位置初始化它:
for (int count = 0; count < NUM_STUDENTS; count++)
或者,如果在外部范围内需要声明,请将其删除:
for (; count < NUM_STUDENTS; count++)
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;
当一天是"星期一"时,"意义"的价值是什么?如果你想到一个数字,你就错了。答案是:它没有定义。
编译器将允许您构建这样的代码,但它将提供您看到的错误以帮助您保护自己。
要同时声明 AND 初始化变量,只需执行以下操作:
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 之间的任何数字。
相关文章:
- 为什么C++有不同的变量初始化方式?
- 静态 constexpr 成员变量初始化
- C++不同的变量初始化
- 全局和局部变量初始化与 constexpr 的差异背后的基本原理
- 是变量初始化失败吗?
- 视觉C++:在 DLL 加载期间,全局变量初始化顺序是否具有确定性?
- 类静态变量初始化顺序
- 使用 constinit 变量初始化 constexpr 变量
- 是否可以在不修改父类的情况下将成员变量初始化推迟到继承的类?
- 如何将局部变量初始化为C++的未知值
- 使用全局变量初始化不同编译单元中的其他全局变量
- 使用默认构造函数引用成员变量初始化错误
- 宏的 if 语句中的变量初始化
- 不稳定的C :每行适应性变化多变量初始化
- DLL 中的 C++ 静态局部变量初始化会导致_CrtIsValidHeapPointer异常
- C++-局部未初始化变量的值
- 在VS2015中c++局部静态变量初始化线程安全吗?
- 多线程环境中的静态局部变量初始化
- 局部静态变量初始化是线程安全的
- C++11 中线程安全局部静态变量初始化的成本