I could use a favour..

This is a discussion on I could use a favour.. within the C Programming forums, part of the General Programming Boards category; Hello, I recently started learning about C programming, and so far I have written what may very well be, a ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    I could use a favour..

    Hello,
    I recently started learning about C programming, and so far I have written what may very well be, a very basic conversion program. It converts celsius to fahrenheit and vice versa, also miles to kilometres and vice versa.

    I basically only know about the basics, conditional code, and looping, so I'm sure there is something I could learn in the future to minimalise my code..
    To the favour, however..

    I was wondering if someone with rather extened or decent knowledge of C to look over my code and tell me if its decent considering its just a bunch of if else, while statements.. Also, I was wondering if you could give me a way to remove the goto, and use something that'll produce the same effect, as I learned GOTO's are very messy and are not recommended..

    i'd have posted the code, but its 160 some lines, so if you're willing to help, you can message me on AIM SN: Into Iniquity
    or email me at obskyra@hotmail.com

    Thank you!

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Code:
     I recently started learning about C programming, and so far I have written what may very well be, a very basic conversion program. It converts celsius to fahrenheit and vice versa, also miles to kilometres and vice versa.
    How can this be 160 lines?

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    I just realised there is an attach file thing at the bottom here, hehe, go blind me! so I have attached it in this post
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    Originally posted by ygfperson
    [code]
    How can this be 160 lines?
    Lots of repetitions more than likely..

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Link soup:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351 14 occurances of that...

    http://cboard.cprogramming.com/showt...100#post267100 Don't use goto

    http://www.cprogramming.com/tutorial/lesson5.html Read up on switch case

    http://www.cprogramming.com/tutorial/lesson4.html Functions!

    That should keep you busy for a while. When you've read those and changed your program to be better by using them, post the new code. Or, if you get stuck, post a smart question.

    That code doesn't need to be anywhere near as long as it is.
    Away.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Also,
    Code:
    if (repeat == 'n')
        repeat = 'n';
    else if (repeat == 'y')
    {
        repeat = 'y';
    } /* end else if */
    else while (repeat != 'y' && repeat != 'n')
    {
        printf("\nPerform another conversion (y/n)? \n");
        fflush(stdin);
        repeat = getchar();
    } /* end else while statement */
    can be simplified to:
    Code:
    while (repeat != 'y' && repeat != 'n')
    {
        printf("\nPerform another conversion (y/n)? \n");
        repeat = getchar();
    } /* end while statement */
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    yeah, its far too long, I noticed.. longer than it could be, also because I took into account user input errors on all possibilities.. and I don't know at the moment if there is an easier way to go about it..
    The main purpose of this post, is just to see if what is written so far, is generally justifiable for the very basics of which I know.. for any of you who have compiled the program and run it, I was wondering if there was a basic way (long or not) to go about choosing their select AFTER they have made an input error and the printf "Please enter a selection between 1-5: " comes up without using a goto..

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The only thing with Zach L.'s example is that you have to initialize repeat to something that is neither n nor y. For example... o. Throw in a toupper in the while loop too, making the code this:

    Code:
    repeat='o';
    while (repeat != 'Y' && repeat != 'N')
    {
        printf("\nPerform another conversion (y/n)? \n");
        repeat = toupper(getchar());
    } /* end while statement */
    edit: You posted while I did. To answer your question, use a while loop
    Last edited by confuted; 07-20-2003 at 05:14 PM.
    Away.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    Originally posted by Zach L.
    Also,
    Code:
    if (repeat == 'n')
        repeat = 'n';
    else if (repeat == 'y')
    {
        repeat = 'y';
    } /* end else if */
    else while (repeat != 'y' && repeat != 'n')
    {
        printf("\nPerform another conversion (y/n)? \n");
        fflush(stdin);
        repeat = getchar();
    } /* end else while statement */
    can be simplified to:
    Code:
    while (repeat != 'y' && repeat != 'n')
    {
        printf("\nPerform another conversion (y/n)? \n");
        repeat = getchar();
    } /* end while statement */

    that helped a lot, I never even looked at it that way... I tend to make things a little more complicated than they need to be.. thanks for that bit

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    But blackrat, repeat exists and has already been initialized.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Iconoklast
    that helped a lot, I never even looked at it that way... I tend to make things a little more complicated than they need to be.. thanks for that bit
    No problem - it was technically correct, just a little longer than it had to be.

    And what blackrat was saying about toupper (or tolower for that matter) is a good point too. That'll allow users to enter y/Y or n/N without additional lines of code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    Originally posted by Zach L.
    No problem - it was technically correct, just a little longer than it had to be.

    And what blackrat was saying about toupper (or tolower for that matter) is a good point too. That'll allow users to enter y/Y or n/N without additional lines of code.
    Ah, excellent, was about to ask what it did, that'll save me a lot of time too

  13. #13
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I had already closed the window with his code in it, and I didn't look at it very closely. It's always safe to make sure it's initialized
    Away.

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    to blackrat, there is a while loop there, are you suggesting I use another?

  15. #15
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yeah, a while loop starting at your label and ending at your goto.

    If you check into putting the different conversions in functions, you could trim main() down to just a few lines - a while loop, within which there is the while loop which accepts input, and then a switch case that calls the conversion functions. Doing that will trim down your code a TON and make it a lot more readable (and it gives you good practice with functions, even though they aren't strictly needed here).
    Away.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug favour needed re: C++ Program
    By Nicole in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21