2012-02-28 16 views
0

私はIPODプレイリストのようなプロジェクトを持っています。私はベクトルを使用せずに構造体配列内の私のデータを読み込むのに問題があります。私のBFは私の教授はベクトルを使わないようにするのは間違っていると言いますが、それがルールです。皆さんには何か提案がありますか?私の教授は私が近くにいると言ったが、まだコンパイルしていないと言った。任意の助けをありがとう:)C++ポインタを使って構造体データを読み込む(曲のプレイリストプログラム)

Struct Songs{ 
string title;  
string artist; 
int mem;   //size of file in MBs 
}song[20];  //up to 20 possible songs 

int main 
{ 
    song * pointer = new song; 
    int num = 0; 

    ifstream fin; 
    fin.open("input.txt") 

    while (fin.good()) 
    { 
    getline(fin, *pointer[num].title; 

    if (*pointer[num].title.empty()) //to skip blank lines 
    continue; 

    getline(fin, *pointer[num].artist; 
    fin >> *pointer[num].mem.get(); //get to avoid whitespace/enter 

    num++; 
    } 

    for (int i = 0; i<num;i++) // my test to see if it reads in properly 
    { 
    cout << *pointer[num].title << endl;  
    cout << *pointer[num].artist << endl; 
    cout << *pointer[num].mem << endl; 
    } 
    fin.close(); 

    delete pointer [] ; 

    return 0; 
} 
+0

コンパイラのエラーを表示することをお勧めします。とにかく、それを見て、あなたは '曲'ではなく、 '曲'を指し示そうとしています。最初の行の構文は、動的に作成された新しいオブジェクトの場合は 'Songs * pointer = new Songs;'、プレマイドオブジェクトへのポインタの場合は 'Songs * pointer = song;'でなければなりません。また、 'int * intPtr =&somePreviousVariable;のように、ポインタを削除するときはメモリ位置へのポインタだけで' delete'を使います。 'int * intPtr = new int [20];'のように、 'new'で作成された配列を含むポインタに対しては' delete [] 'を使います。あなたの場合、 'pointer'はpremade配列を指しています。 – chris

+0

ありがとう私はそれを変更します – gamergirl22

+1

あなたのコードは単純なエラーで謎めいています。コンパイラが非常に有用なエラーを使って自分で修正する必要があります。私たちはあなたの宿題をするためにここにいるわけではありません。 – spencercw

答えて

0

多分、彼は代わりにキューを使用したいですか?曲は、プレイリストに「待ち行列に入れられる」ことができるので、より適したデータ構造である可能性があります。

+5

答えはありません。コメントにしてください –

0

私はいくつか見逃しているかもしれませんが、私はこれを修正するために私が行ったすべての変更をマークしたと思います。私が配置した構文エラーはたくさんあります。また主に私は新しいソング[20]に割り当てを変更したので、20の新しい曲のために十分なスペースを割り当てています。

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

struct Songs{ // Fixed - You had an uppercase S on 'struct' 
    string title;  
    string artist; 
    int mem;   //size of file in MBs 
}; // fixed - Removed since we will be allocating in Main() 

int main() // fixed - you are missing() at the end of main 
{ 

    Songs * pointer = new Songs[20]; //up to 20 possible songs // Fixed - You allocate 20 new Songs here 
    int num = 0; 

    ifstream fin; 
    fin.open("input.txt"); 

    while (fin.good()) 
    { 
      // Fixed - all uses of pointer as an array don't need to be de-referenced with *, [num] will do it 
     getline(fin, pointer[num].title); // Fixed - you were missing the functions closing) 

     if (pointer[num].title.empty()) //to skip blank lines 
      continue; 

     getline(fin, pointer[num].artist); // Fixed - you were missing the functions closing) 
     fin >> pointer[num].mem; //get to avoid whitespace/enter // Fixed - removed .get() 

     num++; 
    } 

    for (int i = 0; i<num;i++) // my test to see if it reads in properly 
    { 
     cout << pointer[num].title << endl;  
     cout << pointer[num].artist << endl; 
     cout << pointer[num].mem << endl; 
    } 
    fin.close(); 

    delete [] pointer; 

    return 0; 
} 
関連する問題