char array storing too many characters? (c++) - c++

Doing a message program thing, and ran into a problem I don't understand with the buffer. The answer i'm sure is really simple but I just can't wrap my head around it. I'm trying to ask the user to type in a 10 digit name that will work as a username.
char myID[11]; // stores 10 characters plus newline at the end
char sendBuff[1024]
cout << "Enter your nickname(10 digits): ";
cin >> myID;
cin.ignore(1000, '\n'); //clears buffer, or so I thought
Then I get some actual text for the message, store it in tempCharArray, then combine the username and the message text using sprintf_s.
char tempCharArray[1024];
cin.getline( tempCharArray, 1024 );
sprintf_s(sendBuff, 1024, "%s: %s", myID, tempCharArray ); //combines username and message text into one line before being printed
Now, my problem is that if I type in a name longer than 10 digits, it will print out the entire name in the sprintf_s function, despite myID being only 10 characters. I do not at all understand this behavior...
For example if I type in "HelloKitty69420" into myID, and then "yoyoyo" into tempCharArray, I would expect it to print "HelloKitty: yoyoyo" because myID can only hold 10 characters, yet it prints "HelloKitty69420: yoyoyo".
What's going on?

I would comment on your overall strategy for accomplishing your goal (ie. it is definitely no the way I would go about it) but I will focus on an answer.
Firstly, you should use std::cin.width(10) to limit the number of characters actually stored and moved to myID.
Secondly, you seem to have been lucky in the above case. The overflow bytes were written into the second array sendBuff and sprintf was writing characters from both myID and sendBuff.
The reason the overflow bytes were written into the second array is that the compiler simply placed myID and sendBuff in memory regions that were contiguous. ie myID[1000] would be the same as sendBuff[1000-11]

Related

Difference between cin and cin.get() for char array

I have these 2 codes:
char a[256];
cin>>a;
cout<<a;
and
char a[256];
cin.get(a,256);cin.get();
cout<<a;
and maybe, relative to the second one without cin.get();
char a[256];
cin.get(a,256);
cout<<a;
My question is (first one) : for a char array, what should i use? cin or cin.get()? And why should i use cin.get(); with no parameter after my char initialisation?
And my second question is: my c++ teacher taught me to use every time cin.get() for initialisation chars and AFTER every initialisation char array or int array or just int or whatever, to again put cin.get(); after it. That's what i wanted to ask initially.
So, now i got these 2:
In this case, without cin.get() after the integer initialisation, my program will break and i can't do anymore my char initialisation.
int n;
cin>>n;
char a[256];
cin.get(a,256); cin.get(); // with or without cin.get();?
cout<<a;
And the correct one:
int n;
cin>>n; cin.get();
char a[256];
cin.get(a,256); cin.get(); // again, with or without?
cout<<a;
So, what's the matter? Please someone explain for every case ! Thank you.
They do different things, so choose whichever does what you want, or the better alternatives given below.
The first cin>>a; reads a single word, skipping over any leading space characters, and stopping when it encounters a space character (which includes the end of the line).
The second cin.get(a,256);cin.get(); reads a whole line, then consumes the end-of-line character so that repeating this will read the next line. cin.getline(a,256) is a slightly neater way to do this.
The third cin.get(a,256) reads a whole line but leaves the end-of-line character in the stream, so that repeating this will give no more input.
In each case, you'll get some kind of ill behaviour if the input word/line is longer than the fixed-size buffer. For that reason, you should usually use a friendlier string type:
std::string a;
std::cin >> a; // single word
std::getline(std::cin, a); // whole line
The string will grow to accommodate any amount of input.
The problem, most likely, is in the way you enter the values later on. The cin.get() after every initialization is there to "grab" the newline character that gets put in the stream every time you press enter.
Say you start entering your values like this:
2
a b c d...
Assuming you have pressed enter after 2, the newline character was also put on the stream. When you call cin.get() after that, it will grab and discard the newline character, allowing the rest of your code to properly get the input.
To answer your first question, for an array, you should use cin.get instead of the overloaded operator >> cin>> as that would only grab a single word, and it would not limit the amount of characters grabbed, which could lead to an overflow and data corruptions / program crashing.
On the other hand, cin.get() allows you to specify the maximum number of characters read, preventing such bugs.
For a char array use cin.get() because it counts whitespace whereas cin does not. More importantly, cin.get() sets the maximum number of characters to read. For example:
char foo[25]; //set maximum number of characters
cout << "Please type in characters for foo" << endl;
cin.get(foo,25);
cout << ' ' << foo;
In this case, you can only type in 24 characters and a null terminator character \0.
I hope this answers your first question.
I would personally use a string.

How can you ignore from cin until it goes to the next line?

If you have a text file that you're reading character by character with cin:
char text;
cin >> text;
cout << char << endl;
Suppose you want to ignore any lines that start with ">" until the new line, how can you do that?
You can compare the char read for '>' using::
int strncmp ( const char * str1, const char * str2, size_t num );
If found, skip till char read equals '\n' i.e., skip till strncmp returns 0 for ( char, '\n', 1 )
Typically you want to use std::istream::ignore, something like:
static const int max_line = 65536;
if (text == ">")
std::cin.ignore(max_line, '\n');
Note that I've specified a maximum distance to skip of 64K bytes. Many people recommend something like std::numeric_limits<std::streamsize>::max(), which basically means to skip any amount of text until you find the delimiter (new-line, in this case).
IMO, specifying such a huge number is usually a poor idea -- if you go for too long without seeing a new-line, it's safe to stop and assume that you've gotten bad data. As soon as you've read enough to be reasonably certain there's a problem, it's better to stop and warn the user, rather than spending minutes with the program apparently locked up, reading gigabytes of useless data (and then probably giving the user an error message anyway).
Another possibility (especially if you're fairly sure you'll get good input) is to start by reading a full line (e.g., with std::getline), then if it starts with a >, just skip processing the line, and go back to read the next one.

C++ Infinite Loop

I'm learning C++.
nav is an integer .
I want to ask user for typing a valid value, if he / she type an invalid value.
void main()
{
printf("Type an integer : ");
if(!scanf("%d", &nav))
{
system("cls");
printf("Invalid ! \n");
main();
}
}
But it's blinking after typing first value . It's blinking like reloading screen. I think it's infinite loop.
How can i do it in right way ? I want to ask a number from users, until it's typing a real number .
If the user types an invalid input, scanf() won't consume it, and you'll be left peeking the same offending input character forever. You need to first read whatever the user enters — I recommend using std::getline() — and then try to parse that with strtol(), sscanf() or std::istringstream. Don't use atoi() because it doesn't report failures.
int nav;
{
string line;
while (getline(cin, line))
if (istringstream(line) >> nav)
break;
}
EDIT: See the comments for a rather beautiful rendition of the above logic. I've left it out of the answer because: a) I don't want to steal someone else's idea, and b) I'm not sure I'd present a newcomer to C++ with that formulation — not in one go, at least.
P.S.: You can't call main() in C++.
Two things.
Scanf needs the 'enter' key pressed before it will process input, so the blinking could just be the cursor waiting for the next key.
Also, calling main from main is fairly non-standard. You should look into a 'while' loop.
Your program is not a good one, but nevertheless I will tell you what is happening.
In C/C++, when scanf cannot read an integer (%d) from input, it wouldn't read anything. That is, whatever prevented scanf from reading an int, will remain there. On the next scanf the same character is going to cause an error.
Let me demonstrate by an example. Imagine you are reading many integers from this input:
12 13 Shahbaz 15
Now if you call scanf with %d, you will read 12 and the input would be:
13 Shahbaz 15
Next, you call scanf with %d and you will read 13. now the input would be:
Shahbaz 15
Again, you call scanf with %d. Here, the input begins with an S (after the whitespace) which makes scanf return with a failure as it could not read an integer. The input is left untouched (save perhaps for the whitespace). That is, the input will be:
Shahbaz 15
As you can see, reading the input with %d will give you the exact same error and you are stuck in an infinite loop.
To solve this, you have many choices. This very much depends on how you want to handle the situation, but two methods would be to either read a character (with %c) or a string (with %s) right after printing printf("Invalid\n").
The first method is good for handling input like this:
12 13 q14 15
where q is a mistake that needs to be ignored. The second method is good for handling input like this:
12 13 Shahbaz 15
where the invalid data are meaningful words, but you just want to ignore them.
And the way I would write it, if I wanted to use scanf would be:
int main() // always write int main
{
int nav;
printf("Type an integer: ");
while (scanf("%d", &nav) != 1) // scanf returns number of successful %'s read
{
printf("Invalid number. Try again: ");
scanf("%*s"); // read a %s but ignore it
}
// The rest of the program, using nav
return 0;
}

Weird problem with string function

