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

Need help with strings

Discussion in 'C' started by askmewhy25, Jan 31, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    I am having a problem with the printf("Enter a name (name count %d): ", i); because it always start to get the value for the Enter a name (name count 2): and it does not start with Enter a name (name count 1):..then can someone give me the codes to alter the things written on the text file or clear some of its data..tnx
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    main(){
           char name[16][128];
           int i, n, m;
           
           printf("How many strings of names do you want to input? ");
           scanf("%d",&n);
           
           for(i=1;i<=n;i++){
                       printf("Enter a name (name count %d): ", i);
                       fgets(name[n],128,stdin);
                       }
                       
           FILE *fp;
           fp=fopen("name.txt","a");
           for(i=0;i<n;i++){
           fprintf(fp,"%s\n",name[i]);
           }
           fclose(fp);
           
           getche();
           }
     
    Last edited by a moderator: Feb 1, 2010
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    Please use code tags:

    [noparse]
    Code:
    
      //code goes here
    
    
    [/noparse]

    You're looping starting at 1 instead of 0. Also, fgets will retrieve the string and the carriage return/line feed. You'll need to strip this off with something like strtok().
    Try this in your loop instead:

    Code:
    
      for(i=0;i<=n;i++){
        printf("Enter a name (name count %d): ", i+1);
        fgets(name[n],128,stdin);
        strtok(name[n],"\r\n");
      }
    
    
     
    shabbir likes this.
  3. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Nothing changed, the problem is still there what just happened is that there is an additional name count and when it comes to the name.txt file it could not print the name properly..
     
  4. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    So the problem is in the file save routine? Try opening your file in write mode instead of append mode (and use the "t" text mode if using microsoft OS):

    Code:
    
      fp=fopen("name.txt","w");
      //or:
      fp=fopen("name.txt","wt");
      
    
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    scanf is formatted input, so it stops reading when it's got what it wanted and leaves the rest on the input buffer. So when you enter a number and press return, scanf reads the number then stops, leaving the enter. So when the for loop starts, there is already an enter on the keyboard buffer, which answers 1, then on the second iteration the buffer is clear so it stops to wait for an answer to 2.

    This is why we don't use scanf for formatted input from users. I just don't understand why teachers insist on teaching that you should, other than sheer laziness on their part. Use gets to read the whole line from the user, then parse out the stuff you want.
     
  6. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    If I use the scanf it could not store a very long string including the spacing..
     
  7. murugaperumal

    murugaperumal New Member

    Joined:
    Feb 20, 2010
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    Dear Friend,

    Your code in correct. But you did some little mistake only.

    The following code contains mistake
    printf("How many strings of names do you want to input? ")

    If you press enter after given the no of name you want, that enter key taken as a first input. So next it will display

    "Enter a name (name count 2)". That is the mistake.

    To avoid this you should use the getchar(). So that time this function will get the "\n".

    Then it will display
    "Enter a name (name count 2)"

    Then you did some other mistake also. That is, while get the inputu from the stdin,you stored the names in the array index start with 1.

    But while store it names in the file, that time you use the index start with 0. So you may not be able to get the last name. It will display the some character as a first name.

    Code:
       
    I have written the correct code. 
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    main(){
           char name[16][128];
           int i, n, m;
           printf("How many strings of names do you want to input? ");
           scanf("%d",&n);
           getchar();
    
           for(i=1;i<=n;i++){
                       printf("Enter a name (name count %d): ", i);
                       fgets(name[i],128,stdin);
                       }
    
           FILE *fp;
           fp=fopen("name.txt","w");
           for(i=1;i<=n;i++){
                   printf("%s\n",name[i]);
           fprintf(fp,"%s\n",name[i]);
           fflush(fp);
           }
           fclose(fp);
           }
    

     
  8. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    the main bug that nobody saw till now is this

    Code:
    ............
           [COLOR=Red]char name[16][128];[/COLOR]
    .....................
           printf("How many strings of names do you want to input? ");
           scanf("%d",[COLOR=Red]&n[/COLOR]);
    ............
    for([COLOR=Red]i=0;i<n;i++[/COLOR]){
        printf("Enter a name (name count %d): ", (i+1));
        fgets([COLOR=Red]name[i],[/COLOR]128,stdin);
       // strtok(name[i],"\r\n");
           for (int j = 0; j <128; j++ ){
            if ( name[i][j] == '\n' ){
                name[i][j] = '\0';
                break;
            }
         }
      }
    ..........
    
    what will happen for example if i enter n=100 ?
    you must check entered n value to be less than 16 or equal.
     
    Last edited: Feb 26, 2010

Share This Page