Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   If [user input] (http://www.go4expert.com/forums/if-user-input-t4993/)

hammil 29Jun2007 21:28

If [user input]
 
Hi
I'm a bit of a noob at c, so I was wondering why the following does not work...

Code:

#include <stdio.h>
void end(){
 int x;
 gets(x);
 exit(0);
}
void main(){
 char c;
 char i;
 c = "Code";
 gets(i);
 if(c==i){
  printf("\r\nyes\r\n");
 }else{
  printf("\r\nno\r\n");
 }
 end();
}

:confused: :confused: :confused:

I use windows XP.

Hamish :D

shabbir 29Jun2007 22:17

Re: If [user input]
 
Why do you expect it to work and what do you expect from the following program.

There are lots of errors
Code:

gets(i);
Code:

c = "Code";
Code:

int x;
 gets(x);


DaWei 29Jun2007 22:49

Re: If [user input]
 
Really, your errors range from uninformed to eggregious.

1) main does not return void, it returns an int. Compilers that allow a void return are not standards compliant, tutorials that use it are in error.

2) Read the documentation for the funcions you use. For instance, gets:
Quote:

The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character.

Security Note
Because there is no way to limit the number of characters read by gets, untrusted input can easily cause buffer overruns. Use fgets instead.

3) c is a char. You said so in your declaration. "Code" is 5 chars. 'C', 'o', 'd', 'e', and '\0'. The latter is called a string (C string) and is indicated by double quotes rather than single quotes. A C string is an array. One cannot assign to arrays in their entirety at run time, though some compiler actions may make it seem so at compile time. Neither can C strings be compared with the "==" operator. One must use the functions declared in string.h and defined in the crt library.

See other parts of this forum for recommendation on good tutorials and books. Get a good compiler. Turn on all warnings and errors. Read the documentation for functions you use. Pay particular attention to the parts about how they fail and what to do about it.

hammil 29Jun2007 23:21

Re: If [user input]
 
OK, so is there any way to split the inputted string into an array, like php's str_split?

DaWei 30Jun2007 03:47

Re: If [user input]
 
Here's one possible method. Study the functions used. Consider that there may be more secure versions for some of the functions. These are things one thinks about when one designs a solution to a problem. There are many alternatives. Design before coding, commensurate with your requirements.
Code:

#include <stdio.h>
#include <string.h>

#define MAXLEN 255
#define MAXWORDS 10

int ohHell (char *trouble)
{
    fprintf (stderr, "%s\n", trouble);
    return EOF;
}
int main()
{
    int count = 0;
    int i;
    char line [MAXLEN];
    char words [MAXWORDS][MAXLEN];
    char* newWord;

    printf ("Enter up to 10 words, separated by white space, on one line:\n");
    if (!fgets (line, MAXLEN, stdin)) return ohHell ("Failure on input");

    newWord = strtok (line, " \t\r\n");
    while (newWord != NULL)
    {
        strcpy (words [count++], newWord);
        if (count >= 10) break;
        newWord = strtok (NULL, " \t\r\n");
    }
    printf ("Contents of the array (%d items):\n", count);
    for (i = 0; i < count; ++i) printf ("%s\n", words [i]);

    return (0);
}

Quote:

Originally Posted by Command window

E:\Documents and Settings\David\My Documents\Visual Studio 2005\Projects\separat
ion\debug>separation
Enter up to 10 words, separated by white space, on one line:
The quick brown fox jumped over the lazy dog
Contents of the array (9 items):
The
quick
brown
fox
jumped
over
the
lazy
dog

E:\Documents and Settings\David\My Documents\Visual Studio 2005\Projects\separat
ion\debug>separation
Enter up to 10 words, separated by white space, on one line:
The quick brown fox jumped over the lazy dog's back time after time
Contents of the array (10 items):
The
quick
brown
fox
jumped
over
the
lazy
dog's
back

E:\Documents and Settings\David\My Documents\Visual Studio 2005\Projects\separat
ion\debug>



All times are GMT +5.5. The time now is 07:30.