+ 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❤
15 Réponses
+ 3
@jamie oh, I think I understood my fault, I just included string.h instead of string. Thank you for your help❤
+ 3
@jamie Thank you so much.
+ 3
@jamie Thank you☺🌸
+ 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 🌼
+ 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.
+ 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.
+ 1
@Kinshuk Vasisht you mean I must to put 'Alt+ASCIICode' instead of '\0' ?
- 4
میشه شماریته بتی؟؟؟
- 4
Hakim khan hoshmand my Facebook account massage me😉