Oct 3, 2013 at 12:20pm UTC
I am just looking for a hint i will admit this is homework but i'm not understanding the problem with the program as i've searched online ,in the archives, and have tried to fix it many different ways.
Oct 3, 2013 at 12:33pm UTC
statements such as
should be followed with a call to
cin >> length
to remove the newline from the input stream.
Oct 3, 2013 at 12:36pm UTC
Or alternatively you could use std::string and get rid of providing expicit length.
Oct 3, 2013 at 12:51pm UTC
MiiNiPaa it has to be an array saddly. i would love to use string and be done with it.
Yanson your help almost compleatly fixed the problem, when i enter the int for length say 8 and then for the string type "what what" it prints "WHAT WH"
any idea on why or a way to solve this?
Oct 3, 2013 at 12:56pm UTC
well, it does read 7 characters + 1 for terminating 0 = 8, as you specified. For "what what" string you need 10 characters at least.
Oct 3, 2013 at 1:55pm UTC
this is going to sound dumb but why is that? w(1)h(2)a(3)t(4) (5)w(6)h(7)a(8)t(9) why wouldn't it be 9? i need to take my time when counting... thinking it would be 8.
Oct 3, 2013 at 1:59pm UTC
Do not forget about terminating 0 which should ne included in array size.
String "what what" actually looks like:
w h a t _ w h a t \0
119 104 97 116 32 119 104 97 116 0
So you either have to pass 10 as size of said string or pass 9 and use
new char[length + 1];
Last edited on
Oct 3, 2013 at 2:00pm UTC
Oct 3, 2013 at 3:14pm UTC
So what would be a way to make it so it always comes out right? no matter the length?
Oct 3, 2013 at 3:44pm UTC
What do you mean by "always correct"? I pointed out how c-string are working. If you want to get rid of length, you need to use dynamic storage, like std::string, or make something similar.
Oct 3, 2013 at 4:54pm UTC
ooo ok well in that case thank you so much for your help and explaining :)