I'm having a weird problem with the following function, which returns a string with all the characters in it after a certain point:
string after(int after, string word) {
char temp[word.size() - after];
cout << word.size() - after << endl; //output here is as expected
for(int a = 0; a < (word.size() - after); a++) {
cout << word[a + after]; //and so is this
temp[a] = word[a + after];
cout << temp[a]; //and this
}
cout << endl << temp << endl; //but output here does not always match what I want
string returnString = temp;
return returnString;
}
The thing is, when the returned string is 7 chars or less, it works just as expected. When the returned string is 8 chars or more, then it starts spewing nonsense at the end of the expected output. For example, the lines
cout << after(1, "12345678") << endl;
cout << after(1, "123456789") << endl;
gives an output of:
7
22334455667788
2345678
2345678
8
2233445566778899
23456789�,�D~
23456789�,�D~
What can I do to fix this error, and are there any default C++ functions that can do this for me?
Use the std::string::substr library function.
std::string s = "12345678";
std::cout << s.substr (1) << '\n'; // => 2345678
s = "123456789";
std::cout << s.substr (1) << '\n'; // 23456789
The behavior you're describing would be expected if you copy the characters into the string but forget to tack a null character at the end to terminate the string. Try adding a null character to the end after the loop, and make sure you allocate enough space (one more character) for the null character. Or, better, use the string constructor overload which accepts not just a char * but also a length.
Or, even better std::string::substr -- it will be easier and probably more efficient.
string after(int after, string word) {
return word.substr (after);
}
BTW, you don't need an after method, since exactly what you want already exists on the string class.
Now, to answer your specific question about why this only showed up on the 8th and later characters, it's important to understand how "C" strings work. A "C" string is a sequence of bytes which is terminated by a null (0) character. Library functions (like the string constructor you use to copy temp into a string instance which takes a char *) will start reading from the first character (temp[0]) and will keep reading until the end, where "the end" is the first null character, not the size of the memory allocation. For example, if temp is 6 characters long but you fill up all 6 characters, then a library function reading that string to "the end" will read the first 6 characters and then keep going (past the end of the allocated memory!) until it finds a null character or the program crashes (e.g. due to trying to access an invalid memory location).
Sometimes you may get lucky: if temp was 6 characters long and the first byte in memory after the end of your allocation happened to be a zero, then everything would work fine. If however the byte after the end of your allocation happened to be non-zero, then you'd see garbage characters. Although it's not random (often the same bytes will be there every time since they're filled by operations like previous method calls which are consistent from run to run of your program), but if you're accessing uninitialized memory there's no way of knowing what you'll find there. In a bounds checking environment (e.g. Java or C# or C++'s string class), an attempt to read beyond the bounds of an allocation will throw an exception. But "C" strings don't know where their end is, leaving them vulnerable to problems like the one you saw, or more nefarious problems like buffer overflows.
Finally, a logical follow-up question you'd probably ask: why exactly 8 bytes? Since you're trying to access memory that you didn't allocate and didn't initialize, whats in that RAM is what the previous user of that RAM left there. On 32-bit and 64-bit machines, memory is generally allocated in 4- or 8-byte chunks. So it's likely that the previous user of that memory location stored 8 bytes of zeroes there (e.g. one 64-bit integer zero) zeros there. But the next location in memory had something different left there by the previous user. Hence your garbage characters.
Moral of the story: when using "C" strings, be very careful about your null terminators and buffer lengths!
Your string temp is not NULL terminated. You requite temp[a] = '\0'; at the end of loop. Also you need to allocate word.size() - after + 1 chars so as to accomodate the NULL character.
You're not null-terminating your char array. C-style strings (i.e., char arrays) need to have a null character (i.e., '\0') at the end so functions using them know when to stop.
I think this is basically your after() function, modulo some fudging of indexes:
string after(int after, string word) {
return word.substring(after);
}

How to check the length of an input? (C++)

I have a program that allows the user to enter a level number, and then it plays that level:
char lvlinput[4];
std::cin.getline(lvlinput, 4)
char param_str[20] = "levelplayer.exe "
strcat_s(param_str, 20, lvlinput);
system(param_str);
And the level data is stored in folders \001, \002, \003, etc., etc. However, I have no way of telling whether the user entered three digits, ie: 1, 01, or 001. And all of the folders are listed as three digit numbers. I can't just check the length of the lvlinput string because it's an array, so How could I make sure the user entered three digits?
Why not use std::string?
This makes storage, concatenation, and modification much easier.
If you need a c-style string after, use: my_string.c_str()
Here is a hint: To make your input 3 characters long, use std::insert to prefix your number with 0's.
You are really asking the wrong question. Investigate the C++ std::string class and then come back here.
Eh? Why do they need to enter 3 digits? Why not just pad it if they don't? If you really want to check that they entered 3 digits, use strlen. But what I recommend you do is atoi their input, and then sprintf(cmd, "levelplayer.exe %03d", lvlinput_as_integer)
Here's how you could do this in C++:
std::string lvlinput;
std::getline(std::cin, lvlinput);
if (lvlinput.size() > 3) { // if the input is too long, there's nothing we can do
throw std::exception("input string too long");
}
while (lvlinput.size() < 3) { // if it is too short, we can fix it by prepending zeroes
lvlinput = "0" + lvlinput;
}
std::string param_str = "levelplayer.exe ";
param_str += lvlinput;
system(param_str.c_str());
You've got a nice string class which takes care of concatenation, length and all those other fiddly things for you. So use it.
Note that I use std::getline instead of cin.getline. The latter writes the input to a char array, while the former writes to a proper string.
What do you mean you can't check the length of the string? getline generates a NULL terminated c-string so just use strlen(lvlinput).
Neil told you where you should start, your code might look like this.
std::string level, game = "levelplayer.exe ";
std::cout << "Enter the level number : ";
std::cin >> level;
if(level.size() != 3)
{
// Error!
}
else
{
// if you have more processing, it goes here :)
game += level;
std::system(game.c_str());
}
You can check the length of your NULL terminated string that getline returns by using:
int len = strlen(lvlinput);
This works because getline returns a NULL-terminated string.
However, this is besides the point to your problem. If you want to stay away from std::string (and there isn't any particular reason why you should in this case), then you should just convert the string to an integer, and use the integer to construct the command that goes to the system file:
char lvlinput[4];
std::cincin.getline(lvlinput, 4);
char param_str[20];
snprintf(param_str, 20, "levelplayer.exe %03d", atoi(lvlinput));
system(param_str);