Thread: Help me to Improve

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    13

    Help me to Improve

    Hello again!
    I asked for help before for opening a web page. I got around that and added things to it.
    A way to choose your activity, and a way to exit the program.
    Well i want feedback on what you think about it. I have an error that happens at the end (when i get the correct password, get the web page opened, and enter "4" to exit the program) that i don't know how to fix. But i also want feedback overall, so go ahead!

    here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main()
    {
        char* pass="hello";
        char* hint="Form of salutation";
        char* search="http://www.goolge.com";
        char* news="http://www.cnn.com";
        char* email="http://www.hotmail.com";
        int* activity;
        char* p;
        int i=0;
        int a=0;
    
    
        printf("Enter password and press ENTER.\n \n");
        do
        {
            printf("Password: ");
            scanf( "%s",  p);
    
            if(strcmp ( p, pass ) == 0)
            {
                printf("Correct Password. \n \n");
                i=10;
            }
            else
            {
                if(strcmp ( p, "hint" ) == 0)
                {
                    printf("Password Hint: %s. \n \n", hint);
                }
                else
                {
                    printf("Incorrect Password. \nType 'hint' for help. \n \n");
                    i++;
                }
            }
    
        }
        while (i<3);
    
        do
        {
    
            printf("Choose your activity:\n1. Search\n2. News\n3. eMail\n4. Exit\n\n");
            printf("Enter a number: ");
            scanf( "%s",  activity);
    
            if (strcmp ( activity, "1" ) == 0)
            {
                ShellExecute(NULL, "open", search, NULL, NULL, SW_SHOWNORMAL);
            }
    
            if (strcmp ( activity, "2" ) == 0)
            {
                ShellExecute(NULL, "open", news, NULL, NULL, SW_SHOWNORMAL);
            }
    
            if (strcmp ( activity, "3" ) == 0)
            {
                ShellExecute(NULL, "open", email, NULL, NULL, SW_SHOWNORMAL);
            }
    
            if (strcmp ( activity, "4" ) == 0)
            {
                i=20;
            }
    
            if (i==10)
            {
                printf("\nOpening application...\n");
                printf("Press any key to continue.\n");
                printf("\n--------------------------------\n\n");
                getch();
            }
    
        }
        while (i == 10);
    
        printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
        getch();
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    char* search="http://www.goolge.com";

    you mispelt the website :-)

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    thanks Tom

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Errors (click links):
    - Using pointers without allocating them
    - String literals should be const char*
    - Also, activity is an int*, not a char array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    Thanks again Elysia. I think I'd improved... Check it out.. I added a Username also..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main()
    {
        char user[80]="epijimenez";
        char pass[80]="hello";
        char hint[80]="Form of salutation";
        char search[80]="http://www.goolge.com";
        char news[80]="http://www.cnn.com";
        char email[80]="http://www.hotmail.com";
        int* activity;
        char u[80];
        char p[80];
        int i=0;
    
    do
    {
        printf("Username: ");
        scanf( "%s", u);
        if(strcmp ( u, user ) == 0)
            {
                i=5;
            }
            else
            {
                printf("Incorrect Username \n \n");
                i++;
            }
    }while (i < 3);
    
    
    if(i == 5)
    {
            do
            {
                printf("Password: ");
                scanf( "%s",  p);
    
                if(strcmp ( p, pass ) == 0)
                {
                    printf("Correct Password. \n \n");
                    i=10;
                }
                else
                {
                    if(strcmp ( p, "hint" ) == 0)
                    {
                        printf("Password Hint: %s. \n \n", hint);
                    }
                    else
                    {
                        printf("Incorrect Password. \nType 'hint' for help. \n \n");
                        i++;
                    }
                }
    
            }while (i<8);
    }
    
    
        if (i == 10)
        {
            do
            {
    
                printf("Choose your activity:\n1. Search\n2. News\n3. Email\n4. Exit\n\n");
                printf("Enter a number: ");
                scanf( "%d",  &activity);
    
                if (activity == 1)
                {
                    ShellExecute(NULL, "open", search, NULL, NULL, SW_SHOWNORMAL);
                }
    
                if (activity == 2)
                {
                    ShellExecute(NULL, "open", news, NULL, NULL, SW_SHOWNORMAL);
                }
    
                if (activity == 3)
                {
                    ShellExecute(NULL, "open", email, NULL, NULL, SW_SHOWNORMAL);
                }
    
                if (activity == 4)
                {
                    i=20;
                }
    
                if (i==10)
                {
                    printf("\nOpening application...\n");
                    printf("Press any key to continue.\n");
                    printf("\n--------------------------------\n\n");
                    getch();
                }
            }while (i == 10);
        }
    
        printf("\n---- PROGRAM TERMINATED ----\n--- PRESS ANY KEY TO END ---\n");
        getch();
    
        return 0;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From what I can see, the biggest problem is scanf: SourceForge.net: Scanf woes - cpwiki
    And activity is int*, and scanf does not expect a pointer to pointer to an int when reading an integer. It wants a pointer to the integer variable to write to. Comparing int* to int should probably yield a warning or two, as well, further indicating it's wrong.

    And you don't need temporary buffers for storing the data you are comparing to, the password and username. You can write it directly in the comparison you just as you did with hint.
    I would also go down an easier path to get the information.
    Looping x times, setting a boolean flag is success (did I get the correct username?) and breaking out of the loop at that point or accommodating the loop condition (loop x times OR until we got the correct username).
    Try implementing that one, as well. It should be good practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    13
    Thanks again Elysia. Im a beginner in this, so I'm learning every step of the way.
    I dont get the scanf problem your talking about. It works fine as of right now :S

    And for assigning varibles to each of the elements (password, username, news, search, email) is to be able to change it easily in the future. I just learned about structures, which I might implement it in my project by having a structure for each different username with their own choices of websites (and the password). What do you think about that?

    I also learned about the break; command, but not through my professor. I have a kinda of weird professor. Again thanks for your help.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by epi_jimenez View Post
    Thanks again Elysia. Im a beginner in this, so I'm learning every step of the way.
    I dont get the scanf problem your talking about. It works fine as of right now :S
    The biggest problem is the buffer overrun which the article clearly mentions, I believe. There is also a solution listed for that. Or more than one.

    And for assigning varibles to each of the elements (password, username, news, search, email) is to be able to change it easily in the future. I just learned about structures, which I might implement it in my project by having a structure for each different username with their own choices of websites (and the password). What do you think about that?
    Sure, sounds like an idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What do I need to improve on for the real world?
    By vhunterd in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-08-2007, 07:49 PM
  2. Help me improve my code!
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-19-2006, 10:04 AM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. can someone double check and maybe improve my code
    By tommy69 in forum C Programming
    Replies: 23
    Last Post: 04-21-2004, 02:04 PM
  5. how to improve my skills..
    By devil@work in forum C++ Programming
    Replies: 14
    Last Post: 04-08-2003, 02:13 PM