Thread: is this what we should start with?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    is this what we should start with?

    I got the idea from a source in the projects part of website so thought id try it on my own.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int age;
        char name[3];
        
        printf( "Enter name of animal: " );
        scanf( "%s", name );
        if ( strcmp( name, "dog" ) == 0) {
             printf( "ok, its a dog\n");
             printf( "enter age: " ); 
             scanf( "%d", &age );
             age *= 7;
             printf( "your dog is %d in doggy years heh\n", age ); }
             else if (strcmp(name, "cat" ) == 0) {
                  printf ( "ok its a cat\n" );
                  printf ( "enter age: " );
                  scanf( "%d", &age );
                  age *= 7;
                  printf( "your cat is %d in cat years\n", age); }
             
             getchar();
             getchar();
             return 0;
             }
    it works fine, is this the easiest way to right it or is there a different way?

    but anyway, is this ok for my first tinyyyyyyy program?

    reece.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Looks alright for a first try, the only thing is, you should try to avoid doing the same things in all cases. For instance getting the age and you multiply age by 7 for both cats and dogs. So you should just put that at the bottom as well as the string that outputs the name and years.

    Code:
    age *= 7;
    printf( "your %s is %d in %s years\n", name, age, name);
    You may have to knock the \n character that may or maynot be in your name string, otherwise you output could look like:

    your cat
    is 41 in cat
    years
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    ok thanks for the tip. yeh i tried putting the age *= 7 at the top and bottom, middle everything and it wouldnt work for them both lol, didnt think of doing the
    Code:
     printf( "your "%s" is "%d" is %s" years old\n" name, age, name );
    works fine when run though so im happy with it considering its my first try at something from a blank page

    thanks. reece.

    last thing, any ideas on a simple program like this that i shud try to do to get me going?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    printf( "your "%s" is "%d" is %s" years old\n" name, age, name );
    This is wrong, you don't close quotes for the identifiers in the string.

    Code:
    printf( "your %s is %d is %s years old\n" name, age, name );
    Not to mention that it says "Your cat is 10 is cat years old"
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    bah, fixed it.

    this works perfect.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int age;
        char name[3];
        
        printf( "Enter name of animal: " );
        scanf( "%s", name );
        if ( strcmp( name, "dog" ) == 0) {
             printf( "ok, its a dog\n");
             printf( "enter age: " ); 
             scanf( "%d", &age );}
             else if (strcmp(name, "cat" ) == 0) {
                  printf ( "ok its a cat\n" );
                  printf ( "enter age: " );
                  scanf( "%d", &age );}{
                  age *= 7;
                  printf("your %s is %d is %s years old\n", name, age, name );
             getchar();
             getchar();
             getchar();
             return 0;
    }
    }
    any ideas of other small programs to start with?

    any u first tried?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    char name[3];
    Name needs at least four elements to accomadate the NULL terminator placed at the end of strings.
    This program could also be compacted a little more:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int age;
        char name[4];
        
        printf( "Enter name of animal: " );
        scanf( "%s", name );
        if (!strcmp(name, "dog") || !strcmp(name, "cat")) 
       {
             printf( "ok, its a %s\n", name);
             printf( "enter age: " ); 
             scanf( "%d", &age );
             printf("your %s is %d years old which is %d in human years\n", name, age, age*7);
    	
       }
       else
          printf("I've never heard of a %s\n", name);
                  
        getchar();
        getchar();
        getchar();
           
    	return 0;
    
    }
    Next you could try to create a simple database program that asks the user to enter records and allows him get information for a record.

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    or you could make a blackjack program. i made one on my TI-83+, its not difficult when you dont have to worry about creating a GUI ;]
    Registered Linux User #380033. Be counted: http://counter.li.org

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I once made a graphical blackjack program in MS Excel with VBA.

    It was... not good, to say the least. It worked, though.
    Sent from my iPadŽ

  9. #9
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    *claps for skymaelstrom*
    feel better?

    Registered Linux User #380033. Be counted: http://counter.li.org

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    ok ill try a database program, any chance youv got a small piece of code from one that i can check out? :-D or the tutorials for them?

  11. #11
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    depends on the type of database.
    Registered Linux User #380033. Be counted: http://counter.li.org

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    one where you enter a name, then last name and then age and it stores it. or you can look some up.

    and even when uve closed it, if u re open the program u can search for what u entered.

    ?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    First you would need to define the structure of each record for example
    Code:
    typedef struct Record
    {
       char FirstName[20];
       char LastName[20];
       int Age;
    };
    It is best to split your program into seperate functions, you could create functions for loading (using fread() ), saving (using fwrite() )and searching (using strcmp/strcmpi to search by first or last name)

  14. #14
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i was thinking like mysql, mssql, etc.
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. C++ gui for windows where to start
    By prixone in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2006, 11:48 PM
  4. GNOME Desktop won't start (Mandriva)
    By psychopath in forum Tech Board
    Replies: 10
    Last Post: 07-19-2006, 01:21 PM
  5. Start bar color in WinXP
    By confuted in forum Tech Board
    Replies: 4
    Last Post: 05-03-2003, 06:18 AM