2015-06-25 41 views
1

我正在尝试将特定变量写入文件,但奇怪的是,在我将其写入文件后,其中的2个变量没有写入文件。变量是平均值等级某些变量不会写入C++中的文件

下面的代码:

// Function to modify a student's exam scores. 
    void ArtStudent::modifyScore(string newName, int newIndian, int newEnglish, int newMath, int newHistory, int newMoral, int newEcon, int newCommerce, int newArt) { 


    map<string, tuple<int, char,int, int, int, int, int, int , int, int> > data; 

    // Read file and fill data map 
    ifstream studentRec("ArtStudentRecord.txt"); 
    string line; 

    while (getline(studentRec, line)) 
    { 
     string name; 
     int studentNo; 
     char gender; 
     int indian = 0; 
     int english = 0; 
     int math = 0; 
     int history = 0; 
     int moral = 0; 
     int economic = 0; 
     int commerce = 0; 
     int art = 0; 

     stringstream ss(line); 
     ss >> name >> studentNo >> gender >> indian >> english >> math >> history >> moral >> economic >> commerce >> art ; 
     data[name] = make_tuple(studentNo, gender, indian, english, math, history, moral, economic, commerce, art); 

    } 

    studentRec.close(); 
    // Modify data 


    auto it = data.find(newName) ; // gets current student record from the map 
    if (it == data.end()) // student not in map, that's an error 
    return ; 
    // now it->second holds your student data 
    // an auto here could be better, but we want to be sure of type 
    auto studentNo = get<0>(it->second) ; 
    auto gender = get<1>(it->second) ; 

    // Modify Data 
    data[newName] = make_tuple(studentNo, gender, newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt); 


    // Open same file for output, overwrite existing data 
    ofstream ofs("ArtStudentRecord.txt"); 

    for (auto entry = data.begin(); entry != data.end(); ++entry) 
    { 
     tie(studentNo, gender, newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt) = entry->second; 
     double average = averageScore(newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt); 
     char grade = getGrade(newIndian,newEnglish, newMath, newHistory, newMoral, newEcon, newCommerce, newArt, average); 

     ofs << left << setw(15) << entry->first << setw(15) << studentNo << setw(15) << gender << setw(15) << newIndian << setw(15) << newEnglish << setw(15) << newMath << setw(15) << newHistory << setw(15) << newMoral << setw(15) << 
      newEcon << setw(15) << newCommerce << setw(15) << newArt << setw(15) << right << average << grade << endl; 
    } 
    ofs.close(); 


} 

只有两个变量没有得到写入文件,平均性别

为了清楚起见,我有两个这些函数执行一些算法来计算平均分数并根据平均分数得出分数。

计算平均得分功能

double ArtStudent::averageScore(int malay, int english, int math, int history, int moral, int econ, int commerce, int art) { 

     double result = (malay + english + math + history + moral + econ + commerce + art)/8; 
     return result; 
    } 

获取学生的年级功能

// get Grade for student 
    char ArtStudent::getGrade(int malay, int english, int math, int history, int moral, int econ, int commerce, int art, double avScore) { 

     if (malay < 60 || english < 60 || math < 60 || history < 60 || moral < 60 || econ < 60 || commerce < 60 || art < 60) { 
      return 'F'; 
     } 

     if (avScore < 60) { 

      return 'F'; 
     } else if (avScore > 59 && avScore < 70){ 
      return 'D'; 
     } else if (avScore > 69 && avScore < 80) { 
      return 'C'; 
     } else if (avScore > 79 && avScore < 90) { 
      return 'B'; 
     } else { 
      return 'A'; 
     } 
    } 

我运行的功能后,我总是会得到这样的结果

Batman   316536   M    33    99    22    31    44    44    55    12    

正如你所看到的,它会写11个变量,而它应该是13个变量

谢谢。

回答

1

我复制了代码中写入文件的部分并制作了一个测试样机。我不知道你使用的是什么文本编辑器,但是你没有看到它,但是你的两个最后的变量也写入了这个文件。

我相信你的意思是说问题是变量的平均值和等级,而不是性别?

平均和档次都写有他们

之间

(...) << std::right << average << grade << std::endl; 
没有空格他们会出现像65一个变量。此外,您使用std :: right,因此平均值和等级将与先前用setw(15)设置的15个空格的右侧对齐。 (这会使它们与其他值相距更远)

看起来您的值正在写入文件。

这是我测试的代码:

std::ofstream ofs("ArtStudentRecord.txt"); 

std::string name = "FooBar"; 
int studentNo = 111111; 
char gender = 'M'; 
int newIndian = 50; 
int newEnglish = 80; 
int newMath = 50; 
int newHistory = 80; 
int newMoral = 50; 
int newEcon = 80; 
int newCommerce = 50; 
int newArt = 80; 
double average = 65; 
char grade = 'C'; 

ofs << std::left << std::setw(15) << name << std::setw(15) << studentNo 
    << std::setw(15) << gender << std::setw(15) << newIndian << std::setw(15) 
    << newEnglish << std::setw(15) << newMath << std::setw(15) << newHistory 
    << std::setw(15) << newMoral << std::setw(15) << newEcon << std::setw(15) 
    << newCommerce << std::setw(15) << newArt << std::setw(15) 
    << std::right << average << grade << std::endl; 

ofs.close(); 

而结果,

FooBar   111111   M    50    80    50    80    50    80    50    80       65C 

(注意如何平均和等级分别略微地与其它值分开)

在一个请注意,在使用它们之前,您应该始终检查您的文件是否已成功打开。

std::ofstream file("PathOfAFile"); 
if (file.is_open()) { 
    // Do things with file 
    file.close(); 
} 
else { 
    // ERROR 
} 
+0

确定吗?因为它仍然没有显示在记事本 – airsoftFreak

+0

我使用记事本++,它显示了我。尝试其他编辑器。(虽然它也出现在普通的记事本中) – aslg

+0

我尝试了崇高的文字,但它仍然没有出现。 – airsoftFreak