hlp and pls give some advise

This is a discussion on hlp and pls give some advise within the C Programming forums, part of the General Programming Boards category; now i writing a program to count person"lila"enter the gate ,other just simply print welcome.that the program is just count ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    hlp and pls give some advise

    now i writing a program to count person"lila"enter the gate ,other just simply print welcome.that the program is just count how many of time lila enter the gate that display it. this my code but i dun know how to break the loop,it able to display but the loop got problem .pls help! and can u suggest other method to write that this one .tq

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        int i,count;
        char name[500];
        printf("please enter you name:\n");
        scanf("%s",name);
    
        if(strcmp(name,"lila")==0)
        {
             for(i = 1; i <= count; i = i + 1){
             printf("number of enter=%d", i);
        
            }
            return;
            
        }
        else
        {
            printf("welcome\n");
            return 0; 
        }
        
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well you need to do things like
    - initialise count to zero.
    - add 1 to count at some point in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    first off, your return on line 18, should not be there!!!
    and your return after welcome ends your program!
    second, your for loop, just loops "number of enter=X" X number of times, then exits....

    if you remove both returns, and remove the for line completely, and replace it with
    Code:
     i++
    that would fix the probs you are having....(also no need for count!!!)

    so change line 7 to
    Code:
    int i = 0;
    THEN

    add a for loop around the entire thing,

    Code:
    for(;; )
    {
    between lines 8 and 9

    then
    Code:
    }
    at line 26

    your golden!
    Last edited by Crossfire; 12-27-2012 at 01:22 AM.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    16
    There was no value of count and much errors!
    I think you should use this
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
     
    int main()
    {
        int count=0;
        char name[500];
        do{
        printf("\nplease enter you name:\n");
        scanf("%s",name);
        if(strcmp(name,"lila")==0)
        {
        count++;
             printf("number of enter=%d", count);
             
        }
        else
        {
            printf("welcome\n");
            getch();
            return 0; 
        }}while(1);
        getch(); 
    }
    This program will reset the count after its termination if you want to keep the data after its termination then you need to add file I/O

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stddef.h>
    #include<string.h> 
    void main()
     {    
      int i=0;
      
      char name[500];    
      for(;;)
      {
       printf("please enter you name: ");    
       scanf("%s",name);     
       
       if(strcmp(name,"lila")==0)    
        {         
         i++;
         printf("number of enters = %d\n", i);             
        }        
       else    
        {        
         printf("welcome\n");        
            
        }     
      }
     }
    Your programming is close, just need to WALK THROUGH it when you find a problem with the flow....

    helps alot if you dont try to rush it, or overly tired like i am now!!! hence my 3 edits on what i typed!

    (sorry Hritik, didnt see you posting, when i was posting!!!)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,962
    Hmm...
    Quote Originally Posted by ckking
    now i writing a program to count person"lila"enter the gate
    You lost me there

    What exactly are you trying to count?

    Note that this is bad:
    Code:
    scanf("%s",name);
    since it is possible to enter more than 500 characters to read into name, but name can only contain a string of maximum length 499 (1 for the null character). It would be better to write:
    Code:
    scanf("%499s", name);
    and of course you should check the return value of scanf, but that can come later.

    Quote Originally Posted by Hritik
    I think you should use this
    I don't think so, as you need to:
    • Indent your code properly.
    • Get rid of the non-standard <conio.h> and getch() since they are non-standard and were not used in ckking's posted code.


    @Crossfire: void main should be int main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Quote Originally Posted by laserlight View Post
    @Crossfire: void main should be int main.
    HA lol, i did that because i took the return out....habit of mine even though pelles yells at me alot!!!

    thanks!

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Quote Originally Posted by Crossfire View Post
    HA lol, i did that because i took the return out....habit of mine even though pelles yells at me alot!!!

    thanks!
    crossfire-yes this the output that i wnt but for(; ? this one i never see before .........quit new for me

    hritik-yap i follow u code as reference and rewrite again...tk!
    laserlight-good advise!

    now tis my new coding,loop proble resolve already but cannot back to main body ....can u guy help to to check which part missing something ?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
        int i,count={0};
        char name[500];
        printf("please enter you name:\n");
        scanf("%s",name);
    
        if(strcmp(name,"lila")==0)
        {
             for(i = 1; i <=count+1 ; i++)
                {
                  printf("number of enter=%d\n", i);
            }
            
        }
        else
        {
            printf("welcome\n");
             }
       return ;
        
    }

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    the
    Code:
    for(;; )
    causes an infinate loop
    same as
    Code:
    while(true)
    Code:
    while(1)
    oh but compair my code to yours, that return you have, and your for statement is why your program isnt looping right

    or do you only want it to loop while lila is entering, and quitting when someone other then lila enters?

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Quote Originally Posted by Crossfire View Post
    the
    Code:
    for(;; )
    causes an infinate loop
    same as
    Code:
    while(true)
    Code:
    while(1)
    oh but compair my code to yours, that return you have, and your for statement is why your program isnt looping right

    or do you only want it to loop while lila is entering, and quitting when someone other then lila enters?
    no ,you output which i want for ......
    o so that meant
    for(;; ) same function as while(true)
    that if i put while(1) at the end of coding will be cause infinate loop?

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    if you put
    Code:
    do
    {
    at the beginning, and then
    Code:
    }while(1);
    then yes it will do the same as
    Code:
    for(;; )
    {
    at the beginning and
    Code:
    }
    at the end


    but if the code i fixed of yours is what you want, then why change it? lol

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Quote Originally Posted by Crossfire View Post
    if you put
    Code:
    do
    {
    at the beginning, and then
    Code:
    }while(1);
    then yes it will do the same as
    Code:
    for(;; )
    {
    at the beginning and
    Code:
    }
    at the end


    but if the code i fixed of yours is what you want, then why change it? lol
    ha ha just like u say never try never know ~~i dun like be a copy cat ......

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    resolve

    question resolve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advise
    By krakatao in forum C Programming
    Replies: 36
    Last Post: 03-11-2012, 10:37 PM
  2. Advise for the day.
    By Mario F. in forum General Discussions
    Replies: 8
    Last Post: 04-01-2010, 03:51 PM
  3. please advise....
    By manny in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 06:09 AM
  4. Need some advise
    By ILoveVectors in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2005, 09:24 AM
  5. Some Advise..
    By VaSt in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2003, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21