Problem with C ++ and cin.getline

I am learning struct in C ++ and I don't know how to get it not to skip over the first question.

If I use char in a struct , it will only copy the first word to me, if I try to use a string, it jumps to the first question (ignoring it).

Could you guide me?

Then edit: I am using the express version of Microsoft Visual C ++ 2010, and is there a difference between std::getline and 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 ve-ti 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(); } 
+4
source share
1 answer

First, yours may not compile. There are a number of errors:

  • void main() is non-standard. main() always returns int , and the only portable declarations of main() are int main() and int main(int argc, char* argv[]) (or int main(int argc, char** argv) , which are identical to the previous one, and, of course, the names of these two arguments can be freely chosen).
  • The get() and getline() member function that you use takes at least one additional argument: the size of the available buffer.

For the rest of the answer, I assume these errors are fixed.

Of course, the entry does not "skip over the first question." Instead, it reads input that has already been specified, but which you probably didn’t think it was: formatted input using >> stops reading immediately after the completion of its corresponding format (or crash). For example, when reading an integer, it stops when an insignificant digit is encountered. Thus, when reading int for repetam input stream stops reading after the last digit when it encounters a new line used to complete this input. Calling get() calls the string (up to the first). When entering repetam , a new line was entered, and thus this line is a new line, where get() stops.

The more general, when mixing formatted input (i.e. using >> ) and unformatted input (i.e. using the named functions get() , getline() , read() , etc.), you usually you need to remove unwanted characters. In your example, you probably want to get rid of the first new line before calling get() (you need to include <limits> to access std::numeric_limits ):

 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.get(pointer[i].artist, 50); 

If your artisti line does not start with a space character, you can use

 (std::cin >> std::ws).get(pointer[i].artist, 50); 

The std::ws manipulator skips all leading spaces (it skips spaces, tabs, linefeeds, etc.) and is somewhat easier to enter. Of course, if you often need to ignore newlines, you can create a manipulator to ignore the characters before and including the newline:

 std::istream& skipline(std::istream& in) { return in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } // ... (std::cin >> skipline).get(pointer[i].artist, 50); 

However, I think you really did not want to use get() , because this function includes the separation character ( '\n' by default) in the string. You probably want to use getline() . In addition, with std::string it is easier to process character arrays, i.e. I would use a different definition for struct :

 struct melodi { std::string artist; std::string titlu; int an; int lungime; }; 

Thus, strings can be of arbitrary size and not limited to 49 characters (your version should have space for a terminating null character). To read std::string you need a non-member function std::getline() ):

 std::getline(std::cin >> skipline, artist); 
+2
source

All Articles