Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char array with cin.getline()

Tags:

c++

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?

like image 567
LeverArch Avatar asked Nov 16 '25 10:11

LeverArch


1 Answers

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.

like image 168
vidit Avatar answered Nov 18 '25 22:11

vidit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!