How to get a string by getline? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

How to get a string by getline?

Hi Everybody! I wanted to code a program but I was dumber than I expected😅 I just forgot how to get a char array without knowing the size of it. I just tried this code: int main(){ char a[1000]; int counter =0; cin>>a[counter]; counter++; while(a[counter-1]!='/0') { cin>>a[counter]; counter++;} } but when I ran this program, it didn't stop getting inputs. Then, I tried getline(cin,/*string*/ s), but there was an error😐 If you know the answer, please help me❤

10th Dec 2017, 7:49 PM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
9 ответов
+ 3
@jamie oh, I think I understood my fault, I just included string.h instead of string. Thank you for your help❤
11th Dec 2017, 1:57 PM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
+ 3
@jamie Thank you so much.
11th Dec 2017, 2:02 PM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
+ 3
@jamie Thank you☺🌸
11th Dec 2017, 2:11 PM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
+ 3
@jamie yes, I always used this code befor learning string, but I don't know why there is a problem. I'll read my code one more time. Thanks alot 🌼
12th Dec 2017, 5:40 PM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
+ 2
@jamie I tried both '/0' and ''\0' . about getline I did the same but there was an error for getline and I don't know what's wrong with it.
11th Dec 2017, 11:56 AM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
+ 2
You want your program to end reading input when you press enter? Then why are you comparing with a '\0' ? Your current code (after replacement of '/0' by '\0') requires the console to read a \0 to stop reading. But you cannot input it directly, and you need to use 'Alt + ASCII Code' for that, if you wish to keep using a \0 to end the read operation.
11th Dec 2017, 1:36 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
@Kinshuk Vasisht you mean I must to put 'Alt+ASCIICode' instead of '\0' ?
11th Dec 2017, 2:04 PM
니카 (Nika) [Dokhtre Falaki]
니카 (Nika) [Dokhtre Falaki] - avatar
- 4
میشه شماریته بتی؟؟؟
13th Dec 2017, 4:09 PM
Hakimkhan Hoshmand
Hakimkhan Hoshmand - avatar
- 4
Hakim khan hoshmand my Facebook account massage me😉
13th Dec 2017, 4:13 PM
Hakimkhan Hoshmand
Hakimkhan Hoshmand - avatar