Thread: Novice Error Resolution

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Novice Error Resolution

    I'm aure it's a simple and foolish mistake that I'm making, here, likely as a result of some error that I never should have made, but I can't quite figure out how to get this to work properly....

    The program is a simple converter for decimal to hex that allows users to choose when to exit (cia a simple "Continue? Y/N"-style continuation). Now, It will certainly allow a user to perform multiple conversions by typing "Y" at the prompt; however, exit does not work through the conventional means expected. Any help with this admittedly basic issue would be greatly appreciated.

    Code:
    #include <stdio.h>
    
        int conv;
        char conf;
        
    void convert()
    {
         printf("\nNumber to be converted: ");
         scanf("%d", &conv);
         getchar();
         printf("\nResult: %x", conv);
         printf("\nConvert another number? Y/N ");
         scanf ("%c", &conf);
         getchar();
    }
        
    int main()
    { 
        printf("Decimal to Hexadecimal Conversion\n");
    
        do {
            convert();
            }
        while (conf == 'y' || 'Y');
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, you should avoid global variables whenever possible (they shouldnt be required in many cases, and they can almost always be removed by writing the code correctly). also, you should give variable names more obvious and unambiguous names, especially when you are using "conf" and "conv", which is easy to use a "f" or "v" when you meant the other. a logical error such as this would be difficult to spot with your 2 similar variable names. that said, i dont know which ones you actually meant to use in the convert function.

    - "convert" should have "conv" and "conf" as local variables. it should be changed to return an integer (the conf variable), and main should save the value of the "convert()" call. this would remove the global variables.

    - in your do/while loop in "main", you are doing 2 comparisons: "convf == 'y'" and the second comparison is "'Y'" (yes the second comparison is just 'Y'!). i believe you could do something like this in the old Visual Basic, which is maybe where you got this idea, but you cant in any other language (N.B.) the 2 comparisons should be "conf == 'y'", "conf == 'Y'" (separated with the or as you have now).

    - in "convert", i dont think there is any reason to use "getchar()", since it returns a value and you arent even doing anything with that value.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    On the global variables, I appreciate the advice, and will use implementations such as recommended in the future. More than anything, I was just testing another method in pursuit of an effective method, and started with something rather simple while I built on my admittedly limited knowledge of the language. Thank you again for your advice on a more proper implementation of that aspect.

    And as for the variable names - conv was short for "conversion" and conf for "confirmation" in my own mind, though I will also use more specific names, particularly when posting such a program to a forum that others will see(for I do not believe any can see into my mind to see the illogical logicality of it all)! Thank you for this, as well.

    On the comparison... That was the problem, as I discovered. Time to hit myself in the head for not paying more attention to such a thing. Once again, as you may soon grow tired of hearing, thank you. Revising that resolved all issue with the program's functionality (though I'll need to edit for variable efficiency, as you pointed out earlier).

    The getchar() at the end was an accident, and was entirely superfluous; however, the use of it in the middle of the "convert" function was necessary, as the program would simply end without it's inclusion upon execution (it was a guess at that time, which just happened to be the solution).

    I thank you a final time for the advice in my resolution of this error, as well as that for improvement of efficiency. That, in its very entirety, ends this post and the problem introduced initially in this thread. Thank you very much, and have a good day.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, you seem very appreciative and your welcome.

    naming your variables more obvious and unambiguously is a good practice to get into the habit of, especially if you are studying computer science or would eventually work on programming projects with others or a company in the future. dont be worried about naming a variable "too" obviously or lengthy, there is no negative impact on the program with this.

    the getchar() by itself i dont think is the (correct) solution, especially since you arent even saving its value, it just happened to work out here i think. there is a FAQ entry about reading numbers in, check it out here: Cprogramming.com FAQ > How do I get a number from the user (C)
    Last edited by nadroj; 05-02-2009 at 06:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  2. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 09:25 PM
  3. Rtfm
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-13-2003, 05:19 PM
  4. help for a C novice
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-02-2002, 09:49 PM
  5. Please help a novice
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2002, 10:53 PM