Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Queries and Discussion > Programming > C-C++

Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Thread Tools Search this Thread Display Modes
Old 06-29-2007, 09:28 PM   #1
Newbie Member
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
hammil is on a distinguished road
Question

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();
}


I use windows XP.

Hamish
hammil is offline   Reply With Quote
Old 06-29-2007, 10:17 PM   #2
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 12,744
Thanks: 130
Thanked 294 Times in 228 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

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);
shabbir is offline   Reply With Quote
Old 06-29-2007, 10:49 PM   #3
Team Leader
 
DaWei's Avatar
 
Join Date: Dec 2006
Location: Texan now in Central NY
Posts: 835
Thanks: 0
Thanked 3 Times in 2 Posts
Rep Power: 4
DaWei is on a distinguished road

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.
__________________
DaWei on Pointers Grumpy on C++ Exceptions
Functionality rules; clarity matters. If you can work a little elegance in there, you're stylin'.
DaWei is offline   Reply With Quote
Old 06-29-2007, 11:21 PM   #4
Newbie Member
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
hammil is on a distinguished road

Re: If [user input]


OK, so is there any way to split the inputted string into an array, like php's str_split?
hammil is offline   Reply With Quote
Old 06-30-2007, 03:47 AM   #5
Team Leader
 
DaWei's Avatar
 
Join Date: Dec 2006
Location: Texan now in Central NY
Posts: 835
Thanks: 0
Thanked 3 Times in 2 Posts
Rep Power: 4
DaWei is on a distinguished road

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>
__________________
DaWei on Pointers Grumpy on C++ Exceptions
Functionality rules; clarity matters. If you can work a little elegance in there, you're stylin'.
DaWei is offline   Reply With Quote
Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

 

All times are GMT +5.5. The time now is 01:45 AM.