Probably because you're adding the termination character at the wrong place in the string. You would be better off terminating the string each time you add a character to the string.
1 2
newstr[i] = str[i];
newstr[i + 1] = '\0';
Another option would be to initialize the string to all zeros.
By the way your program really shouldn't compile because you're using a Variable Length Array which is not allowed in C++. Array sizes must be compile time constants.
Why are you using two loops? what you appear to be trying to do can be accomplished with one loop.
I used two loops because I can't figure out a way to test if the last character is a duplicate,since the for loop stops executing when i = 3 the last value never gets compared with the rest of the string,
also what do you mean by variable length array?
so you can't have char array[n] ? how come?
I know you can it when declaring arrays on the heap such as int *array = new int[n];
Correct if n is not a compile time constant. Because the C++ standard states that array sizes must be compile time constants.
I know you can it when declaring arrays on the heap such as int *array = new int[n];
That is correct. However the better option would be to use std::string instead of C-strings and std::vector for most other arrays.
I used two loops because I can't
What exactly are you trying to accomplish? Are you trying to "remove" all duplicate characters or are you trying to populate your "new" string with only the non-duplicated characters, or are you trying to populate the "new" string with only unique characters???
just one quick follow up how come you cam do it when declaring arrays on the heap
The C++ standard doesn't allow creating statically allocated arrays on either the heap or the stack with run time sizes. When you use "new" you are manually allocating the memory and you can use non-run time sizes. But now you, the programmer, are responsible for deleting the memory when it is no longer needed and if you forget to properly delete the memory you will have a memory leak.
also I'm trying to find if duplicates of a letter exist in a string then display the letters
Are you trying to find all duplicates or just adjacent duplicates?
and yes trying to populate the new string with unique characters
So what do you consider unique characters? If your source word is "hello" would the resulting "unique" string be "helo" or "heo". Also consider something like "unique" what would the resulting string contain?
I never knew that,Is that allowed in java? but how come my code runs and compiles with no issues?
and yes it did print heo but I wanted it to print helo,I kind of found an algorithm that works,it does actually print only a character once but it displays it in the wrong order
how come my code runs and compiles with no issues?
Apparently you're using a compiler that both supports this non-standard language extension and doesn't warn you that you're using it and that your program won't work in another compiler.
Your code fragment does not compile when I try to use it:
MSVC 2017 says
consoleapplication1.cpp(6): error C2131: expression did not evaluate to a constant
consoleapplication1.cpp(6): note: failure was caused by non-constant arguments or reference to a non-constant symbol
consoleapplication1.cpp(6): note: see usage of 'size'
prog.cc: In function 'void check(const char*, int)':
prog.cc:6:24: error: ISO C++ forbids variable length array 'newstr' [-Wvla]
char newstr[size+1];
^
prog.cc:6:17: error: variable length arrays are a C99 feature [-Werror,-Wvla-extension]
char newstr[size+1];
^
prog.cc:22:12: warning: variable 'newSize' is uninitialized when used here [-Wuninitialized]
newSize++;
^~~~~~~
Note I had to supply -pedantic-errors to gcc and clang, since those compilers require that commandline option to use portable C++.
Also, don't overlook this comment:
jlb wrote:
the better option would be to use std::string instead of C-strings and std::vector for most other arrays.
I'd say you have no business touching arrays at all until you're comfortable using strings and vectors (and good textbooks follow that order, too)
What difference does it make, you're writing C++ programs not Java programs and in C++ array sizes must be compile time constants.
By the way arrays in Java are objects, which would be the same as a class in C++. And all Java arrays are dynamically allocated, I don't think it is even possible to manually allocate memory in Java.
good point thanks jlb,sorry for asking so many questions just wondering would there be a better way of doing what I'm trying to do but using one loop? you said I could use one loop but this will only check up to the [size-1] element,in my example I then use another while loop to test for duplicates with the last character,is this ok or would you go about another way,