Store user input to char array

I want a user to input text, which can include spaces, tabs etc and store it into char array. User can input longer text than my array size. Smaller code preferred. I need to handle that without overflow. Best way I could find was described at http://www.daniweb.com/software-development/c/tutorials/45806/user-input-strings-and-numbers-c , but upon entering a longer text than my first array size it overflows to second array and doesn't ask for input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

/*
================
MAIN
================
*/
int main( int argc, char ** argv )
{
	char	plaintext[ 8 ];
	char	key[ 10 ];

	cout << "plaintext: ";

	fflush( stdout );
	if ( fgets( plaintext, sizeof plaintext, stdin ) != NULL )
	{
		char *newline = strchr( plaintext, '\n' );
		if ( newline != NULL )
		{
			*newline = '\0';
		}
	}

	cout << "key: ";

	fflush( stdout );
	if ( fgets( key, sizeof key, stdin ) != NULL )
	{
		char *newline = strchr( key, '\n' );
		if ( newline != NULL )
		{
			*newline = '\0';
		}
	}


	cout << "plaintext:<" << plaintext << ">" << endl;
	cout << "key:<" << key << ">" << endl;

	return 0;
}

Here's what I get:
1
2
3
plaintext: salkjflasdjflasdj
key: plaintext:<salkjfl>
key:<asdjflasd>


If you have any link to a tutorial which explains how to handle garbage user input without complicated code, help would be much appreciated.
Sorry for my english.
Last edited on
You have to consume the entire first line before reading in the second one.

BTW:
The following has nothing to do with your problem:
1. You're mixing C stdio with C++ iostreams. This may confuse your IO because both systems use different IO buffers. See http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/ for help or better: Only use one of those systems.
2. You want to write a function to read in your character arrays to avoid writing the same code multiple times (line 16-26 and 28-38).
Thanks for reply. I added a function to handle input and made the program C-style only.
Thought I don't understand what you mean by this:
You have to consume the entire first line before reading in the second one.

You mean I should copy first array elsewhere before reading second one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <string.h>

/*
================
input
================
*/
void input( char *text, int size )
{
	fflush( stdout );
	if ( fgets( text, size, stdin ) != NULL )
	{
		char *newline = strchr( text, '\n' );
		if ( newline != NULL )
		{
			*newline = '\0';
		}
		printf("text = \"%s\"\n", text);
	}
}

/*
================
MAIN
================
*/
int main()
{
	char plaintext[ 8 ];
	char key[ 10 ];
	
	input( plaintext, 8 );
	input( key, 10 );
	
	printf( "p: %s\ns: %s\n", plaintext, key );
	
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
static const char EOL = '\n';  // May be defined as '\r' on Macs

/*
================
skipLine
================
*/
void skipLine()
{
    int c;

    while ( (( c = fgetc( stdin )) != EOF ) && ( c != EOL ) )
        ;
}


Add after line 19:

skipLine();


This may work on UNIX and even on WinDOS
Thanks a lot, that worked! (I'm on linux btw)

For anyone with same problem, use this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <string.h>

void input( char *text, int size )
{
	if ( fgets( text, size + 1, stdin ) != NULL )
	{
		char *newline = strchr( text, '\n' );
		if ( newline != NULL )
		{
			*newline = '\0';
		}
		int c;

		while ( (( c = fgetc( stdin )) != EOF ) && ( c != '\n' ) )
			;
	}
}

Might not be perfect (and most probably isn't) but it works.
Last edited on
Topic archived. No new replies allowed.