我正在学习struct
C++,但我不知道如何让它不跳过第一个问题。
如果我在其中使用 charstruct
它只会复制我的第一个单词,如果我尝试使用 string 它会跳过第一个问题(忽略它)。
你能指导我吗?
std::getline
稍后编辑:我使用 Microsoft Visual C++ 2010 速成版,和之间有区别cin.getline
吗?
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
struct melodie{
char artist[50];
char titlu[50];
int an;
int lungime;
};
void main(){
int repetam;
double rezultat;
cout<<"Cate melodii veti introduce: ";
cin>>repetam;
melodie *pointer = new melodie[repetam];
for(int i = 0; i<repetam; i++){
cout<<endl;
cout<<"Introduceti artistul melodiei: ";
cin.get(pointer[i].artist);
cout<<"Introduceti titlul melodiei: ";
cin.getline(pointer[i].titlu);
cout<<"Introduceti anul melodiei: ";
cin>>pointer[i].an;
cout<<"Introuceti lungea melodiei (in secunde): ";
cin>>pointer[i].lungime;
}
cout<<"Lista melodiilor introduse este:"<<endl;
for(int i =0; i<repetam; i++){
cout<<"Artistul melodiei este: "<<pointer[i].artist<<endl
<<"Titlul melodiei este: "<<pointer[i].titlu<<endl
<<"Anul melodiei este: "<<pointer[i].an<<endl
<<"Lungimea melodiei (in secunde) este: "<<pointer[i].lungime<<endl;
cout<<endl;
}
cout<<"Melodiile care au aparut dupa anul 2000 si au lungimea mai mica de 180 de secunde sunt: "<<endl;
for(int i = 0;i<repetam; i++){
if(pointer[i].an>2000 && pointer[i].lungime<180){
cout<<"Artist: "<<pointer[i].artist
<<endl
<<"Titlul: "<<pointer[i].titlu<<endl<<endl;
}
}
getch();
}