Thread: String trouble

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    String trouble

    I'm new to C programming, but not at all new to programming in general. My professor wants me to use scanf() function to read a string of characters into a variable. I missed a day of class and did not learn how to actually go about this. From what I could get from his lecture and the students around me, I've come up with the following:

    Code:
    char *name;
    char *bufp;
    char buf[BUFL];
    
    printf("Please enter your name: ");
    name = fgets(buf, BUFL, stdin);
    buflen = strlen(name);
    buf[buflen-1] = "\0";
    What I am trying to do is use scanf to put the input into *name. I can do that fine, but it also reads the \n and places it into *name. That is where I know I am going wrong. What I have above is one of many things I tried and failed at.

    How can I read a string using scanf() into a variable, and get rid of the \n (replace it with a NUL byte).

    Thanks.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well, if you want to use scanf(), perhaps you should actually use scanf()...

    Now these two lines:

    Code:
    char *name;
    char *bufp;
    I hope you have pointed these to some real space that you own, and not just try to assign some value to it.

    There are many ways to get rid of the newline...

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    more over it is not a good idea of using a scanf to read a string. is dosn't read after the white space. u will be more clear with the following programe

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void clear_buffer(void);
    
    int main()
    {
            char *str1;
            char *str2;
            
            str1=malloc(25);
            str2=malloc(25);
            
            printf("Enter a string 1\n?");
            scanf("%s",str1);
            
            clear_buffer();
            printf("Enter a string 2\n?");
            scanf("%s",str2);
            
            printf("\nAfter entering the string\n");
            printf("---------------------------\n");
            printf("string 1  -> %s\n",str1);
            printf("string 2  -> %s\n",str2);
            
            clear_buffer();
            getchar();
    }
    
    void clear_buffer(void)
    {
        int ch;
        
        while((ch=getchar())!='\n' && ch!=EOF);
    }           
    
    /*my output
    Enter a string 1
    ?hello there
    Enter a string 2
    ?hellothere
    
    After entering the string
    ---------------------------
    string 1  -> hello
    string 2  -> hellothere
     */
    ssharish2005

  4. #4
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <string.h>
    int main() {
    	char string[10];
    	char name[10];
    	clrscr();
    	printf("Enter String] ");
    	scanf("%s", &string);
    	strcpy(name,string);
    	printf("Your String is %s",name);
    	getch();
    	return 0;
    }
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A classic example of what not to do:
    Code:
    scanf("%s", &string);
    Certainly doing a board search out to turn up several reasons why. Maybe even visit the FAQ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    or you could even use a function to fix gets similar to the one my C programming instructor wrote...
    Code:
    /* This routine fixes fgets incorporation of line feeds and other
     * ASCII values less than 32 into an input string.  Written by H. Brown.
     */
    int fixgets(char input_array[], int length, FILE *file_name)
       {
       register int position=0;
    
       if (fgets(input_array,length,file_name) != NULL)
         {
         while(input_array[position] != 0)
    	  {              if (input_array[position] < 32) input_array[position]=0;
    	  else ++position;
    	  }
         }
       /* The length of the string is returned. */
       fflush(stdin);
       return position;
       }
    then you'd call it exaclty like fgets
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fflush(stdin);
    FAQ!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM