Thread: string problem !!!!!!

  1. #16
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    lol thise amazing so much pro in here.

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Adak View Post
    I refreshed it once, and did not see several posts in this thread. After refreshing twice more, I see them all, and that it's solved.
    Adak I was not referring to you, not even want to "shout" at you

    @Sharlin glad you found your way

    // I wish a good day to everybody

    //@Sharlin, mind that it is better to keep what you want to say in one post, and not post 2-3 times in a row
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But remember that scanf() is just as unsafe as gets(), unless you use the width modifier. And that scanf() will not easily accept strings with spaces, for this you need something like fgets().
    Code:
       char _str[100];
       int i=0,count_str=0;
       printf("enter string up to 10 letters!");
       scanf("%100s",_str);
    Jim

  4. #19
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    But remember that scanf() is just as unsafe as gets(), unless you use the width modifier. And that scanf() will not easily accept strings with spaces, for this you need something like fgets().
    Code:
       char _str[100];
       int i=0,count_str=0;
       printf("enter string up to 10 letters!");
       scanf("%100s",_str);
    Jim
    Very good point. Thank you Jim.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #20
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    white gets() its working and white scanf it doesn't.
    Code:
      #include "stdafx.h"#include #include #include #include int main(){float n;char number[10];printf ("enter a  number!\n");//gets (number );scanf("%s",&number[10]);n=atof(number);printf("n=%f\n",n);getch();return 0;}

  6. #21
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
    float n;
    char number[10];
    printf ("enter a  number!\n");
    //gets (number );
    scanf("%s",&number[10]);
    n=atof(number);
    printf("n=%f\n",n);
    
    
    
    
    
    
    getch();
    
    
    return 0;
    
    
    
    
    }

  7. #22
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For a more complete approach to line-based to address the above concerns (safe input, and embedded spaces), consider the following extended strategy and adjust for your use case:

    1. read an entire input line
    2. if the line is too long, abort with an error
    3. parse the line
    4. if the line does not contain the expected data, abort with an error
    5. print the result

    Here is an example (breaking into functions would help in a real program):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 10000
    static char inputline[MAX] = "";
    
    int main(void)
    {
        // (optional) show prompt
        printf("%s", "Enter your name using the format: LAST, FIRST.\n"
                "LAST and FIRST are allowed to have embedded spaces in them.\n"
                ": ");
        
        // get inputline and handle errors
        fgets(inputline, MAX, stdin);
        size_t len = strlen(inputline);
        if (len == 0) {
            printf("No line received.\n");
            return EXIT_FAILURE;
        }else if(len == MAX-1){
            printf("Line is too long.\n");
            return EXIT_FAILURE;
        }
        
        // parse the inputline into LAST, FIRST
        char last[100]="", first[100]="", leftover[10000]="";
        if (sscanf(inputline, " %99[^,], %99[^.].%9999c", last, first, leftover) != 3) {
            printf("Name was in the wrong format.\n");
            return EXIT_FAILURE;
        }
        len = strlen(leftover);
        if (len > 1) {
            leftover[len-1] = '\0'; // chomp the \n from leftover
            printf("Warning: trailing characters found after inputline: \"%s\"\n", leftover);
        }
        
        // show result
        printf("\nLast name: \"%s\"\n", last);
        printf("First name: \"%s\"\n", first);
        return EXIT_SUCCESS;
    }
    Explanations:
    scanf returns the number of items it found, so in this case 3 should be expected for correct input. If the input was correct, leftover should contain "\n" (only the newline).

    %99[^,], means to get at most 99 non-commas followed by a comma. Change the size and delimiter to what you need. Example: if you use ; and the buffer for that component is 1000 characters, use %999[^;];

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "scanf()" requires an address of a variable as the second argument. If you're reading a string, you can simply put:

    Code:
    scanf("%s",number);
    Here, the name of the array without any indices ("number") acts as a pointer to the first element of that array, fulfilling the requirements of the second argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM