LeverArch LeverArch - 1 year ago 201
C++ Question

Char array with cin.getline()

I'm doing an assignment where we aren't allowed to use strings, we have to use char arrays. This is my code:

cout << "Enter Album name: ";
cin >> CDdata[count].title;
fout << CDdata[count].title;

the problem is that when I enter something with a space in it, the rest of my code gets screwed up.

How do I get it so that I can enter something with a space in it?

Answer Source

Use cin.getline(CDdata[count].title, 1000). The second parameter is the length of your char array, CData[count].title.

The above function either reads 1000 characters or until it finds a delimiter, which is by default a newline (\n) but can be changed as follows.

 cin.getline(CDdata[count].title, 1000, ',') //delimiter is changed to ','

If you want a more formal description, read here.

P.S: I have used 1000, second argument, as a placeholder. You should change it accordingly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download