2

After entering some letters to myString, I get an output containing exact letters as myString with some weird, scrambled up characters. For example I type in "letters" and the output I have is the following:

letters ÷ìu ╝■( Bñìu¿☻÷u³■( ʶì Ä◄ÿub◄ÿu←öïÜ ÉpB º■( ( ─ ( î¯u Å{N´■   b◄ÿu─[uÉpB ö ( ¯pB ÉpB P4å Ç

Also this weird characters appear different with different inputs, but they don't change after a rebuild with the same input.

Here is the code I have wrote:

#include <iostream> using namespace std; int main() { constexpr int BUFFER_SIZE = 128; char myString[BUFFER_SIZE + 1] = {}; // + 1 for null. Initialize all with null. cout << "Enter a string: "; fgets(myString, BUFFER_SIZE, stdin); int myString_size = sizeof(myString); for (int i = 0; i < myString_size; i++) { cout << myString[i]; } system("PAUSE"); return 0; } 

Now, I know that fgets() function will place an new-line character in the end of letters but why those characters still appear?

10
  • 4
    a simple hint : sizeof is not returning the length of the string Commented Aug 26, 2018 at 17:15
  • ...and you can't go one-past end anyway. Commented Aug 26, 2018 at 17:15
  • So @gogaz what sizeof actually returns is 128, am I right? Commented Aug 26, 2018 at 17:18
  • @FeritYiğitBALABAN no sizeof returns the # of bytes for the datatype. Commented Aug 26, 2018 at 17:19
  • 1
    @gogaz It returns the size of the array, not length of the c-style string. The OP should use strlen()rather than sizeof() to determine the size of all characters entered. Commented Aug 26, 2018 at 17:19

3 Answers 3

4

A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself). To get the length of a C string, use:

size_t myString_size = strlen(myString); 

Than, the loop should looks something like:

for (size_t i = 0; i < myString_size; i++) { cout << myString[i]; } 
Sign up to request clarification or add additional context in comments.

Comments

0

You reserve a buffer myString of 128 bytes, which is not initialized in the course of its declaration (i.e. it may contain arbitrary values for each of the 128 bytes). Each of these arbitrary values may yield "something weird" when output to console.

When you enter a string then, e.g. "Hello!", when only the first portion of myString will be assigned values, i.e. H, e, ... , !, \n, \0. All others will remain as they have been before fgets, i.e. they still may contain "weird stuff".

In your loop, you print all the 128 characters, i.e. also the weird ones. BTW: with <=sizeof(myString), you go one behind the end of the buffer and access non-reserved memory.

To overcome this, try the following:

int main() { char myString[128]; cout << "Enter a string: "; fgets(myString, 128, stdin); size_t myString_size = strlen(myString); for (size_t i= 0; i < myString_size; i++) { cout << myString[i]; } } 

Or:

#include <string.h> int main() { char myString[128]; cout << "Enter a string: "; fgets(myString, 128, stdin); cout << myString; } 

Or:

int main() { char myString[128]; cout << "Enter a string: "; fgets(myString, 128, stdin); size_t myString_size = strlen(myString); for (size_t i= 0; i < myString_size && myString[i] != '\0'; i++) { // stop when reaching the end of the string cout << myString[i]; } } 

2 Comments

I did size_t myString_size = (unsigned)strlen(myString); but now I am getting an error messsage saying : strlen was not declared in this scope. I added string header but why this error?
Did you #include <string.h> (note the .h)?
0

sizeof(myString) returns the size of the buffer char[128] (in bytes), not the size of the string. To iterate through the characters of the string you can use a loop like this:

for (char *c = &myString[0]; *c != '\0' ; ++c) std::cout << c; 

or cast to std::string:

for (char c : std::string(myString)) std::cout << c; 

or more simply:

std::cout << std::string(myString); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.