Thread: Newbie need some help

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Newbie need some help

    I have some problem with my program, it won't run into the

    Code:
    if (selection2=='y'){
                system("clr");
                return main();
             }
    please help me with this

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(){
        float km,ml,result;
        int selection;
        char selection2;
        
        printf("\n\nPlease select the mesurement unit you want to convert with\n");
        printf("1) Kilometer\n2) Mile\n");
        scanf("%d",&selection);
        if (selection==1){
            printf("\nPlease enter the kilometers \n");
            scanf("%f",&km);
            result=km*0.621371;
            printf("\n %.2f km = %.2f mile",km,result);
        }
        else if (selection==2){
            printf("\nPlease enter the miles \n");
            scanf("%f",&ml);
            result=ml/0.621371;
            printf("\n %.2f mile = %.2f km",ml,result);
        }
        
        else if(selection!=1 && selection!=2){
            printf("Error, Do you want to restart the program (y/n)");
            scanf("%c",&selection2);
        if (selection2=='y'){
                system("clr");
                return main();
             }
             else
             return 0;
         }
         getchar();
         return 0;
         }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Code:
        if (selection2=='y'){
                system("clr");
                return main();
             }
    For starters, NEVER call main() within your code!!!

    main() should only be called once by the startup code. You will want to create another function, and call that multiple times if needed.

    You should also check the return value from scanf() to insure you have received legitimate data. If not, then you need to clear the input buffer.
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    Don't use "Magic Numbers" in your code. "0.621371", for example. Use a #define with a descriptive name where you need this value.

    I'm sure there are more issues, but it is late, and I have not compiled the program. Start with these and repost the code.

  3. #3
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by spdragon View Post
    I have some problem with my program, it won't run into the

    Code:
    if (selection2=='y'){
                system("clr");
                return main();
             }
    please help me with this
    When you read characters you need to clear the input buffer from left over characters of previous read operations to make sure that what you read is the users input change your code to
    Code:
    else if(selection!=1 && selection!=2){        printf("Error, Do you want to restart the program (y/n)");
            fflush(stdin);  //clear the input buffer from the left over line end characters.
            scanf("%c",&selection2);
    
        if (selection2=='y'){
    
                system("clr");
    
                return main();
    
             }
    
             else
    
             return 0;
    
         }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fflush(stdin); //clear the input buffer from the left over line end characters.
    Right idea, wrong solution.
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Salem View Post
    > fflush(stdin); //clear the input buffer from the left over line end characters.
    Right idea, wrong solution.
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    thank you for the input. I read everything there is to read on the provided link and there is a logic behind the post, one that I'm inclined to respect at this point, but after spending some time evaluating the different solution presented there I have to admit that I dislike them all. So far only two solutions are acceptable in my book. 1) the fflash, as far as I have tested it on win 7 64, win xp 32bit and win 8.1 32bit it behaves the same way so I'm inclined to use it on windows and 2 use the conio.h and the kbht function to determine if there are more chars in the buffer to read.
    In any case thank you for the input it was educating.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > as far as I have tested it on win 7 64, win xp 32bit and win 8.1 32bit it behaves the same way
    Try fflush(stdin) on Linux, you will fail.

    > but after spending some time evaluating the different solution presented there I have to admit that I dislike them all.
    Personal preference doesn't come into it, if the code is broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Salem View Post
    > as far as I have tested it on win 7 64, win xp 32bit and win 8.1 32bit it behaves the same way
    Try fflush(stdin) on Linux, you will fail.
    linux is broken in many ways that is why I'm on windows.
    Quote Originally Posted by Salem View Post
    > but after spending some time evaluating the different solution presented there I have to admit that I dislike them all.
    Personal preference doesn't come into it, if the code is broken.
    Preference is the only thing to use when learning, half measures are not acceptable for a reason. Console applications are only used in the learning process and from some anal admins. Opinions don't help me learn so I'm going to avoid addressing them in the future.
    Last edited by taazz; 06-02-2016 at 04:30 AM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by taazz
    So far only two solutions are acceptable in my book. 1) the fflash, as far as I have tested it on win 7 64, win xp 32bit and win 8.1 32bit it behaves the same way so I'm inclined to use it on windows and 2 use the conio.h and the kbht function to determine if there are more chars in the buffer to read.
    Most of the time you can completely avoid needing fflush(stdin); at all. A combination of fgets + sscanf is often easy and correct, in that it doesn't necessarily leave behind any stray characters.
    Quote Originally Posted by taazz
    Opinions don't help me learn so I'm going to avoid addressing them in the future.
    Every post is fair game. If you say something stupid, you will get called out on it here.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Nothing to do with Linux (which is a hell of a lot less broken than windows malware)

    This is what the standard says, the rest is irrelevant.
    7.19.5.2 The fflush function
    Synopsis
    1
    #include <stdio.h>
    int fflush(FILE *stream);
    Description
    2 If stream points to an output stream or an update stream in which the most recent
    operation was not input, the fflush function causes any unwritten data for that stream
    to be delivered to the host environment to be written to the file; otherwise, the behavior is
    undefined.

    3 If stream is a null pointer, the fflush function performs this flushing action on all
    streams for which the behavior is defined above.
    Returns
    4
    The fflush function sets the error indicator for the stream and returns EOF if a write
    error occurs, otherwise it returns zero.
    stdin is NOT an output stream nor an update stream.

    You're in for some more hard lessons with your current approach - fine by me, I'll just say "I told you so in advance"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by whiteflags View Post
    Most of the time you can completely avoid needing fflush(stdin); at all. A combination of fgets + sscanf is often easy and correct, in that it doesn't necessarily leave behind any stray characters.
    I guess you are write, but it is not something I want to spend my time on especially when the application I'm writing are 90% GUI and 10% services/daemons.
    Quote Originally Posted by whiteflags View Post
    Every post is fair game. If you say something stupid, you will get called out on it here.
    As far as there is a hint that I can follow to learn I have no problem, in fact I'm counting on it, I might not follow the advice but I'll have a good idea where to start debugging. If on the other hand its a one line opinion like
    Quote Originally Posted by Salem View Post
    Personal preference doesn't come into it, if the code is broken.
    then I reserve the right to avoid getting involved in a meaningless debate and move on with my learning process.

    Quote Originally Posted by Salem View Post
    This is what the standard says, the rest is irrelevant.
    I know I read it in the link you provided and thank you for that
    Quote Originally Posted by Salem View Post
    You're in for some more hard lessons with your current approach - fine by me, I'll just say "I told you so in advance"
    I'm happy that I can amuse you in a small way, in fact I can make it a think to post something every time I get in trouble ignoring your advice, if that will make your participation in this forums more enjoyable, just don't expect me to respond to those kind of comments in the future.
    Last edited by taazz; 06-02-2016 at 05:23 AM.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Does it really matter what only you are doing? The thread is about console applications. If you want to talk about GUI programming I guess you should visit Windows Programming, where that is more topical.

  12. #12
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by whiteflags View Post
    Does it really matter what only you are doing? The thread is about console applications. If you want to talk about GUI programming I guess you should visit Windows Programming, where that is more topical.
    It does to me. If you think you can do better then show us the code and stop "talking" about the code. Sorry but this thread is not interesting any more, I'm out. have fun.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by taazz
    It does to me.
    Which is fine, except that you should remember that you answered spdragon's post in the general-purpose C programming forum. You did not venture your own topic with constraints specific to yourself, or perhaps replied in the Windows programming forum where certain assumptions can be more readily made, even if they remain undefined behaviour.

    Quote Originally Posted by taazz
    If you think you can do better then show us the code and stop "talking" about the code.
    This has already been answered by Salem's post #4.

    Quote Originally Posted by taazz
    Sorry but this thread is not interesting any more, I'm out. have fun.
    The concern about fflush(stdin) ceased to be interesting after post #4; you should have been out after post #4 if you only wanted to argue about fflush(stdin). Whether this thread remains interesting in general is up to spdragon to decide.
    Last edited by laserlight; 06-02-2016 at 07:45 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by taazz View Post
    If you think you can do better then show us the code and stop "talking" about the code.
    You're disregarding the advice of highly experienced and talented C programmers, because they hurt your feelings. Quit being so arrogant and try to learn something. The first couple of posts showed what is objectively wrong with your code. You chose to disregard the advice you were given, and act like your preference is more important than what is actually the correct course of action. Good luck getting through life with that attitude.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  15. #15
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by taazz View Post
    Sorry but this thread is not interesting any more, I'm out. have fun.
    Your reply is the common refrain of the loser.

    There's nothing special about you, little boy. We've seen it again and again. You're not the first and you won't be the last. You're demonstrating a common human weakness summed up in the phrase "a little knowledge is a dangerous thing", or the word "sophomore", meaning wise idiot. You just have it worse than most due to your general lack of intelligence.

    A little learning is a dangerous thing;
    drink deep, or taste not the Pierian spring:
    there shallow draughts intoxicate the brain,
    and drinking largely sobers us again.
    -- Alexander Pope

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello, I am a newbie and so I have a newbie question;)
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 12-20-2012, 10:59 AM
  2. newbie
    By honeythigh in forum C++ Programming
    Replies: 10
    Last Post: 03-05-2011, 08:05 AM
  3. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  4. newbie q!
    By xzentorian in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2004, 04:38 PM
  5. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM

Tags for this Thread