Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To make a function in C which does not except "\n" & EOF as input.

To make a function in C which does not except "\n" & EOF as input.

Scheduled Pinned Locked Moved C / C++ / MFC
collaborationregexhelp
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D David Crow

    Tarun Jha wrote:

    if((c=temp.name[0])==EOF && c == '\n')

    I'm not sure how a character retrieved from gets() could ever be equal to -1, but that issue aside, I do not believe that the variable c can be equal to both EOF (or any other value) and \n at the same time. Another issue with gets() is buffer overflow.

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

    T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #6

    But in K&R it's && not ||

    1 Reply Last reply
    0
    • T Tarun Jha

      i searched on net but didn't got the concept of how it should be corrected. can you tell me how to correct it ?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #7

      Tarun Jha wrote:

      i searched on net but didn't got the concept...

      So what if the "net" ceased to exist one day? Some things you just have to learn by doing, not by searching.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      T 1 Reply Last reply
      0
      • D David Crow

        Tarun Jha wrote:

        i searched on net but didn't got the concept...

        So what if the "net" ceased to exist one day? Some things you just have to learn by doing, not by searching.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #8

        yes, i tried it many times but am unable to do it correctly. That's why i'am asking for correct answer.

        D 1 Reply Last reply
        0
        • T Tarun Jha

          yes, i tried it many times but am unable to do it correctly. That's why i'am asking for correct answer.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #9

          Tarun Jha wrote:

          That's why i'am asking for correct answer.

          Did you try replacing logical AND with OR? Did you try changing c to be a char instead of an int? Did you set a breakpoint on the if() test to see what the value of name[0] is? As the documentation states, it is neither EOF nor \n.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          T 1 Reply Last reply
          0
          • D David Crow

            Tarun Jha wrote:

            That's why i'am asking for correct answer.

            Did you try replacing logical AND with OR? Did you try changing c to be a char instead of an int? Did you set a breakpoint on the if() test to see what the value of name[0] is? As the documentation states, it is neither EOF nor \n.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #10

            yes i did, and it still does'nt works.

            #include #include typedef struct
            {
            char name[100];
            int goals;

            }match;

            match check();

            int main()
            {
            match team, temp;

            printf("Enter goals:\\n");
            scanf("%d", &team.goals);
            
            fflush(stdin);
            temp = check();
            printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals);
            
            return 0;
            

            }

            match check()
            {
            char c;
            match temp;

            printf("Name: \\n");
            gets(temp.name);
            
            if((c=temp.name\[0\])==EOF || c == '\\n')
            {
            	printf("Invalid entry!");
            	check(temp);
            }
            
            return(temp);
            

            }

            D 1 Reply Last reply
            0
            • T Tarun Jha

              yes i did, and it still does'nt works.

              #include #include typedef struct
              {
              char name[100];
              int goals;

              }match;

              match check();

              int main()
              {
              match team, temp;

              printf("Enter goals:\\n");
              scanf("%d", &team.goals);
              
              fflush(stdin);
              temp = check();
              printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals);
              
              return 0;
              

              }

              match check()
              {
              char c;
              match temp;

              printf("Name: \\n");
              gets(temp.name);
              
              if((c=temp.name\[0\])==EOF || c == '\\n')
              {
              	printf("Invalid entry!");
              	check(temp);
              }
              
              return(temp);
              

              }

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #11

              Tarun Jha wrote:

              check(temp);

              How does this compile?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              T 1 Reply Last reply
              0
              • D David Crow

                Tarun Jha wrote:

                check(temp);

                How does this compile?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                T Offline
                T Offline
                Tarun Jha
                wrote on last edited by
                #12

                i corrected that it still doesn't work.

                #include #include typedef struct
                {
                char name[100];
                int goals;

                }match;

                match check();

                int main()
                {
                match team, temp;

                printf("Enter goals:\\n");
                scanf("%d", &team.goals);
                
                fflush(stdin);
                temp = check();
                printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals);
                
                return 0;
                

                }

                match check()
                {
                char c;
                match temp;

                printf("Name: \\n");
                gets(temp.name);
                
                if((c=temp.name\[0\])==EOF || c == '\\n')
                {
                	printf("Invalid entry!");
                	check();
                }
                
                return(temp);
                

                }

                D L L 3 Replies Last reply
                0
                • T Tarun Jha

                  i corrected that it still doesn't work.

                  #include #include typedef struct
                  {
                  char name[100];
                  int goals;

                  }match;

                  match check();

                  int main()
                  {
                  match team, temp;

                  printf("Enter goals:\\n");
                  scanf("%d", &team.goals);
                  
                  fflush(stdin);
                  temp = check();
                  printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals);
                  
                  return 0;
                  

                  }

                  match check()
                  {
                  char c;
                  match temp;

                  printf("Name: \\n");
                  gets(temp.name);
                  
                  if((c=temp.name\[0\])==EOF || c == '\\n')
                  {
                  	printf("Invalid entry!");
                  	check();
                  }
                  
                  return(temp);
                  

                  }

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #13

                  Tarun Jha wrote:

                  i corrected that it still doesn't work.

                  My question was not meant to fix your logic error. It was a compiler error. As it stands now, if you were somehow able to detect an invalid entry, a recursive call to check() is made but it would not return anything to the original caller. You'll also need to address this issue.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  1 Reply Last reply
                  0
                  • T Tarun Jha

                    i corrected that it still doesn't work.

                    #include #include typedef struct
                    {
                    char name[100];
                    int goals;

                    }match;

                    match check();

                    int main()
                    {
                    match team, temp;

                    printf("Enter goals:\\n");
                    scanf("%d", &team.goals);
                    
                    fflush(stdin);
                    temp = check();
                    printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals);
                    
                    return 0;
                    

                    }

                    match check()
                    {
                    char c;
                    match temp;

                    printf("Name: \\n");
                    gets(temp.name);
                    
                    if((c=temp.name\[0\])==EOF || c == '\\n')
                    {
                    	printf("Invalid entry!");
                    	check();
                    }
                    
                    return(temp);
                    

                    }

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #14

                    Richard and I made sure you realized you that you cant use 'gets' ... it's a drop dead. 'gets' only returns chars .. EOF is an int (-1 usually) End-of-file - Wikipedia[^] please look at the difference between 'getc' (returns an int .. yipeee EOF safe) and 'gets' (returns char boo) Then it's not real hard

                    int c;
                    int namelen = 0; // initialize array position index to zero
                    do {
                    c = getc(stdin); // fetch the integer keyboard read value .. EOF safe
                    temp.name[namelen++] = (char) c; // convert the integer to a char and place in array .. increment position
                    } while (c !=EOF && c != '\n'); // repeat until EOF or new line
                    temp.name[namelen-1] = '\0'; // Make C null terminated string (overwrite last EOF or '\n')

                    That should do what you want and I commented what each line does.

                    In vino veritas

                    1 Reply Last reply
                    0
                    • T Tarun Jha

                      i corrected that it still doesn't work.

                      #include #include typedef struct
                      {
                      char name[100];
                      int goals;

                      }match;

                      match check();

                      int main()
                      {
                      match team, temp;

                      printf("Enter goals:\\n");
                      scanf("%d", &team.goals);
                      
                      fflush(stdin);
                      temp = check();
                      printf("Name: %s\\t\\tGoals: %d\\n", temp.name, team.goals);
                      
                      return 0;
                      

                      }

                      match check()
                      {
                      char c;
                      match temp;

                      printf("Name: \\n");
                      gets(temp.name);
                      
                      if((c=temp.name\[0\])==EOF || c == '\\n')
                      {
                      	printf("Invalid entry!");
                      	check();
                      }
                      
                      return(temp);
                      

                      }

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #15

                      I showed you where to find the answer two days ago: read the documentation.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups