Reputation: 105
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?
Upvotes: 2
Views: 4277
Reputation: 4176
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);
Upvotes: 0
Reputation: 2674
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];
}
Upvotes: 4
Reputation: 35154
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];
}
}
Upvotes: 0