Problem with cout char array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Problem with cout char array

char words[5][20] = {"ball", "earth", "government", "environment", "someone"}; cout << words[3]; when using g++ on PC it prints: environment @ @ shouldn't appear at all, all other words print fine, index 3 is problem no matter the word. Google didn't help

8th Feb 2018, 8:12 PM
Elva
Elva - avatar
8 Answers
+ 5
I also tried debugging, when index is 3, in gdb I don't see @, but cout magicaly prints it... also via gdb I printed all possible addresses and values, all good... I'll try tomorrow again to see what exactly is messing up my output
8th Feb 2018, 11:51 PM
Elva
Elva - avatar
+ 4
logic is correct from what is tested, only problem is 3rd index of array, no matter what's the word inside it, so it can't be logic problem
8th Feb 2018, 10:31 PM
Elva
Elva - avatar
+ 4
I'm using char arrs simply to understand pointers and how they work in memory I'll move to strings later... maybe you're right I'll check that piece of code out (and I understand exactly what you wrote) but it works flawlessly when rand()%5 is not 3.... anyways thank you so much 😀
8th Feb 2018, 10:56 PM
Elva
Elva - avatar
+ 4
hey guys/girls, I made slight modification (Martin Taylor's advice that something is wrong with pointers so I got rid of them) and now you can see problem even on SoloLearn's Compiler, just input single char... could it be string terminator?
9th Feb 2018, 9:28 AM
Elva
Elva - avatar
+ 3
sorry, it's on my native lang, it's hangman, just input chars, you'll get it 😀 https://code.sololearn.com/cfl1k1RObw2e/?ref=app
8th Feb 2018, 8:43 PM
Elva
Elva - avatar
+ 1
good luck with that :)
8th Feb 2018, 9:04 PM
---
--- - avatar
0
Hi, can you share your code pls ?
8th Feb 2018, 8:40 PM
---
--- - avatar