使用while循环创建对象

时间:2016-09-10 02:57:37

标签: c++ class object while-loop cin

我正在处理的最大问题是在我的main.cpp文件中,但我发布了我的video.h文件和video.cpp文件以防万一。

我很难理解阅读输入然后创建对象的概念。

我有一个类视频,其中包含创建对象所需的成员函数。我的班级很好。在我的主要内容中,我希望能够在创建对象之前读取输入,而不是创建对象然后初始化变量。

video.h

#ifndef VIDEO_H
#define VIDEO_H
#include<iostream>
#include<string>
using namespace std;

class Video{

  public:

// constructor works fine
    Video(string name, string link, string comment,
        double rating, int stars);
    void print();




// member variables
  private:

    string m_name;
    string m_link;
    string m_comment;
    double m_rating;
    int m_stars;

};

#endif

video.cpp

#include<iostream>
#include "video.h"
using namespace std;


Video :: Video (string name, string link, string comment,
                double rating, int stars){


  m_name = name;
  m_link = link;
  m_comment = comment;
  m_rating = rating;
  m_stars = stars;
}

void Video :: print (){

  cout << m_name << ", " << m_link << ", " << m_comment << ", " <<
    m_rating << ", ";

for(int count = 0; count < m_stars; ++count){

  cout << "*";
//cout << endl;
}
cout << endl;
  //<< " , " << m_stars << endl;


}

的main.cpp

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

#include "video.h"

int main()

/*  My program can create objects and initialize the string variables
   if the data is hard-coded when the object is created however I want
   read data using cin and a while loop then create an object.
    Video video1("Title One", "www.youtube.com/one", 
                           "Comment ONE", 1.1, 1);
    Video video2("Title Two", "www.youtube.com/two", 
                           "Comment TWO", 2.2, 2);

    video1.print();
    video2.print();
*/
{

  while (Video >> cin) { //I want to use a while loop here
    /* The program needs to read these getlines 
       and other variables in order to store names or comments 
       that have spaces */


    getline(cin, name); // user enters the name then presses "enter"
    getline(cin, link); // user enters the link then presses "enter"
    getline(cin, comment); // user enters the comment then presses "enter"
    double rating; // user enters the rating then presses "enter"
    int stars; // user enters the amount of stars then presses "enter"
    Video video_one; /* Once the user enters data for all five members a new object is created */
  }



}

2 个答案:

答案 0 :(得分:0)

我会做的

    while(true)
    {
      getline(cin, name); // user enters the name then presses "enter"
      getline(cin, link); // user enters the link then presses "enter"
      getline(cin, comment); // user enters the comment then presses "enter"
      double rating; // user enters the rating then presses "enter"
      cin >> rating;
      int stars; // user enters the amount of stars then presses "enter"
      cin >> stars;
      Video video_one(name,link,comment,rating,stars); /* Once the user enters data for all five members a new object is created */
    }

答案 1 :(得分:0)

注意: 你不能这样做:

while(video>>cin){...}

没有朋友函数重载提取&gt;&gt;运营商。要做你想做的事,在你的Video类中重载提取操作符(&gt;&gt;):

friend istream &operator>>(istream &input, Video &V)
 {
  input>> V.m_name>>V.m_link>>V.m_comment...;
  return input;
 }

并像这样使用它:

 while(cin>>video){...}   

另外,要首先读取输入然后实例化对象,您需要一个容器(矢量或数组)来容纳所有创建的新视频对象。您知道预先输入的视频数量吗?原因是因为数组被分配了固定数量的内存,并且如果新视频对象的数量合并了。输入的数据超过了数组内存的数量,会导致分段错误。有一些方法可以使您的数组动态,但使用Vector类会更容易,因为随着更多视频节目的输入,它将自动增加容器大小(获得更多内存)。我使用了一个名为listofVideos的矢量。在while循环结束时,使用迭代器遍历向量。

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

typedef struct Video_t{ //I used a struct in this case instead of the class 

    string m_name;
    string m_link;
    string m_comment;
    int m_stars;
}Video_t;
//#include "video.h"

int main()
{
  string buffer;
  string name;
  string link;
  string comment;
  double rating;
  double stars;
    vector<Video_t> listofVideos; //I used a vector in this case to store all the videos. 

  while (getline(cin,buffer)&&buffer != "end") { //I used a control word to signal end of inputs.
    if(buffer.empty());
    istringstream is(buffer);
    if(is>>name>>link>>comment>>stars)
    {
      Video_t vid;
      vid.m_name = name;
      vid.m_link=link;
      vid.m_comment = comment;
      vid.m_stars = stars;
      listofVideos.push_back(vid);
    }
  }  

  vector<Video_t>::iterator it;
 for(it=listofVideos.begin();it!=listofVideos.end();++it)
  cout<<(*it).m_name<<" "<<(*it).m_link<<" "<<(*it).m_stars<<endl;

  return 0;
  }