C++ 程序不';I don’不要把我想要的全部打印出来
我正在编写一个程序,该程序应该将不同视频的信息作为输入,然后将所有这些信息打印出来(该输出应该进行排序,但我还没有谈到这一部分)。我的问题是,当我结束输入时,它不会打印每个视频的信息,而只打印最后一个视频 我是一个编程新手,根本就搞不懂这一点 这是我的main.cpp文件:C++ 程序不';I don’不要把我想要的全部打印出来,c++,output,C++,Output,我正在编写一个程序,该程序应该将不同视频的信息作为输入,然后将所有这些信息打印出来(该输出应该进行排序,但我还没有谈到这一部分)。我的问题是,当我结束输入时,它不会打印每个视频的信息,而只打印最后一个视频 我是一个编程新手,根本就搞不懂这一点 这是我的main.cpp文件: #include<iostream> using namespace std; #include "video.h" int main() { const int MAX = 100; Video
#include<iostream>
using namespace std;
#include "video.h"
int main() {
const int MAX = 100;
Video *video[MAX]; //up to 100 videos
for(int l = 0; l < MAX; l++)
{
video[l] = NULL;
}
string title;
string url;
string desc;
string sorting;
float length;
int rate;
cout << "What sorting method would you like to use?" << endl;
getline(cin, sorting);
cout << "Enter the title, the URL, a comment, the length, and a rating for each video" << endl;
while(getline(cin, title))
{
getline(cin, url);
getline(cin, desc);
cin >> length;
cin >> rate;
cin.ignore();
}
for(int k = 0; k < MAX; k++)
{
video[k] = new Video(title, url, desc, length, rate);
}
video[MAX-1]->print();
return 0; }
#include "video.h"
#include<iostream>
using namespace std;
Video::Video(string title, string url, string desc, float length, int rate)
: m_title(title), m_url(url), m_desc(desc), m_length(length), m_rate(rate)
{
}
void Video::print() {
cout << m_title << ", " << m_url << ", " << m_desc << ", " << m_length;
cout << ", ";
for(int y=0; y < m_rate; y++)
{
cout << "*";
}
cout << endl;
}
#包括
使用名称空间std;
#包括“video.h”
int main(){
常数int MAX=100;
视频*Video[MAX];//最多100个视频
对于(int l=0;l打印();
返回0;}
这是我的video.cpp文件:
#include<iostream>
using namespace std;
#include "video.h"
int main() {
const int MAX = 100;
Video *video[MAX]; //up to 100 videos
for(int l = 0; l < MAX; l++)
{
video[l] = NULL;
}
string title;
string url;
string desc;
string sorting;
float length;
int rate;
cout << "What sorting method would you like to use?" << endl;
getline(cin, sorting);
cout << "Enter the title, the URL, a comment, the length, and a rating for each video" << endl;
while(getline(cin, title))
{
getline(cin, url);
getline(cin, desc);
cin >> length;
cin >> rate;
cin.ignore();
}
for(int k = 0; k < MAX; k++)
{
video[k] = new Video(title, url, desc, length, rate);
}
video[MAX-1]->print();
return 0; }
#include "video.h"
#include<iostream>
using namespace std;
Video::Video(string title, string url, string desc, float length, int rate)
: m_title(title), m_url(url), m_desc(desc), m_length(length), m_rate(rate)
{
}
void Video::print() {
cout << m_title << ", " << m_url << ", " << m_desc << ", " << m_length;
cout << ", ";
for(int y=0; y < m_rate; y++)
{
cout << "*";
}
cout << endl;
}
#包括“video.h”
#包括
使用名称空间std;
视频::视频(字符串标题、字符串url、字符串描述、浮点长度、整数速率)
:m_title(title)、m_url(url)、m_desc(desc)、m_length(length)、m_rate(rate)
{
}
void Video::print(){
cout您的线路video[MAX-1]->print();
上写着:
“获取数字为MAX-1的视频元素(最后一个元素)并调用其print()
-函数。”
如果您想打印每个视频,您需要另一个for循环,就像之前填充阵列时一样
顺便说一句:你总是用相同的信息填充你的数组,这是你想要的吗
编辑对于您的输入,请使用以下内容:
int k=0;
while(getline(cin, title))
{
getline(cin, url);
getline(cin, desc);
cin >> length;
cin >> rate;
cin.ignore();
video[k] = new Video(title, url, desc, length, rate);
k++;
}
如果您首先执行while循环,您的变量将只包含最后一次读取的值,并且所有视频将具有相同的信息。否,我希望使用与用户输入相同数量的不同视频填充数组。每个元素应包含不同视频的信息。for(int s=0;sprint();}
这就是我应该使用的吗?输出现在看起来是find,但在打印出所有视频后,出现了一个分段错误。你知道为什么它会在工作正常后这样说吗?你正在尝试打印MAX videos,但你创建的更少,因为你可能没有输入100。所以对于打印循环,使用s
作为条件输入时使用的是相同的k。之后,使用new
创建的每个元素都应该使用delete
删除。因此,从0到k的另一个for循环……非常感谢,我非常感谢。