0

I want to read the data: sample text opp I see this: sample (enter) text (enter) opp (enter)

However, my code does not work well.

#include <stdio.h>
#include <stdlib.h>
int main(){
    char separator[] = " ";
    char *schowek;
    char *wejscie;

    gets(&wejscie);
    schowek = strtok(&wejscie,separator);

    while( schowek != NULL )
    {
        printf( "%s\n", schowek );
        schowek = strtok( NULL, separator );
    }

    return 0;
}

Ok, I have this code.

#include <stdio.h>
#include <stdlib.h>

int main()
{

    char * slowo[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred",
"thousand", "million"};
    int liczba[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,1000,1000000};

    char n[]="";
    int i=0;

    char s1[]="zero";


    char separator[] = " ";

    char wejscie[1024];
    if (fgets(wejscie, 1024, stdin))
    {
        char* schowek = strtok(wejscie,separator); /* Removed '&'. */

        if(strcmp(wejscie,n)==0)
        {
            exit;
        }

        while (schowek)
        {
            printf("%s\n", schowek);
            schowek = strtok(NULL, separator);
        }
    }
    return 0;
}

Is everything alright with him? Now I want to convert string to number.

Sample Input

six negative seven hundred twenty nine one million one hundred one

Sample Output

6 -729 1000101

How can I do that?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
lukassz
  • 3,135
  • 7
  • 32
  • 72
  • I'm almost positive, someone else may need to confirm, but I believe `gets()` is a huge problem. `gets()` will read as many characters are there are until a `\n`, which can read more than the buffer causing problems. I believe it's standard to use `fgets()`. – Seth Kania Mar 21 '13 at 12:18

3 Answers3

3

wejscie is an unitialised pointer and gets() is writing to memory that it should not be, probably causing a segmentation fault. Note that gets() is susceptible to buffer overruns as there it provides no facility to limit the number of characters read (see warning:gets function is dangerous). Use fgets() instead and a char[]:

char wejscie[1024];
if (fgets(wejscie, 1024, stdin))
{
    char* schowek = strtok(wejscie,separator); /* Removed '&'. */
    while (schowek)
    {
        printf("%s\n", schowek);
        schowek = strtok(NULL, separator);
    }
}

While not relevent to this program, just to note that strtok() is not thread safe.

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

You did not allocate a buffer to hold the read data.

 char *wejscie;

Allocates a pointer to hoild the address of your buffer, but not the actual buffer.

 gets(&wejscie);

reads into that pointer, not into a big enough buffer.

This is the way to do it (allocates the buffer on the stack):

 char buffer[1024];
 fgets(buffer, sizeof(buffer), stdin);

If you wish to use a pointer and dynamic memory then this is the way to do it:

 char * wejscie = malloc(1024);
 fgets(wejscie, 1024, stdin);
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Don't encourage use of gets(), it has been removed in C11. –  Mar 21 '13 at 12:16
  • wasn't aware of that. I'm still in C99 – Sergey L. Mar 21 '13 at 12:19
  • Do [not](http://stackoverflow.com/questions/2843073/warninggets-function-is-dangerous) [use](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-is-dangerous-why-should-not-be-used) gets. –  Mar 21 '13 at 12:26
0

You dont have memory to store data. Quick example:

char wejscie[256];
gets(wejscie);

Now wejscie is pointing to valid block of memory.

yattering
  • 436
  • 4
  • 7
  • `wejscie` is the symbol for the buffer address, not `&wejscie` which is the address of that symbol. – Sergey L. Mar 21 '13 at 12:17
  • @SergeyL. With arrays: wejscie == &wejscie. –  Mar 21 '13 at 12:19
  • @Armin: No! `wejscie` and `@wejscie` are different, namely on their type. `(wejscie) + 1 != (@wejscie) + 1`. – pmg Apr 03 '13 at 12:02
  • @pmg Back then i didn't realize that :-s, i saw someone with high reputation +50000 saying that they are equal and i believed it without checking. –  Apr 03 '13 at 12:45