【发布时间】:2014-11-27 21:37:48
【问题描述】:
我的第一节编程课(第一节编程课)即将结束,我需要帮助我编写代码,看看我是如何弄不清楚编译错误的。
首先,我尝试加载的数组给我带来了问题。
string stu[15];
stu[0]= Billy;
stu[1]= Bobby;
stu[2]= Bailee;
stu[3]= Barney;
stu[4]= Bambi;
stu[5]= Barbie;
stu[6]= Barrie;
stu[7]= Barry;
stu[8]= Benny;
stu[9]= Barkley;
stu[10]= Bennie;
stu[11]= Bonnie;
stu[12]= Bernie;
stu[13]= Bertie;
stu[14]= George;
显然它说每一个都是未声明的。但我认为这几乎是数组的重点?不要把所有的时间都花在声明变量上?
我的另一个问题是这段代码......
int name_count = 0;
while (name_count < 15)
{
inputFile >> (stu[0]);
if (!inputFile.good()) break;
++name_count;
}
我正在尝试将 .txt 文件中的数据加载到数组中,但我对如何执行此操作感到困惑。
所有代码放在一起:
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main() {
string inputFile;
string stu[15];
stu[0] = Billy;
stu[1] = Bobby;
stu[2] = Bailee;
stu[3] = Barney;
stu[4] = Bambi;
stu[5] = Barbie;
stu[6] = Barrie;
stu[7] = Barry;
stu[8] = Benny;
stu[9] = Barkley;
stu[10] = Bennie;
stu[11] = Bonnie;
stu[12] = Bernie;
stu[13] = Bertie;
stu[14] = George;
ofstream(studentFile)("Students.txt");
studentFile.open("Students.txt");
studentFile << " STUDENT INFO HERE ";
studentFile.close();
int name_count = 0;
while (name_count < 15) {
inputFile >> (stu[0]);
if (!inputFile.good())
break;
++name_count;
}
system("pause");
return 0;
}
我不断收到编译错误:
'inputFile >> stu[0]'中的'operator>>'不匹配
和
'struct std::string' 没有名为 'good' 的成员
很确定我在这里遗漏了一些东西......任何帮助都将不胜感激。非常感谢。
【问题讨论】:
-
不要给我们带来编程课作业。
标签: c++ arrays compilation