1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Are there different ways to store long strings(including spacing)?

Discussion in 'C' started by askmewhy25, Feb 6, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    I'm having problems when i use the fgets() and the gets() functions..

    Code:
    if(sel==1){
                                                     do{
                                                        system("cls");
                                                        printf("Input a string: ");
                                                        scanf("%s",&string[s]);
                                                        s++;
                                                        do{
                                                           system("cls");
                                                           printf("String successfully added\n");
                                                           printf("Enter another string?\n\n");
                                                           printf("1 - Yes\n");
                                                           printf("2 - No\n");
                                                           scanf("%d",&rep);
                                                           }while(rep!=1&&rep!=2);
                                                        }while(rep==1);
                                                     }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your code doesn't use fgets or gets. What is the problem? Does string not contain what you expect? Have you RTFM on scanf?
     
  3. Deadly Ghos7

    Deadly Ghos7 New Member

    Joined:
    Dec 19, 2009
    Messages:
    55
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Earth
    Home Page:
    Don't use gets() function, its vulnerable.
    Code:
    fgets(string,sizeof(string),stdin);
    
    And for scanf(),
    Code:
    scanf("%[^\n]",string);
    
    Hope that helps you.
     

Share This Page