如何输入某个文件,同时将某些部分分配到不同的变量中

时间:2014-04-28 07:03:56

标签: c++ debugging input io getline

我有一个包含埃尔维斯普雷斯利作品唱片的文件。我需要将它输入到我的程序中,同时将:歌曲名称,发行日期,专辑名称以及分钟和秒钟分成不同的变量。我不是很多,而且我确定它充满了错误(Haven已编译)但我真的会对此有所帮助。非常感谢你提前。这就是我得到的。

编辑:我只是需要帮助输入而不是计划的其余部分

输入文件:

Elvis Presley
March 1956 
 1. Blue Suede Shoes                     1:58 
 2. I'm Counting on You                  2:24
 3. I Got A Woman                        2:23
 4. One-Sided Love Affair                2:09
 5. I Love You Because                   2:42
 6. Just Because                         2:32
 7. Tutti Frutti                         1:59
 8. Tryin' to Get to You                 2:33
 9. I'm Gonna Sit Right Down and Cry     2:05
10. I'll Never Let You Go                2:25
11. Blue Moon                            2:43
12. Money Honey                          2:36
=============================================
Blue Hawaii
November 1961 
 1. Blue Hawaii                          2:39
 2. Almost Always True                   2:22
 3. Aloha-Oe                             1:54
 4. No More                              2:25
 5. Can't Help Falling In Love           3:04
 6. Rock-a Hula Baby                     2:02
 7. Moonlight Swim                       2:22
 8. Ku-U-I-Po (Hawaiian Sweetheart)      2:23
 9. Ito Eats                             1:25
10. Slicin' Sand                         1:38
11. Hawaiian Sunset                      2:37
12. Beach Boy Blues                      2:07
13. Island of Love                       2:42
14. Hawaiian Wedding Song                2:51
=============================================
Elvis' Christmas Album
October 1957 
1.  Santa Claus Is Back in Town          2:22
2.  White Christmas                      2:23
3.  Here Comes Santa Claus               1:54
4.  I'll Be Home for Christmas           1:53
5.  Blue Christmas                       2:07
6.  Santa Bring My Baby Back (to Me)     1:54 
5.  O Little Town of Bethlehem           2:35
6.  Silent Night                         2:23
7.  Peace in the Valley                  3:22 
=============================================
G.I. Blues (album)
October 1960 
 1. Tonight Is So Right for Love         2:14
 2. What's She Really Like               2:17
 3. Frankfort Special                    2:58
 4. Wooden Heart                         2:03
 5. G.I. Blues                           2:36 
 6. Pocketful of Rainbows                2:35
 7. Shoppin' Around                      2:24
 8. Big Boots                            1:31
 9. Didja' Ever                          2:36
10. Blue Suede Shoes                     2:07
11. Doin' the Best I Can                 3:10 
=============================================
Follow That Dream
April 1962 
1.  Follow That Dream                    1:39
2.  Angel                                2:39 
3.  What A Wonderful Life                2:27
4.  I'm Not The Marrying Kind            1:51

到目前为止我对这个计划的看法。

#include <iostream>
#include <iomanip>

using namespace std;

int main(){

 char songName[256];

 String AlbumName[5];
 String ReleaseDate[5];
 String SongName[5][14];\
 int hours[5];
 int minutes[5][14];
 int seconds[5][14];

 for(int i = 0; i < 5; i++){
  getline(cin,AlbumName[i]);
  getline(cin,RelaseDate[i]);
  for(int r = 0; r < 14; r++){
   cin.get(SongName[i][r], 41);
   cin.get(minutes[i][r], 42);
   cin.get(seconds[i][r], 44);
  }

 }
}

在得到第一个答案之后,我得到了一堆错误,我可以看到它是如何工作的,但它又不是。

#include <iostream>
#include <iomanip>
//#include <fstream>

using namespace std;

 struct Album
    {
      string albumName;
      string releaseDate;
      string songName;
      int minutes;
      int seconds;
      //..... other members
    };

    int main()
    {
        struct Album albums[5][14];
        // do other things
        for (int i=0; i< 5;i++){
         /// read in values like albums[i].albumName = value ...
         cin.get(albums[i][0].albumName, 22);
         cin.get(albums[i][0].releaseDate, 22);
         for(int r=0; r< 14;r++){
            char a;
            cin >> a;
            if(a == '='){
                r == 14;
            }
            get(albums[i][r].songName, 39);
            cin >> albums[i][r].minutes >> albums[i][r].seconds;
            // cin >> albums[i][r].songName >> albums[i][r].minutes >> albums[i][r].seconds;
           }
        }


        for(int i = 0; i<5;i++){
                cout<<albums[i][0].albumName<<endl<<albums[i][0].releaseDate<<endl;
                for(int r = 0; r<14;r++){
                        cout <<albums[i][r].albumName <<"       "<< albums[i][r].minutes <<" "<< albums[i][r].seconds;
                }
        }
        return 0;
    }

1 个答案:

答案 0 :(得分:1)

您应该使用结构(可能名为&#39;相册&#39;)来保存特定相册的所有相关数据,如专辑名称,发布日期等。然后创建一个结构数组,从文件中读取数据值并存储在结构中。

    struct Album
    {
      string albumName;
      string releaseDate;
      //..... other members
    };

    int main()
    {
        struct Album albums[5];
        // do other things
        for (int i=0; i< 5;i++){
         /// read in values like albums[i].albumName = value ...
        }
        return 0;
    }