help me with c++

Discussion in 'C++' started by telfer, Jun 5, 2009.

  1. telfer

    telfer New Member

    Joined:
    May 31, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    good day


    if I have name and ID number in a file like this :

    telfer 678
    jon 896
    necales 678
    .
    .
    .



    and I want to search for a nume>>>
    how I can do this ?


    thank you
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    I think this should work : :)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
          char Name[80], Search[80];
          int ID, flag = 0;
          FILE * pFile;
    
          scanf("%s", Name);
          pFile = fopen ("myfile.txt","r");    // Enter your file-name instead of myfile.txt
          while( !feof(pFile) )
          {
                fscanf (pFile, "%s %d", Search, ID);
                if( ! strcmp(Name, Search) )
                {
                      flag = 1;
                      break;
                }
          }
          fclose (pFile);
          if( flag )      printf ("The name %s was found in the file !!\n", Name);
          else            printf ("The name %s was NOT found in the file !!\n", Name);
          return 0;
    }
     
  3. telfer

    telfer New Member

    Joined:
    May 31, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    thik you

    but;
    how I can write the ID with the Name
    for exampe

    I search

    telfer

    the anuser will be
    found in

    telfer 678


    thank you again
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    That should be simple enough :

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
          char Name[80], Search[80];
          int ID, flag = 0;
          FILE * pFile;
    
          scanf("%s", Name);
          pFile = fopen ("myfile.txt","r");    // Enter your file-name instead of myfile.txt
          while( !feof(pFile) )
          {
                fscanf (pFile, "%s %d", Search, ID);
                if( ! strcmp(Name, Search) )
                {
                      flag = 1;
                      break;
                }
          }
          fclose (pFile);
          if( flag )      printf ("The name %s was found in\n%s %d\n", Name, Name, ID);
          else            printf ("The name %s was NOT found in the file !!\n", Name);
          return 0;
    }
     
  5. telfer

    telfer New Member

    Joined:
    May 31, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    thank you

    so,

    how I can conut the name

    I mean

    if I use switsh like this
    1- search
    2- count nume

    how I count the nume ( which is the code for this )
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    I have three things to say:
    (1) Don't be in a hurry ! Take your time and post without so many spelling error.
    (2) Could you explain what exactly do you mean by count ? May be give an example.

    (3) This website is not for providing free home-work help. Please tell us where you are stuck, and we would help. Don't ask us for complete source code.
     
  7. telfer

    telfer New Member

    Joined:
    May 31, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    I am sorry

    may be I have some error and I ask you more , but I want to know only

    count mean ( how many name in this file )

    if you want to answer this , thank you for ever
     
  8. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    You can ask as many times and as many questions as you want, but please do try yourself first and then ask us. It will be beneficial for you only. :)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
          char Name[80], Search[80];
          int Count = 0;
          int ID, IDSearch, flag = 0;
          FILE * pFile;
    
          scanf("%s", Name);
          pFile = fopen ("myfile.txt","r");    // Enter your file-name instead of myfile.txt
          while( !feof(pFile) )
          {
                fscanf (pFile, "%s %d", Search, IDSearch);
                ++ Count;
                if( ! strcmp(Name, Search) )
                {
                      flag = 1;
                      ID = IDSearch;
                }
          }
          fclose (pFile);
          printf("Total %d names found in the file.\n", Count);
          if( flag )      printf ("The name %s was found in\n%s %d\n", Name, Name, ID);
          else            printf ("The name %s was NOT found in the file !!\n", Name);
          return 0;
    }
    All these 3 were very easy. Are you beginning to learn C/C++ ?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice