while循环和从文件读取的问题

时间:2013-03-05 04:44:31

标签: c++ while-loop

我正在研究这个项目,而且我对C ++很新。它很难解释我正在尝试做什么,但我会尝试。所以我正在处理一个名为flix.txt的文件,其内容如下所示:

1 A 5
1 B 4
1 D 3
1 F 5
2 A 1
3 E 3
3 F 1
4 A 2

第一列是人物(我的物体),第二列是电影,第三列是物体给出的等级。

我试图首先从每一行中提取第一个int,然后使用'operator new'创建一个对象。然后我正在拍摄一部电影并将其变成一个int,这样我就可以将评级插入一个数组中。对不起,如果这听起来很混乱。这是我现在的代码:

//flix program

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#define NUMBER_OF_MOVIES 6

using namespace std;

int tokenize(string line);
int getMovieNum(char movie);
void resetPos(istream& flix);

class Matrix{
    public:
        int movieRate[NUMBER_OF_MOVIES];
};

int main(){

    int distinctCount = 0;
    int checker = -1;
    int check = 0;
    string line;

    int personNum;
    char movie;
    int rating;
    int movieNum;

    ifstream flix("flix.txt"); 
    ofstream flick("flix1.txt");

    //identify distinct account numbers in file    
    while(getline(flix, line)){
        check = tokenize(line);

        if(check != checker)
            distinctCount++;

        checker = check;
        check = 0;
    }

    //reset position in file
    resetPos(flix);

    //create objects in accordance with distinct numbers
    Matrix* person = new Matrix[distinctCount];

    for(int i = 0; i < distinctCount; i++){
        for(int j = 0; j < NUMBER_OF_MOVIES; j++){
            person[i].movieRate[j] = 0;
            cout << i + 1 << ' ' << person[i].movieRate[j] << endl;
        }
        cout << "\n";
    }
    //reset position in file
    resetPos(flix);

    //get data from file and put into respective variables
    while(getline(flix, line)){
        flix >> personNum >> movie >> rating;
        cout << personNum << ' ' << movie << ' ' << rating << endl;

        //changes the char into an int
        movieNum = getMovieNum(movie);

        person[personNum].movieRate[movieNum] = rating;
    }

    //reset position in file
    resetPos(flix);

    //input ratings into movie array
    for(int i = 0; i < distinctCount; i++){
        for(int j = 0; j < NUMBER_OF_MOVIES; j++){
            cout << i + 1 << ' ' << person[i].movieRate[j] << endl;
            flick << i + 1 << ' ' << person[i].movieRate[j] << endl;
        }
    }


    //write data to text file
    //??

    flick.close();
    //free memory
    delete[] person;

    system("pause");
    return 0;
}

int tokenize(string line){
    string myText(line);
    istringstream iss(myText);
    string token;

    getline(iss, token, ' ');

    int strInt = atoi(token.c_str());

    return strInt;
}

int getMovieNum(char movie){
    int movieNum = 0;

    switch(movie){
        case 'A':
            movieNum = 1;
            break;
        case 'B':
            movieNum = 2;
            break;
        case 'C':
            movieNum = 3;
            break;
        case 'D':
            movieNum = 4;
            break;
        case 'E':
            movieNum = 5;
            break;
        case 'F':
            movieNum = 6;
            break;
        default:
            movieNum = 0;
            break;
    }

    return movieNum;    
}
void resetPos(istream& flix){
        flix.clear();
        flix.seekg(0);
}

如果这里有任何错误的错误,我也会事先道歉。

我认为问题出在while循环的某个地方,这就是它一直锁定的地方。我花了几个小时在这上面,我无法弄清楚它为什么不起作用。在while循环中,我试图访问文件的每一行,从行中获取数据,获取movie char并将其转换为int,然后将数据插入到对象中的数组中。当我确实工作时,所有数据都是错误的。任何输入都非常感谢。提前谢谢。

3 个答案:

答案 0 :(得分:0)

好的,让我试着给出一个简单的起点:

#include <iostream>
#include <iterator>
#include <vector>
#include <fstream>

struct rater {
    std::vector<int> ratings;

    rater() : ratings(6) {}

    friend std::istream &operator>>(std::istream &is, rater &r) {
        char movie;
        is >> movie;
        return is >> r.ratings[movie-'A'];
    }

    friend std::ostream &operator<<(std::ostream &os, rater const &r) {
        for (int i=0; i<r.ratings.size(); i++) {
            os << char(i + 'A') << ":" << r.ratings[i] << "\t";
        }
        return os;
    }
};

int main() { 
    std::ifstream in("flix.txt");
    std::vector<rater> ratings(5);

    int i;

    while (in >> i) 
        in >> ratings[i-1];

    i=1;
    for (auto r : ratings) 
        std::cout << i++ << "-> " << r << "\n";    
}

答案 1 :(得分:0)

这里有点清理。它使用std::map来跟踪Person和Movie键,这可以更灵活,因为可以使用任何类型的文本字符串(无空格)。你添加了一条评论,说你特意要列出人们没有在他们的输出中评分的电影 - 这可以通过使用std::set并确保插入遇到的每个电影名称来完成,然后使用集合上的迭代引导每个人的评分中的查找:留作练习。

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;

typedef std::map<std::string, int> Movie_Ratings;
typedef std::map<std::string, Movie_Ratings> Persons_Movie_ratings;

int main()
{
    if (!ifstream flix("flix.txt"))
    {
        std::cerr << "error opening input\n";
        exit(1);
    }

    if (!ofstream flick("flix1.txt"))
    {
        std::cerr << "error opening output\n";
        exit(1);
    }

    Persons_Movie_Ratings ratings;

    std::string line;
    while (getline(flix, line))
    {
        istringstream iss(line);
        string person, movie;
        int rating;
        if (line >> person >> movie >> rating)
            ratings[person][movie] = rating;                
    }

    // input ratings into movie array
    for (Persons_Movie_Ratings::const_iterator i = ratings.begin();
         i != ratings.end(); ++i)
    {
        for (Movie_Ratings::const_iterator j = i->second.begin();
             j != i->second.end(); ++j)
        {
            cout << i->first << ' ' << j->second << endl;
            flick << i->first << ' ' << j->second << endl;
        }
    }

    system("pause");
}

答案 2 :(得分:0)

您需要在程序中稍微更改一下,我只会粘贴更改的部分。 在将人[]。movieRate []重置为零之后的某个地方,你已经写了这个循环

resetPos(flix);
int k = NUMBER_OF_MOVIES + 2;  //this is the variable that i have declared
//get data from file and put into respective variables
while(k){      //do you see that instead of getline() i have used the variable k. i'l tell you why later

    flix >> personNum >> movie >> rating;
    //personNum = tokenize(line,1);
    cout << personNum << ' ' << movie << ' ' << rating << endl;

    //changes the char into an int
    movieNum = getMovieNum(movie);
    person[personNum - 1].movieRate[movieNum] = rating;  //this is personNum-1 and NOT personNum the most common mistake while indexing array.
    k--;
}

此代码似乎可以作为您的标准。 我删除了getline()的原因是,如果你调用getline,那么get指针的位置会增加。所以在此之后你打电话给flix&gt;&gt;某事......,这会从第二行读取数据。你的第一行1 A 5丢失了。这是麻烦的原因。改变它,让我知道。