Thread: Simple question (for you not for me!)

  1. #1
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32

    Simple question (for you not for me!)

    Hi
    I want to make a programm that takes a text line from the keyboard, and then I want to use the line as a parameter in a function.
    Here is my code:
    Code:
    #include <stdio.h>
    main()
    {
    char line;
    scanf("%s",line);
    
    /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    }
    This code just names line the first word of the line, not the whole line!!!
    What can I do?
    I tried getchar, in a while and stored the text in a array, but I get segmentations and don't know how to use tha whole array as a parameter in my function.

    Thx for your help,
    antonis
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  2. #2
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Code:
    scanf("%s",line);
    change line to &line

  3. #3
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Code:
    #include <stdio.h>
    main()
    {
    char *line;
    scanf("%s",&line);
    
    /*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    printf("%s",&line);
    Here,
    but I still get just the first word of the text.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    a char is one character of text. If you want the whole word then use a char array (to form a string). The printf line should be line, why are you using the assignment operator there?

  5. #5
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    I use the assigment operator at printf because else I get segmentation. But actually I dont need to read and write a line(so printf is not actually needed), I can do this by another way:
    Code:
    while(c=getchar()!='\n'){putchar(c);}
    or smth like that. I need to use the whole line as a parameter in a function.
    I don't want a whole word but a whole line of text consisting of more words. for example if i type:
    antonis has just finished his post,
    and use line as a parameter in my function, It only uses the word antonis. And the function is OK, I am sure about this.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you want the whole line you can use gets()
    Code:
    #include <stdio.h>
    int main()
    {
    	char line[256];
    	gets(line);
    
    	/*NOW USE THE TEXT LINE WHILE CALLING A FUNCTION*/
    	printf("%s",line);
    	return 0;
    }

  7. #7
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    1. Problem: You need to reserve memory for your line of text. Do that either by creating an array or by using malloc() together with your char *line.
    2. Problem: You will not get more than the first word of your input when using scanf(). To read a complete line use fgets().

    Read your manual on how to use the functions I mentioned.

  8. #8
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    try using std::cin.getline(line, 256); That should get you the whole line instead of just the first word. The reason for this in scanf is because it registers a space as an EOF delimiter or '\0', it's mainly made for parsing files. std::cin.getline() will get you the text until the enter key is pressed.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  9. #9
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    @Quantum:Your code gives this while compiling
    Code:
    /home/plutonas/tmp/ccADhe1V.o(.text+0x2a): In function `main':
    : warning: the `gets' function is dangerous and should not be used.
    and for fgets, I understand that it has toread from a file
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    like TactX said using scanf will only give you the first word if you use %s:

    scanf("%s",line); = first word


    So use fgets:

    fgets(line, sizeof(line), stdin);

    Or somthing like that.

  11. #11
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    @durban: what do the std::cin. mean with std::cin.getline(line, 256);it doesnt get compiled. When using just getline(line, 256); I get segmentation immidiatelly when I run the prog.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    cin.getline is C++ not C, dont use it in C code :P

  13. #13
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Thanks barnzey it now worked. Just one more question stdin means it reads from the keyboard????
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    stdin is from the command line.

  15. #15
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Code:
            else if (l=='b'){
                    /*Dialog to insert a new line before current line.*/
                    printf("Insert the new line:\nTEEDI>");
                    fgets(newline, MAXLINE,stdin);
                    InsertBefore(buffer, newline);
            }
    here there is the code in my programm. I don't get a prompt to write the line! Why???? How can I fix it?
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM