Thread: No result out for this CP

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Kuala Lumpur, Malaysia, Malaysia
    Posts
    5

    No result out for this CP

    No any result out for tis CP. Any one can help? TQ

    Code:
    #include <stdio.h>
    
    main()
    {
        int i,split,n_split;
        float km,n_km;
    
    
        printf("Miles\tKilometers\tMiles\tKilometers\n");
    
        for(i=1;i<=split;i++)
        {
            km=i*1.61;
            split=(1+20)/2;
            n_split=split+i;
            n_km=n_split*1.61;
            printf("%d\t%.2f\t\t%d\t%.2f\n",i,km,n_split,n_km);
    
        }
    
        return 0;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by leevenchoon View Post
    No any result out for tis CP. Any one can help? TQ

    Code:
    #include <stdio.h>
    
    main()
    {
        int i,split,n_split;
        float km,n_km;
    
    
        printf("Miles\tKilometers\tMiles\tKilometers\n");
    
        for(i=1;i<=split;i++)
        {
            km=i*1.61;
            split=(1+20)/2;
            n_split=split+i;
            n_km=n_split*1.61;
            printf("%d\t%.2f\t\t%d\t%.2f\n",i,km,n_split,n_km);
    
        }
    
        return 0;
    }
    What did you expect it to do ?
    Shouldn't you give an initial value to split ?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You have not initialised the variable named split before the loop. That means your code exhibits undefined behaviour due to accessing the value of an uninitalised variable, which means ANYTHING is allowed to happen.

    In practice, you are probably getting lucky, and split probably contains a value less than 1, so the loop body is never executed. However, with undefined behaviour, more gruesome possibilities are feasible, such as the program being terminated with segmentation violation or it simply reformatting your hard drive.

    The solution is to initialise split to something that makes sense before the loop, not within the loop body.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be helpful if you posted C in the C section, not the C++ section.
    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
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Elysia View Post
    It would be helpful if you posted C in the C section, not the C++ section.
    I think your expectations are set just a little too high
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by AndrewHunter View Post
    I think your expectations are set just a little too high
    Elysia has given fair guidance, and his expectations are reasonable, so I think you set the bar a little too low.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by grumpy View Post
    Elysia has given fair guidance, and his expectations are reasonable, so I think you set the bar a little too low.
    I was not suggesting anything to the contrary. It was just meant as a joke; no offense intended.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Location
    Kuala Lumpur, Malaysia, Malaysia
    Posts
    5
    Any idea how 2 fix it?

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You were told how to fix it. Where do you define split(in red)? Is it before or after you use it(in green)?
    Quote Originally Posted by leevenchoon View Post
    Code:
    #include <stdio.h>
    
    main()
    {
        int i,split,n_split;
        float km,n_km;
    
    
        printf("Miles\tKilometers\tMiles\tKilometers\n");
    
        for(i=1;i<=split;i++)
        {
            km=i*1.61;
            split=(1+20)/2;
            n_split=split+i;
            n_km=n_split*1.61;
            printf("%d\t%.2f\t\t%d\t%.2f\n",i,km,n_split,n_km);
    
        }
    
        return 0;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Location
    Kuala Lumpur, Malaysia, Malaysia
    Posts
    5
    i test at dev C++ have problems. but my friends test at Code Blocks no problems.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by grumpy View Post
    You have not initialised the variable named split before the loop. That means your code exhibits undefined behaviour due to accessing the value of an uninitalised variable, which means ANYTHING is allowed to happen.
    Hence undefined. Move your variable definition to before your for loop.
    Code:
    split=(1+20)/2;
    for(i=1;i<=split;i++)
        {
            km=i*1.61;
            n_split=split+i;
            n_km=n_split*1.61;
            printf("%d\t%.2f\t\t%d\t%.2f\n",i,km,n_split,n_km);
    
        }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Location
    Chandigarh, India, India
    Posts
    2
    Give some initial value greater than i, that is greater than 1 to split so that loop can be initialized. Change x to any value>i.

    Code:
    int i,split=x,n_split;
        float km,n_km;
        clrscr();
    
    
        printf("Miles\tKilometers\tMiles\tKilometers\n");
    
        for(i=1;i<=split;i++)
        {
            km=i*1.61;
            split=(1+20)/2;
            n_split=split+i;
            n_km=n_split*1.61;
            printf("%d\t%.2f\t\t%d\t%.2f\n",i,km,n_split,n_km);
    
        }

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by PrashantDhamija View Post
    Give some initial value greater than i, that is greater than 1 to split so that loop can be initialized. Change x to any value>i.
    There is no reason to do this. Split does not get calculated in the loop, it is a constant. Simply moving it to before the for loop is all that is required.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unexpected result
    By abotaha in forum C++ Programming
    Replies: 14
    Last Post: 11-17-2009, 08:32 PM
  2. Help with result only...
    By Ervilha in forum C Programming
    Replies: 5
    Last Post: 08-19-2008, 11:29 AM
  3. What would be the result...
    By sreeramu in forum C Programming
    Replies: 8
    Last Post: 03-25-2008, 04:43 AM
  4. No Result when run *.exe
    By hednast in forum C Programming
    Replies: 6
    Last Post: 08-23-2005, 12:50 AM
  5. wrong result?
    By o0o in forum C++ Programming
    Replies: 5
    Last Post: 01-11-2004, 09:53 AM