The console prints with weird spacing?

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. ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;

class Film
{
public:
    string name;
    string type;
};

int main()
{
int filmNo = 0;
Film filmer[30];
string key;
char selection;
while (true){
cout << "[L]ägg till en film"
		"\n[V]isa alla titlar och typer"
        "\n[T]a bort film"
		"\n[S]ök film"
        "\n[A]vsluta";
cin >> selection;
switch (selection){


case 'l':
{
	cout << "Lägg till en film: ";
    getline(cin, filmer[filmNo].name) >> filmer[filmNo].name;
    cout << "Ange typ, blueray eller dvd: ";
    getline(cin, filmer[filmNo++].type) >> filmer[filmNo++].type;
}
break;


case 'v':
for (int i = 0; i < filmNo; i++){
	cout << "Filmerna i ditt bibliotek är: " << filmer[i].name << " / " << filmer[i].type << endl;
}
break;


case 't':
	int i;
	cout << "Vilken titel vill du ta bort?";
	cin >> i;
	filmer[i].name = "";
	filmer[i].type = "";
break;

case 's':
cout << "Vad heter filmen du vill söka efter?";
getline(cin, key) >> key;

for (int i = 0; i < filmNo; i++)
{
	if (filmer[i].name == key)
	{
		cout << "Filmen finns på plats: " << i;
		break;
	}
}
break;

case 'a':
return false;
break;


default: // funkar
cout << "Ogiltigt val!" << endl;
break;
        }
    }
}
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');
Last edited on
Topic archived. No new replies allowed.