2013-01-06 52 views
-1

我正在尝试编写一个程序,该程序从文本文件中读取信息并使用结构和联合和cstrings将其复制到数组中。我的文件看起来像这样。Switchcase声明问题。从txt文件读取信息C++

F South Korea 
Male Psy Park Jae Sang 
31 - 12 - 1977 
3 CSCI114 55 CSCI103 44 GangNam 100 

S Female Super Junior 
5 - 8 - 1978 
2 CSCI114 60 CSCI103 80 

F People Republic Of China 
Unknown James Bond 
11 - 12 - 1976 
4 CSCI114 54 CSCI124 66 CSCI007 99 CSCI123 28 

我的查询是,我写了一个简单的开关案例与一个for循环读取信息基于其标准。例如,每段前面的第一个字母表示学生是外国人,F还是新加坡人,S.根据他们的国籍,我选择将其信息复制到哪个结构/联盟。为了给你一个更好的主意,这是我从原始文本文件中获取信息并处理它后,最终的文本输出文件的样子。

http://i.stack.imgur.com/Bv2YS.jpg

下面是我的,我遇到的麻烦的代码段。似乎char变量是否读取“S”或“F”,我的switch语句只读取它为“F”。

 //file to array. 


     char dateJunk; 
     int numOfCourses; 

     int k = 0; 
     while (!afile.eof()) 
     { 
      for (int k = 0; k < 3; k++) 
      { 
      afile >> locale; 
       switch (locale) 
       { 
        case 'F': 
         afile.getline(x[k].st.foreignStudent.nationality, MAX); 
         afile >> x[k].st.foreignStudent.gender; 
         afile.getline(x[k].st.foreignStudent.name, MAX); 
         afile >> x[k].st.foreignStudent.bd.day; 
         afile >> dateJunk; 
         afile >> x[k].st.foreignStudent.bd.month; 
         afile >> dateJunk; 
         afile >> x[k].st.foreignStudent.bd.year; 
         afile >> x[k].st.foreignStudent.numOfCourses; 

         for (int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++) 
         { 
          afile >> x[i].st.foreignStudent.subjects[k]; 

          afile >> x[i].st.foreignStudent.grades[k]; 
         } 
         break; 

        case 'S': 
         afile >> x[k].st.localStudent.gender; 
         afile.getline(x[k].st.localStudent.name, MAX); 
         afile >> x[k].st.localStudent.bd.day; 
         afile >> dateJunk; 
         afile >> x[k].st.localStudent.bd.month; 
         afile >> dateJunk; 
         afile >> x[k].st.localStudent.bd.year; 
         afile >> x[k].st.localStudent.numOfCourses;; 
         for (int i = 0; i < x[k].st.localStudent.numOfCourses; i++) 
         { 
          afile >> x[i].st.localStudent.subjects[k]; 

          afile >> x[i].st.localStudent.grades[k]; 
         } 
       } 
       } 
     } 

     //Tests my cstring arrays to see everything is copied in correctly. 
     for (int k = 0; k < 3; k++) 
     { 
     cout << locale << " " << x[k].st.foreignStudent.nationality; 
     cout << endl; 
     cout << x[k].st.foreignStudent.gender; 
     cout << x[k].st.foreignStudent.name; 
     cout << endl; 
     cout << x[k].st.foreignStudent.bd.day << " - "; 
     cout << x[k].st.foreignStudent.bd.month << " - "; 
     cout << x[k].st.foreignStudent.bd.year; 
     cout << endl; 
     cout << x[k].st.foreignStudent.numOfCourses; 
     cout << endl; 
     for(int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++) 
     { 
     cout << x[i].st.foreignStudent.subjects[k] << " "; 
     cout << x[i].st.foreignStudent.grades[k] << " "; 

     } 
      cout << endl; 
     } 

     return 0; 
    } 

以下是代码整体。

