-3

I am new to C++ and I have a dynamic char array with a max size of 30. I need to extract the name from the array into a string but i have no way of knowing how long the name is.

For example someone named Bob should look like: Bob____________________ (_ is blank space)

but it instead reads like: Bob%#$(%$#)%@#*#@$$#

or something of the sort. I know its doing that because its just random unasigned memory, but how can i cut it off at the end of bob so i can add the blank spaces manually?

In case it is unclear i am reading the letters character by character using a for loop that runs 30 times exactly.

this is my current loop

string n = "";
  for(int i=0; i<MAX_NAME_LEN; i++)
  {
     n += name[i];
  }

In this case n should be equal to "Bob___________________"

but it is Bob and a bunch of random crap as stated earlier.

Josh
  • 187
  • 1
  • 1
  • 9
  • Where are you getting this array from? – user657267 Oct 08 '15 at 00:06
  • I created it. It is char *name = new char[30]; – Josh Oct 08 '15 at 00:07
  • 1
    Where's it getting its input from? Why is it not null terminated? Why isn't it a `std::string`? – user657267 Oct 08 '15 at 00:09
  • OK null terminated is not someething i am familar with, i assume that would be like ending the loop when it reaches white space. Could you explain this to me? – Josh Oct 08 '15 at 00:11
  • End the loop when it reaches white space. – Karoly Horvath Oct 08 '15 at 00:12
  • @Josh: are you familiar with google search? – Karoly Horvath Oct 08 '15 at 00:13
  • To be clear the root of your issue is in the code you aren't showing, I have the sneaking suspicion you have zero need for the `char` array in the first place, but if you don't know what a null terminated string is then I suggest you read the first few chapters of an [introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user657267 Oct 08 '15 at 00:15
  • haha you are right. I transfered schools and here all coding is in C++ which i have never done before whereas i have already learned years of java so theres a lot of things they expect us to know from previous years which i have never heard of before. – Josh Oct 08 '15 at 00:18
  • From what i have researched the way i see it is i should check if the current value is 0 and if so break the loop. Thank you – Josh Oct 08 '15 at 00:24
  • how about `string n(name)` ? – Karoly Horvath Oct 08 '15 at 00:26
  • 1
    First you should take a look at this: http://www.cplusplus.com/doc/tutorial/ntcs/ – ProXicT Oct 08 '15 at 00:33
  • Thank you! That literally explains it perfectly. – Josh Oct 08 '15 at 00:35

1 Answers1

0

IF you read this caracters from a array of char so a C string you can do something like this(it should be a null terminated string '\0').

n = std::string(name)

And affter that you can add the number of time your want a white space in your string. In my code I want a 30 caracter max with the name included. So it can be someting like that.

if(n.size() < 30)
    n = n + std::string(30-n.size(), ' ');
Captain Wise
  • 480
  • 3
  • 13