Need help with strings

askmewhy25's Avatar, Join Date: Jan 2010
Go4Expert Member
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 shabbir; 1Feb2010 at 09:44.. Reason: Code blocks
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
Please use code tags:

[code]

//code goes here

[/code]

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");
  }
askmewhy25's Avatar, Join Date: Jan 2010
Go4Expert Member
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..
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
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");
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
askmewhy25's Avatar, Join Date: Jan 2010
Go4Expert Member
If I use the scanf it could not store a very long string including the spacing..
murugaperumal's Avatar, Join Date: Feb 2010
Go4Expert Member
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);
       }

virxen's Avatar, Join Date: Nov 2009
Pro contributor
the main bug that nobody saw till now is this

Code:
............
       char name[16][128];
.....................
       printf("How many strings of names do you want to input? ");
       scanf("%d",&n);
............
for(i=0;i<n;i++){
    printf("Enter a name (name count %d): ", (i+1));
    fgets(name[i],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 by virxen; 26Feb2010 at 12:37..