Thread: my first semi useful program

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    28

    my first semi useful program

    Ok here is the first semi useful program written in c. I think I will keep developing this further as I learn more, hopefully one day I will have a full gui interface for it, but thats just wishful thinking lol. Anyways though I thought I would let you guys critique it and let me know how I could change things in it to be more efficient. Maybe I could learn a thing or two. Don't be to harsh on me though.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    void fillstring(char *s,char *keys,int size);
    
    int error = 0;
    
    int main()
    { 
      //typing tutor program
      char c;
      char home[] = "asd\'f\"ghjkl;";
      char string[16];
      char *s;
      s = string;
      int x,range;
      error = 0;
      range = sizeof(home)-1;      
      
      fillstring(s,home,range);
      
      system("cls");
      printf("Typing Tutor 0.1\n");
      printf("Type the following line:\n");
      printf("\n%s\n",s);
      
      do
      {
          while((c = getch()) != *s)
          {
                if(c == 'r')
                     main();     
                error++;
          }
                                                                  
          printf("%c",c);
          s++;
      }          
      while(*s);    
      
      printf("\n");
      printf("\nYou typed %i characters wrong!\n",error);
      if(error > 5) printf("\n         \rYou suck!");
      printf("\nTry again? [y/n]");
      c = getche();
      if(c == 'y' || c == 'Y')
      {
          system("cls");
          main();   	
      } 
      else
        exit(0); 
      
    }
    
    void fillstring(char *s,char *keys,int size)
    {
        int i;
        srand((unsigned)time(NULL));
        
        for(i=0;i<16;i++)        //fill string with home row characters
        {
            *(s+i) = keys[rand()%size];
          
            while(*(s+i) == *(s+i-1))          //this loop keeps letters from repeating
                  *(s+i) = keys[rand()%size];
            
                                    
        }  
        *(s+16) = '\0';
        
     return 0; 
    }

  2. #2
    Banned
    Join Date
    Oct 2004
    Posts
    8
    interesting, keep working on it

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    please don't call main recursively

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Why is that not a good thing? What kinds of problems could it cause?

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    All the arguments presented in that thread can happen to any recursive function. Plus the standards don't mention recursive mains (I think). The only problem I see is the organization of your code. And if you call main, write
    Code:
    return main();
    otherwise when your main function ends and is backtracked you'll end up in the previous while loop.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ummm......to my understanding, everytime you call main, a new instance of main and all it's variables will be reproduced in memory and will not go away until exit(0)....


    maybe from there you can time words/minute and have different levels of difficulty

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    For sanity's sake do something like this

    Code:
    void MenuFunction ( void )
    {
    
        // Menu Code here
    
    }
    
    int main ( void )
    {
    
        while ( 1 ) {
    
            MenuFunction ();
     
            // Do stuff and check for input
            // If the user inputs the "exit code"
            // just break out of the loop
    
        }
    
        return 0;
    
    }
    What is C++?

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    ok, i rewrote my code, is this a better way of doing it?
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void fillstring(char *s,char *keys,int size);
    void typestring(char *s);
    
    int error = 0;
    int line = 0;
    char home[] = "asdfghjkl;\'\"";
    int range = sizeof(home)-1;
    int main()
    { 
      char string[16];
      char *s;
      s = string;
      
            
      
      fillstring(s,home,range);
      typestring(s);
      
       
      
    }
    
    void typestring(char *s)
    {
      char c;
      
      system("cls");
      printf("Typing Tutor 0.1\n");
      printf("Type the following line:\n");
      printf("\n%s\n",s);
      
      do
      {
          while((c = getch()) != *s)
          {
                if(c == 'r')
                {
                     fillstring(s,home,range);
                     typestring(s);
                 }         
                error++;
          }
                                                                  
          printf("%c",c);
          s++;
      }          
      while(*s);    
      
      line++;
      
      if(line < 4)
      {
          fillstring(s,home,range);
          typestring(s);
      }    
      
      printf("\n");
      printf("\nYou typed %i characters wrong!\n",error);
      if(error > 5) printf("\n         \rYou suck!");
      printf("\nTry again? [y/n]");
      c = getche();
      if(c == 'y' || c == 'Y')
      {
          system("cls");
          line = 0;
          fillstring(s,home,range);
          typestring(s);   	
      } 
      else
        exit(0);
    }
        
    void fillstring(char *s,char *keys,int size)
    {
        int i;
        srand((unsigned)time(NULL));
        
        for(i=0;i<16;i++)        //fill string with home row characters
        {
            *(s+i) = keys[rand()%size];
          
            while(*(s+i) == *(s+i-1))          //this loop keeps letters from repeating
                  *(s+i) = keys[rand()%size];
            
                                    
        }  
        *(s+16) = '\0';   
    }
    Last edited by Vertex34; 10-04-2004 at 09:03 AM.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes get rid of global variables

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    void fillstring(char *s,char *keys,int size);
    void typestring(char *s);
    prototypes dont need names
    Code:
    void fillstring(char *,char *,int);
    void typestring(char *);

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There's no need for recursion at all. You're only wasting memory. Every time they say they want to try again it grows a bit and it will keep growing until they quit. Just use a loop instead of recursion.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is accessing the array beyond its bounds.
    Code:
    *(s+16) = '\0';
    I'd add some headers.
    Code:
    #include <conio.h>
    #include <stdlib.h>
    Both getch and getche return an int.
    Code:
      char c;
       /* ... */
          while((c = getch()) != *s)
       /* ... */
      c = getche();
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    ok here is another go at it, there is one bug in it though, when you type n to quit it hang on my comp, gotta go to class though so I will work on it some more later. Thanks for all of the input you guys have given me.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define HOME "asdfghjkl;\'\""
    #define RANGE sizeof(HOME)-1
    
    void fillstring(char *s,int error);
    int typestring(char *s,int *size);
    
    
    
    int main()
    { 
      char string[16];
      char *s;
      s = string;
      int do_again = 1;
      
      int er = 0;
      int *error;
      error = &er;
            
      
      while(do_again)
        do_again = typestring(s,error);
        
       
      return 0;
    }
    
    int typestring(char *s,int *error)
    {
      char c;
      *error = 0;
      int line = 0;
      
      do
      {   
          fillstring(s,RANGE); 
          system("cls");
          printf("Typing Tutor 0.1\n");
          printf("Type the following lines:\n");
          printf("\n%s\n",s);
      
            do
            {
                    while((c = getch()) != *s)       
                         (*error)++;
                    printf("%c",c);
                    s++;
            }              
            while(*s);    
      
      line++;
      
      if(line == 2)
        break;
      }        
      while(1);
      
      printf("\n");
      printf("\nYou typed %i characters wrong!\n",*error);
      if(*error > 5) printf("\nYou suck!");
      printf("\nTry again? [y/n]");
      c = getche();
      if(c == 'y' || c == 'Y')
      {
          return 1;   	
      } 
      return 0;
    }
        
    void fillstring(char *s,int size)
    {
        int i;
        char home[] = HOME;
        
        srand((unsigned)time(NULL));
        
        for(i=0;i<16;i++)        //fill string with home row characters
        {
            *(s+i) = home[rand()%size];
          
            while(*(s+i) == *(s+i-1))          //this loop keeps letters from repeating
                  *(s+i) = home[rand()%size];
            
                                    
        }  
        *(s+16) = '\0';   
    }
    Last edited by Vertex34; 10-04-2004 at 10:04 AM.

  15. #15
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Ok I worked on it a little more and made a few more minor changes but I still cannot figure out why it hangs when I select not to try again. I did try putting a printf statement at the end of the main() right before return 0; and the program reaches that point and the printf displays that it is exiting then windows error pops up

    here is my latest code:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define HOME "asdfghjkl;\'\""
    #define RANGE sizeof(HOME)-1
    #define LINES 2                //number of lines to type
    void fillstring(char *s,int error);
    int typestring(char *s,int *size);
    
    
    
    int main()
    { 
      char string[16];
      char *s;
      s = string;
      int do_again = 1;
      
      int er = 0;
      int *error;
      error = &er;
            
      
      while(do_again = typestring(s,error));
       
      return 0;
    }
    
    int typestring(char *s,int *error)
    {
      char c;
      *error = 0;
      int line = 0;
      
      do
      {   
          fillstring(s,RANGE); 
          system("cls");
          printf("Typing Tutor 0.1\n");
          printf("Type the following lines:\n");
          printf("\n%s\n",s);
      
            do
            {
                    while((c = getch()) != *s)       
                         (*error)++;
                    printf("%c",c);
                    s++;
            }              
            while(*s);    
      
          line++;
      
          if(line == LINES)
              break;
      }        
      while(1);
      
      printf("\n");
      printf("\nYou typed %i characters wrong!\n",*error);
      if(*error > 5) printf("\nYou suck!");
      printf("\nTry again? [y/n]");
      
      c = getche();
      
      if(c == 'y' || c == 'Y')
          return 1;   	
      else
          return 0;
    }
        
    void fillstring(char *s,int size)
    {
        int i;
        char home[] = HOME;
        
        srand((unsigned)time(NULL));
        
        for(i=0;i<16;i++)        //fill string with home row characters
        {
            *(s+i) = home[rand()%size];
          
            while(*(s+i) == *(s+i-1))          //this loop keeps letters from repeating
                  *(s+i) = home[rand()%size];
            
                                    
        }  
        *(s+16) = '\0';   
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM