Issues with input

I'm trying to enter strings to use for a command interface through hyperterminal, but having issues with the second "gets" function. It lets me enter a command of greater than 4 characters and waits for ENTER to move to the next prinf, but once I try and enter the name with the second "gets" function, it immediately moves on once 4 characters have been entered, it will not allow anything greater than 4 characters... it's as if I hit ENTER again. This isn't all of my code, but what should be needed to help determine the issue:

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
46
47
//strcmp definition
int strcmp (  char* first,
		      char* second
)
{
    int RES;
	int i;

	for(i; i < 20; i++){
	if(first[i] == second [i]) RES = 1;
	else RES = 0;
	}

	if(RES) return 0;
	if(!RES) return 1;

}

//IN MAIN CODE:
        char mkdir [20] = "mkdir";
	char fopen [20] = "fopen";
	char rmdir [20] = "rmdir";
	char rmfile [20] = "rmfile";



	printf("\nPlease input the type of command you would like to perform: \n");

	gets (CMD);

	printf("\nNow enter the name of the file or directory: \n");

	gets (NM); 

	

        if(!strcmp(CMD, mkdir)){

	rc = f_mkdir(NM);
	if (rc) die(rc);
	}

	if((!strcmp(CMD, rmdir)||(!strcmp(CMD, rmfile)))){

	rc = f_unlink(NM);
	if (rc) die(rc);
	}


here's a good article on why you shouldn't use gets():
http://www.gidnetwork.com/b-56.html

and why did you need to redefine strcmp()? http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
Typically it is conventional to use uppercase identifiers for constants in C.

Besides that observation you could just use scanf(CMD); assuming CMD is defined as char*. Is it?
Awesome, I'll look into both of these. I did notice I had some declarations and things wrong, wrote this in a few minutes trying to get a quick test in on an embedded system.

I had to define strcmp because it didn't appear to be an included function. I know it is in C++ but I'm rather new to C so I wasn't sure. Also, I'm using Keil uVision 3, not sure if it has different libraries?

Anyway, I'll try these out, thanks for the help!
closed account (zb0S216C)
Johnnystarr is right. Coding conventions will help with code readability, to some extent. However, coding conventions are more strictly used when working in teams, as it can become confusing to other team members when a convention changes.

Wazzak
Topic archived. No new replies allowed.