Hi,
in a school-assingment I'm trying to print out movie titels and type (blueray or dvd) from an array. But when it prints the spacing of the text gets pretty weird. It cant handle space in strings. So Avatar or Terminator is ok but Forrest Gump gets weird. Also every film seems to take up an extra slot in the array as noted below:
Filmerna i ditt bibliotek är: Terminator / dvd
Filmerna i ditt bibliotek är: /
Filmerna i ditt bibliotek är: Avatar / blueray
Filmerna i ditt bibliotek är: /
Filmerna i ditt bibliotek är: Forrest / dvd
Filmerna i ditt bibliotek är: / Gump
Here's the code, sorry for the swedish but I think it should be readeble anyhow. ;)
getline(cin, filmer[filmNo].name) >> filmer[filmNo].name;
First you read a line and store it in filmer[filmNo].name and then you read the next word and store that also in filmer[filmNo].name, so the old content is lost. I think a single call to getline is what you want. getline(cin, filmer[filmNo].name);
Same thing when you read the type.
After you you read the selection you need to remove the newline character, that is added after when you press enter, from cin. If you don't do that the next call to getline will find a new line character right away and give you an empty string. You can use cin.ignore();
This will remove the next character from cin. The user might add additional characters so the next character isn't necessary the new line character. To be safe you can remove the whole line, including the new line character by instead doing cin.ignore(numeric_limits<streamsize>::max(), '\n');