字符串下标超出了带有fstream的c++范围
string subscript out of range c++ with fstream
我的代码:
#include <iomanip>
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <ctime>
using namespace std;
void insertionSort(int arrtosort[], int size);
const int SIZE = 10;
int main() {
ofstream out;
ifstream ingrades;
unsigned seed = time(0);
char doAgain = ' ';
int id = 0;
string grade = " ";
int choice = 0;
int fileid[SIZE];
int numgrade[SIZE];
int sum = 0;
int counter = 0;
int average = 0;
int idx = 0;
string letterGrade = " ";
srand(seed);
out.open("dataout.txt");
while(counter <= 10) {
out << (10000 + rand() % 99999);
out << " ";
out << (-1 + rand() % 10) << endl;
//out<<" ";
counter++;
}
out.close();
ingrades.open("dataout.txt");
for(idx = 0; idx < SIZE; idx++ ) {
ingrades >> fileid[idx] >> numgrade[idx];
sum = sum + numgrade[idx];
counter++;
}
ingrades.close();
average = sum / counter;
out.open("dataout.txt");
for( idx = 0; idx < SIZE; idx++ ) {
if(numgrade[idx] == average || numgrade[idx] == average + 1) {
out << fileid[idx] << " " << numgrade[idx] << " B";
out << endl;
} else if(numgrade[idx] == average + 2 || numgrade[idx] == average + 3) {
out << fileid[idx] << " " << numgrade[idx] << " A-";
out << endl;
} else if(numgrade[idx] == average + 4 || numgrade[idx] >= 7 || numgrade[idx] == 10) {
out << fileid[idx] << " " << numgrade[idx] << " A";
out << endl;
} else if(numgrade[idx] == average - 1) {
out << fileid[idx] << " " << numgrade[idx] << " C";
out << endl;
} else if(numgrade[idx] == average - 2) {
out << fileid[idx] << " " << numgrade[idx] << " F";
out << endl;
} else if (numgrade[idx] == -1) {
out << fileid[idx] << " " << numgrade[idx] << " N/A";
out << endl;
}
}
out.close();
ingrades.open("dataout.txt");
for(idx = 0; idx < SIZE; idx++ ) {
ingrades >> fileid[idx] >> numgrade[idx];
}
ingrades.close();
do {
cout << "n" << right << setw(35) << "Teacher's Menu" << right << endl;
cout << "---------------------------------------------------------" << endl;
cout << "1) Sort the student list by id, and write it to your file" << endl;
cout << "2) Search for a student by id and display the id," <<
" numeric and letter grade ton the console" << endl;
cout << "3) Search for students with a particular letter grade and display the resultsn to the console" << endl;
cout << "4) Sort the student list by numeric grade and write it out to a file" << endl;
cout << "5) Find the percentage of students for a particular letter grade" << endl;
cout << "ntPlease enter which number option you wish to use: ";
cin >> choice;
ingrades.open("dataout.txt");
switch(choice) {
case 1:
break;
case 2:
ingrades.clear();
cout << "Enter in Student id: ";
cin >> id;
idx = 0;
for ( idx = 0; idx <= SIZE; idx++ ) {
ingrades >> fileid[idx] >> numgrade[idx] >> letterGrade[idx];
if ( fileid[idx] == id && idx <= SIZE ) {
cout << fileid[idx] << " " << numgrade[idx] << " " << letterGrade[idx] << endl;
break;
}
}
system("pause");
break;
case 3:
/* cout << "Enter in the Student letter grade: ";
cin >> grade;
for(idx = 0; idx < SIZE; idx++) {
if(grade == letterGrade[idx])
cout << fileid[idx] << " " << numgrade[idx] << " " << letterGrade[idx] << endl;
}
break;
*/
case 4:
break;
case 5:
break;
default:
cout << "Incorrect option" << endl;
}
cout << "Would you like to go back to the main menu y or n: " << endl;
cin >> doAgain;
} while(doAgain == 'y' || doAgain == 'Y');
ingrades.close();
//system("PAUSE");
return EXIT_SUCCESS;
}
void insertionSort(int arrtosort[], int size) {
int temp = arrtosort[0];
for(int i = 1; i <= size; i++) {
temp = arrtosort[i];
int j = 0;
for(j = i; j > 0; j--)
if(temp < arrtosort[j - 1])
arrtosort[j] = arrtosort[j - 1];
else break;
arrtosort[j] = temp;
}
}
这是一个学校的项目,但我没有通过课程,但我决心完成这个项目,继续学习。真的需要帮助。我觉得自己碰壁了。
我正试图从一个文本中读取ID,它也起作用,第四次我会出现错误。我想了解为什么这不起作用,以及我如何继续执行程序的其余部分。非常感谢任何和所有的帮助。也可以使用应用我的自动排序功能的帮助。老师给了我们循环,但不确定它是如何完全起作用的,也不确定如何正确应用它。
我遇到的麻烦从第127行开始,直到出现错误,比如字符串子字符串超出范围,以及在情况3 中比较字符串时遇到困难
首先,lettergrade不是一个数组,因此存在超出范围的故障。正确的代码如下:
for ( idx = 0; idx < SIZE; idx++ ) {
ingrades >> fileid[idx] >> numgrade[idx] >> letterGrade;
if ( fileid[idx] == id && idx <= SIZE ) {
cout << fileid[idx] << " " << numgrade[idx] << " " << letterGrade << endl;
break;
}
}
insert排序也有几个bug,我的版本会是这样的。
void insertionSort(int arrtosort[], int size) {
int temp = arrtosort[0];
for(int i = 1; i < size; i++) {
temp = arrtosort[i];
int j;
for(j = i; j > 0; j--)
if(temp < arrtosort[j - 1])
{
arrtosort[j] = arrtosort[j - 1];
arrtosort[j-1] = temp;
}
else break;
}
}
试着看看你的旧代码,看看当一个id不小于它的前一个id时会发生什么。正如@Dukeling所提到的,所有与SIZE的比较都应该是更少的,而不是更少的相等。
实现插入排序的一种方法可以是这样的;
case 4:
insertionSort(fileid,SIZE);
//fileid is now sorted
for(int i=0; i< SIZE;++i)
{
ingrades.open("dataout.txt");
for ( idx = 0; idx < SIZE; idx++ )
{
int id, t_grade;
ingrades >> id >> t_grade >> letterGrade;
//find the ith ID
if ( fileid[i] == id)
{
//and print it;
cout << id << " " << t_grade << " " << letterGrade << endl;
break;
}
}
ingrades.close();
}
break;
这将按
相关文章:
- 在fstream期间忽略空行
- 使用fstream对txt文件中的数字进行循环
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 使 std::fstream 写入文件末尾,但从头开始读取
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- fstream / ifstream / ofstream 对象如何转换为布尔值
- 使用命令的未定义引用<fstream>
- 如何检查文件是否已存在于 fstream C++中
- 如何正确使用 fstream 并在 char 数组中传递数据?
- 如何使用 fstream 的 .get 字符串?
- 使用 fstream 库并在屏幕上打印的文件有问题?
- Windows std::fstream 修改文件后打开()?
- 如何删除文件中的数字,通过 c++ fstream 的
- std::fstream 在 MSVC 和 G++ 上使用 UTF-8 的不同行为
- C++ 错误检查 fstream open() 命令和一般字符串流错误处理
- 在istream_iterator中使用后的 C++ FSTREAM obj 回滚
- 如何使用单个fstream创建、读取和写入文件