#include <iostream> 
    #include <fstream> 
    #include <cstdlib> 
    #include <ctime> 
    using namespace std; 

    const int MAX = 80; 

    struct Birthday 
    { 
     char day[MAX]; 
     char month[MAX]; 
     char year[MAX]; 
    }; 

    struct Local 
    { 
     char name[MAX]; 
     char nationality[MAX]; 
     char gender[MAX]; 
     Birthday bd; 
     char subjects [MAX][MAX]; 
     char grades [MAX][MAX]; 
     int numOfCourses; 

    }; 

    struct Foreigner 
    { 
     char name[MAX]; 
     char nationality[MAX]; 
     char gender[MAX]; 
     Birthday bd; 
     char subjects [MAX][MAX]; 
     char grades [MAX][MAX]; 
     int numOfCourses; 
    }; 

    union Student 
    { 
     Local  localStudent; 
     Foreigner foreignStudent; 
    }; 

    enum CountryType {S, F}; 

    struct UowStudents 
    { 
     CountryType ct; 
     Student st; 
    }; 

    int fileToArray (fstream& afile, char fileName [], UowStudents* x, char& locale); 

    int main() 
    { 
     srand(time_t(NULL)); 

     fstream afile; 
     UowStudents x [MAX]; 
     char fileName[MAX]; 
     char locale; 
     cout << "Enter filename: "; 
     cin >> fileName; 
     int size = fileToArray (afile, fileName, x, locale); 


    } 

    int fileToArray (fstream& afile, char fileName [], UowStudents* x, char& locale) 
    { 
     afile.open(fileName, ios::in); 

     if (!afile) 
     { 
      cout << fileName << "could not be opened for read" << endl; 
      exit (-1); 
     } 
     //file to array. 


     char dateJunk; 
     int numOfCourses; 

     int k = 0; 
     while (!afile.eof()) 
     { 
      for (int k = 0; k < 3; k++) 
      { 
      afile >> locale; 
       switch (locale) 
       { 
        case 'F': 
         afile.getline(x[k].st.foreignStudent.nationality, MAX); 
         afile >> x[k].st.foreignStudent.gender; 
         afile.getline(x[k].st.foreignStudent.name, MAX); 
         afile >> x[k].st.foreignStudent.bd.day; 
         afile >> dateJunk; 
         afile >> x[k].st.foreignStudent.bd.month; 
         afile >> dateJunk; 
         afile >> x[k].st.foreignStudent.bd.year; 
         afile >> x[k].st.foreignStudent.numOfCourses; 

         for (int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++) 
         { 
          afile >> x[i].st.foreignStudent.subjects[k]; 

          afile >> x[i].st.foreignStudent.grades[k]; 
         } 
         break; 

        case 'S': 
         afile >> x[k].st.localStudent.gender; 
         afile.getline(x[k].st.localStudent.name, MAX); 
         afile >> x[k].st.localStudent.bd.day; 
         afile >> dateJunk; 
         afile >> x[k].st.localStudent.bd.month; 
         afile >> dateJunk; 
         afile >> x[k].st.localStudent.bd.year; 
         afile >> x[k].st.localStudent.numOfCourses;; 
         for (int i = 0; i < x[k].st.localStudent.numOfCourses; i++) 
         { 
          afile >> x[i].st.localStudent.subjects[k]; 

          afile >> x[i].st.localStudent.grades[k]; 
         } 
       } 
       } 
     } 

     //Tests my cstring arrays to see everything is copied in correctly. 
     //The print for foreign student cstrings also has information for the 
     //one Singaporean student "S" in the middle. Singaporean must go into 
     //the local cstrings. 
     for (int k = 0; k < 3; k++) 
     { 
     cout << locale << " " << x[k].st.foreignStudent.nationality; 
     cout << endl; 
     cout << x[k].st.foreignStudent.gender; 
     cout << x[k].st.foreignStudent.name; 
     cout << endl; 
     cout << x[k].st.foreignStudent.bd.day << " - "; 
     cout << x[k].st.foreignStudent.bd.month << " - "; 
     cout << x[k].st.foreignStudent.bd.year; 
     cout << endl; 
     cout << x[k].st.foreignStudent.numOfCourses; 
     cout << endl; 
     for(int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++) 
     { 
     cout << x[i].st.foreignStudent.subjects[k] << " "; 
     cout << x[i].st.foreignStudent.grades[k] << " "; 

     } 
      cout << endl; 
     } 

//as you can see, everything gets copied into localStudent struct as well. 
     for (int k = 0; k < 3; k++) 
     { 

      cout << x[k].st.localStudent.gender; 
      cout << x[k].st.localStudent.name; 
      cout << endl; 
      cout << x[k].st.localStudent.bd.day << " - "; 
      cout << x[k].st.localStudent.bd.month << " - "; 
      cout << x[k].st.localStudent.bd.year; 
      cout << endl; 
      cout << x[k].st.localStudent.numOfCourses; 
      cout << endl; 
      for(int i = 0; i < x[k].st.localStudent.numOfCourses; i++) 
      { 
       cout << x[i].st.localStudent.subjects[k] << " "; 
       cout << x[i].st.localStudent.grades[k] << " "; 

      } 
      cout << endl; 
     } 




     return 0; 
    } 
+0

[不要使用'while(!eof())'](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)。 – chris

+0

并且不要复制/粘贴所有代码,而是提供可用于重现问题的最小块:没有人会在此处读取超过10-15行的代码。无论如何,在调试器下运行一切,看看有什么不对。并添加一些错误处理!我看到100行代码没有一个错误处理的事情。 – stijn

+1

@stijn我以为我在第一个代码框中提供了这个问题块?第二个代码框是整个程序。 –

回答

0

您正在看到在C++中使用union的效果。

所有成员共享同一块内存的联合的主要特征。并且因为您的成员结构LocalForeigner具有完全相同的布局,您可以使用其中任一种来打印联合的内容。

如果您更改其中一个结构(例如,从Local删除nationality),您将看到一些打印输出出现乱码(特别是,您打印外国学生的地方反之亦然) 。这是因为用于学生的内存块对本地和外国学生的解释不同。
注意:从技术上讲,这会导致未定义的行为,但在这种情况下,结果不可能是灾难性的。正式的,你只能阅读你最后写给工会的同一个工会成员,或者(如果成员是结构的话),你可以阅读最后写给工会的结构中最初的成员​​。

+0

谢谢巴特,很好的回答! –