如何修复C++中的"arrStud未在此范围内声明"错误
How to fix 'arrStud was not declared in this scope' error in C++
我目前正在编写一个程序,它将在 4 列(ID、名字、姓氏和 GPA(中显示学生记录,按升序对他们的记录进行排序,并从排序的学生记录中将他们的每个 GPA 重置为 0.0。
学生记录存储在我称为"StudentInfo.txt"的文本文件中
123456789 John Johnson 3.5
512434990 Mary Jackson 3.9
342432444 Peter Young 2.3
470068625 Jim Lee 2.9
234324324 Tammy Gaddis 3.1
121219000 Ester Schwab 2.7
我遇到麻烦的事情是在ifstream inputFile
之前声明一个结构数组。在第 42、53 和 68 行中显示"arrStud was not declared in this scope"
的错误消息。
我试图实现studentInfo **arrStud[SIZE]
和studentInfo *arrStud[SIZE]
但它没有奏效,并且发生了另一堆错误。
如果有人可以帮助我解决这个问题,我将不胜感激!
我当前的代码:
#include <string>
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
struct studentInfo
{
int ID;
string firstName;
string lastName;
double GPA;
};
const int SIZE = 100;
void display(/*parameter*/,int);
void resetGPA(studentInfo **, int);
void sortStud(studentInfo **, int);
int main()
{
int counter = 0;
int ID;
string firstName;
string lastName;
double GPA;
// Declare arrStud here...
ifstream inputFile;
inputFile.open("StudentInfo.txt");
if (inputFile.is_open())
{
cout << "ID:" << setw(15) << "Name:" << setw(14) << "GPA:" << endl;
cout << "-------------------------------------------" << endl;
while(!inputFile.eof())
{
inputFile >> ID >> firstName >> lastName >> GPA;
arrStud[counter] = new studentInfo;
arrStud[counter]->ID = ID;
arrStud[counter]->firstName = firstName;
arrStud[counter]->lastName = lastName;
arrStud[counter]->GPA = GPA;
counter++;
}
for (int i = 0; i < counter; i++)
cout << i << arrStud[i]->ID
<< setw(8) << arrStud[i]->firstName
<< setw(10) << arrStud[i]->lastName
<< setw(8) << arrStud[i]->GPA
<< endl;
cout << "-------------------------------------------" << endl;
cout << endl;
inputFile.close();
}
else
cout << "File cannot be opened.";
inputFile.close();
display(arrStud, counter);
cout << endl;
cout << "Sorting Students by ID..." << endl;
cout << endl;
sortStud(arrStud, ID);
cout << endl;
cout << "Resetting GPA Data..." << endl;
cout << endl;
resetGPA(arrStud, GPA);
}
void display()
{
}
void resetGPA(studentInfo** students, int numStu)
{
for (int i = 0; i < numStu; i++)
{
students[i]->GPA = 0.0;
}
}
void sortStud(studentInfo** students, int numStu)
{
int lowestIDIndex; //holds the index in the array students of the student with the lowest ID
for (int i = 0; i < numStu; i ++)
{
lowestIDIndex = i; //always start with lowest ID being first student
for (int j = i; j < numStu; j++) //j is equal to i so that you don't search the already sorted elements, which are less than i
{
if (students[j]->ID < students[lowestIDIndex]->ID) //search for the lowest ID
{
lowestIDIndex = j; //keep track of the lowest ID
}
}
//switch the lowest element with the front-most element
studentInfo* tempStuPtr = students[i];
students[i] = students[lowestIDIndex];
students[lowestIDIndex] = tempStuPtr;
}
}
预期输出:
ID: Name: GPA:
-----------------------------------
123456789 John Johnson 3.5
512434990 Mary Jackson 3.9
342432444 Peter Young 2.3
470068625 Jim Lee 2.9
234324324 Tammy Gaddis 3.1
121219000 Ester Schwab 2.7
-----------------------------------
Sorting Students by ID...
ID: Name: GPA:
-----------------------------------
512434990 Mary Jackson 3.9
123456789 John Johnson 3.5
234324324 Tammy Gaddis 3.1
470068625 Jim Lee 2.9
121219000 Ester Schwab 2.7
342432444 Peter Young 2.3
-----------------------------------
Resetting GPA Data...
ID: Name: GPA:
-----------------------------------
512434990 Mary Jackson 0.0
123456789 John Johnson 0.0
234324324 Tammy Gaddis 0.0
470068625 Jim Lee 0.0
121219000 Ester Schwab 0.0
342432444 Peter Young 0.0
-----------------------------------
你的代码中没有任何地方arrStud
声明!你需要这样的东西,靠近你的main()
函数的顶部(或者你可以把它作为一个全局变量,比如在main
的定义之前
studentInfo* arrStd[SIZE];
然后,在(或接近(main
结束时,您需要释放通过new
调用创建的内存:
for (int c = 0; c < counter; ++c) delete arrStud[c];
您还需要更正display
函数的声明和定义,如下所示:
void display(studentInfo**, int);
void display(studentInfo** students, int numStu)
{
// Do something here, I guess!
}
相关文章:
- 错误:未在此范围内声明'reverse'
- 错误:"imread"未在此范围内声明
- 未在此范围内声明错误 'xy'
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 'num1'未在此范围内声明
- 如何找到在本地范围内声明的变量的地址?
- NT状态未在此范围内声明
- 变量未在此范围内声明 数组线性搜索
- 包含文件中的 Typdef "未在此范围内声明"
- 'ClassName'和'ClassInstance'未在此范围内声明
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建
- 如何修复C++中的"arrStud未在此范围内声明"错误
- 错误:"SHGetKnownFolderPath"未在此范围内声明
- 错误:'Sprite'未在此范围内声明