C++ 如何在C++;
我试图允许用户修改数据文件中的值。我使用类成员来保存值和数组C++ 如何在C++;,c++,C++,我试图允许用户修改数据文件中的值。我使用类成员来保存值和数组 #include <iostream> #include <fstream> using namespace std; class student { private: string stuID; string lastN; string firstN; float GPA; int numb_of_enrolled; public: void displa
#include <iostream>
#include <fstream>
using namespace std;
class student
{
private:
string stuID;
string lastN;
string firstN;
float GPA;
int numb_of_enrolled;
public:
void displayAll();
void setAll(string StuID, string LastN, string FirstN, float gpa, int nof);
};
const int SIZE = 30;
int main()
{
ifstream inFile;
int option, count, scount = 0, cIndex;
student stuFile[SIZE];
string sID, sLN, sFN;
float sgpa;
int snof;
char repeat = 'y';
inFile.open("Students.dat", ios::in);
cout << "MENU\n";
cout << "3. Change an Existing Record's Fields\n";
cout << "What would you like to do?: ";
cin >> option;
cin.ignore();
switch(option) {
case 3:
inFile >> sID >> sLN >> sFN >> sgpa >> snof;
cout << "There are 19 Students in this data file\n";
cout << "Enter an Index from 0 - 19: ";
cin >> cIndex;
cin.ignore();
stuFile[cIndex].setAll(sID, sLN, sFN, sgpa, snof);
stuFile[cIndex].displayAll();
}
}
void student::displayAll()
{
cout << "Student ID: " << stuID << endl;
cout << lastN << ", " << firstN << endl;
cout << numb_of_enrolled << " Classes taken this Semester\n";
cout << "Current GPA is: " << GPA << "\n\n";
}
void student::setAll(string StuID, string LastN, string FirstN, float gpa, int nof)
{
stuID = StuID;
lastN = LastN;
firstN = FirstN;
GPA = gpa;
numb_of_enrolled = nof;
}
#包括
#包括
使用名称空间std;
班级学生
{
私人:
弦杆;
字符串lastN;
先串;
浮动GPA;
登记的int NUMBER_;
公众:
void displayAll();
void setAll(字符串StuID、字符串LastN、字符串FirstN、浮点gpa、整数nof);
};
常数int SIZE=30;
int main()
{
河流充填;
int选项,计数,Scont=0,cIndex;
学生档案[大小];
字符串sID、sLN、sFN;
浮动sgpa;
int snof;
字符重复='y';
infle.open(“Students.dat”,ios::in);
cout>sID>>sLN>>sFN>>sgpa>>snof;
cout-cIndex;
cin.ignore();
stuFile[cIndex].setAll(sID、sLN、sFN、sgpa、snof);
stuFile[cIndex].displayAll();
}
}
void student::displayAll()
{
cout您需要为cIndex循环N行,声明inti
并执行此代码
case 3:
cout << "There are 19 Students in this data file\n";
cout << "Enter an Index from 0 - 19: ";
cin >> cIndex;
cin.ignore();
for(i=0;i<cIndex;i++) {
inFile >> sID >> sLN >> sFN >> sgpa >> snof;
}
stuFile[cIndex].setAll(sID, sLN, sFN, sgpa, snof);
stuFile[cIndex].displayAll();
inFile.clear();
inFile.seekg(0);
案例3:
cout-cIndex;
cin.ignore();
对于(i=0;i>sID>>sLN>>sFN>>sgpa>>snof;
}
stuFile[cIndex].setAll(sID、sLN、sFN、sgpa、snof);
stuFile[cIndex].displayAll();
infle.clear();
填充量见千克(0);
你能把文件中的一行数据放进去吗,我会帮你AF101 Smith Zachery 3.5 3@Dickensas使用我的代码,你需要通过循环到达这一行,或者你需要在fstreamIt中查找方法显示所有5个,我只需要它显示调用的内容。因此,如果AF101 Smith Zachery 3.5 3在元素4中,我需要它跳过1-3,只显示4.as根据代码,它将只读取所有行,但仅显示一行,在我的计算机中,可能是您有更多的代码,而开关中没有break语句,仅stuFile[cIndex].displayAll()
将显示值,infle>>sID>>sLN>>sFN>>sgpa>>snof
将读取值并将其保存在这些变量中,我在本地测试了此代码:)哦,我在for循环中使用了stuFile,取出它解决了问题。非常感谢。现在我遇到了另一个问题。因为它读取值并将它们保存在这些变量中。我必须重新打开它作为一个OFFROW,以便我可以修改DATAFILNO的值,需要重新打开使用,从我的答案中求零,以重绕文件开始,显示可以使用局部变量而不是SID的值,使用一个本地SID1,只用于显示,在C++中不需要在顶部声明所有变量,可以在本地声明变量。