Thread: Console App Error

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    23

    Console App Error

    Okay, so I made a console application that - since I'm a beginner - took me a total of 8 hours to make so that it is easily expansible and easily read. I think that this lives up to the standards. But, please, I'm not concerned about the standards right now. My programs are sometimes like duct tape. They aren't pretty, but they work. So, don't go rambling on about the standards and making my code look pretty. I'm currently concerned about functionality. Now, with that rant out of the way...

    Here is the code. I'm using the Code::Blocks in conjunction with the GNU GCC compiler. No errors from the compiler. Just from Windows. One of those 'send error report sorry for the inconvenience' things.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LOOPNUM 42 //just a way to keep a loop going infinitely without a GOTO
    /***********************************************/
    /*  The point of this program is to create a   */
    /*  variety of different functions that can be */
    /* called through main.  It's really just going*/
    /* to be every C program I make made so that I */
    /*     can call any one of them at a whim.     */
    /***********************************************/
    
    
    int inputnum; //main()'s scanf input variable
    /**********************/
    /*        Main        */
    /**********************/
    int main()
    {
        printf("Okay.  This is a computer program that is specifically geared toward a \ncompendium of all the useless things I can do with the C Programming language.\n");
        printf("Remember.  The number corresponds to a certain function.  There is a quit \nfunction as well.  Just type in '0'.\n");
        printf("Also, remember.  This works as a command line would.  Once you finish, a \nnew line appears.  Then, you can put something else in.\n\n");
        printf("Hello, world!-----------------------------------------------------------1\n");
        printf("2-way temperature converter---------------------------------------------2\n\n");
        int x = LOOPNUM; //I had to assign something to x, so that my loop would just go over and over.  cool, huh?
        while (x = LOOPNUM)
        {
        printf("-->");  //this pointer looks nice, doesn't it?
        inputnum = 100; //in order to reset it to a value every time, so that I don't get an infinite loop
        scanf("%d", &inputnum);
        skipgarb();
        switch(inputnum)  //first time using switch, this is probably not very good form
            {
                case 1 : helloworld();
                        break;
    
    
                case 2 : tempconvert();
                        break;
    
    
                case 0 : exit(0);
    
    
                default: printf("Uhhhmmm...well, this is awkward.  Please, try again.\n");
                        break;
            }
        }
    return 0;
    }
    
    
    /************************************************************************************************************************************************************************************/
    skipgarb()  //skips user input garbage
    {
        while (getchar() != '\n')
            {
            ;
            }
    }
    
    
    /************************************************************************************************************************************************************************************/
    helloworld() //exactly as it says
    {
        printf("Hello, world!\n");
    }
    
    
    /************************************************************************************************************************************************************************************/
    tempconvert() //I'm having some problems with this one right now
    {
        int z;
        int x = LOOPNUM;
        printf("Okay.  Start by putting in a number.  Enter 1 for a fahrenheit to celsius \nconversion, and enter 2 for a celsius to fahrenheit conversion.  As always, put in 0 to exit.\n");
        while (x = LOOPNUM)
        {
            int m;
            printf("-->");
            int z = 100; //see line 26
            scanf("%d", z);
            skipgarb();
            switch(z)
            {
                case 1 : fahrtocels();
                        x = 1;
                        break;
    
    
                case 2 : celstofahr();
                        x = 1;
                        break;
    
    
                case 0 : system("cls");
                        main();
                        x = 1; //I know i won't need this.  Just a precaution in case of the (supposedly) impossible possibility of exiting from main but not the program via scanf input.
                        break;
    
    
                default: printf("Mmmmmm...try again.  Remember.  1, 2, or 0.");
                        break;
            }
        }
    }
    
    
    /************************************************************************************************************************************************************************************/
    fahrtocels() //convert fahrenheit to celsius
    {
        float fahrenheit;
        float answer;
        printf("Enter your fahrenheit value.\n");
        scanf("%f", fahrenheit);
        answer = 5.0 / 9.0 * (fahrenheit - 32); //don't you just love math?
        printf("%f\n", answer);
    }
    
    
    /************************************************************************************************************************************************************************************/
    celstofahr() //convert celsius to fahrenheit
    {
        float celsius;
        float answer;
        printf("Enter your celsius value.\n");
        scanf("%f", celsius);
        answer = (9.0/5.0)*(celsius+32);
        printf("%f\n", answer);
    }
    Okay. There are two problems I'm having. When I run the program and type in 2 to go to tempconvert(), I then type in 1, 2, or 0 and it gives me the previously described error. However, the default works fine. By commenting out most of fahrtocels() and adding a printf() to it, I was able to determine that the problem is not within the functions fahrtocels() or celstofahr(). This can also be determined by the fact that my 0 case also comes up with an error.

    Now. On to the other problem. By commenting out the contents of main() and replacing them with nothing but calls to fahrtocels() and celstofahr(), and, while the scanf() did come up, I was able to determine by putting a printf() after it that it didn't complete. Also, they return the same value every time. fahrtocels() returns one value and celstofahr() returns a different one.

    And, if I wasn't clear enough on what I just said, you can always just run it yourself. I've been trying for a long time to fix it up, but just can't. Oh, and everything that I didn't discuss works fine.

    Sorry for the long post! (-:

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I hope this doesn't fall into the category of "rambling on about the standards and making your code look pretty," but -

    Code:
    int x = LOOPNUM; //I had to assign something to x, so that my loop would just go over and over.  cool, huh?
        while (x = LOOPNUM)
    As long as you're forcing the loop to be infinite, why not just do this?

    Code:
    while(1)
    {
        // ...
    }
    And also, though it makes no difference in this ... configuration, the argument of the "while" loop should be a comparision; which means, it should have a comparison operator (==) and not an assignment operator (=).

    ---

    Now for the problem at hand. The following snippets of code are incorrect:

    Code:
    scanf("%d", z);
    scanf("%f", fahrenheit);
    scanf("%f", celsius);
    You need to pass an address as an argument of "scanf" using the "address-of" operator (&)

    There is more to comment on, but let's start there.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    23
    Thanks. Nope, no rambling. And your 'while(1)' was pretty neat. Thanks. Also, I must say that the pointers in C still confuse me a bit. So, yeah. I'll fix those things mentioned and run with it. This is the biggest, most complicated program I've ever written. And I plan to expand it. This is also the prettiest program I've ever made.

    Yeah. I'm a beginner.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Both your version and my version of the "while" loop were the same, in that the argument evaluated to "true." My version was just more practical/concise.

    You're doing fine. Happy coding!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    tempconvert() //I'm having some problems with this one right now
    {
    
        ...
    
        switch(z)
        {
                ... 
    
                case 0 : system("cls");
                        main();
                        x = 1;
                        break;
     
           ...
    
        }
    }
    You should not be recursively calling main. It would more appropriate to have a simple return statement here.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to shut down console app in an error trap
    By Dale in forum C# Programming
    Replies: 1
    Last Post: 11-19-2011, 10:08 PM
  2. console aplication error dev c++ winmain@16
    By caldeira in forum C Programming
    Replies: 2
    Last Post: 09-13-2008, 12:06 AM
  3. Error when running console app in vs2005
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 01-17-2007, 06:10 PM
  4. Another console game logic error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2006, 08:18 PM
  5. Weird console game logic error...?
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2006, 07:13 AM

Tags for this Thread