How to get a string by getline? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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