Need urget c++ help (nested if-else)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Need urget c++ help (nested if-else)

  1. #1
    Join Date
    Feb 2014
    Posts
    3

    Need urget c++ help (nested if-else)

    I'm having trouble with making a nested if-else program for my computer programming class, I really need some help with getting the variables set up and I believe I can work it out from there, but as for right now I am stuck.

    http://www.uwplatt.edu/csse/courses/CS143/prog1.html

    here are the guidelines please pm me if you can help! I need to have this done before 10 PM tonight and I can provide some FG is someone is really helpful in getting me going the right direction.

    I have some of the code written but I am a complete newbie so it could be all garbage.
    Code:
    //---------------------------------------------------------------------
    //
    // Name:   
    //
    // Course:  CS 1430, Section 2
    //
    // Purpose: A program that converts between kilometers and miles
    //          or between Fahrenheit temperature and Celcius.
    //
    // Input:   A character value of 'K' , 'M' , 'F' or 'C'.
    //          An int or float representing the amount.
    //
    // Output:  A correct conversion between 'K' and 'M' or 'F' and 'C'
    //          An error message shown for incorrect input of character or
    //          an error if the temperature or distance is out of range.
    //---------------------------------------------------------------------
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    const float MILES_PER_KM      = 1.0f / 1.61f;
    const float KM_PER_MILE       = 1.61f;
    const float FREEZING_F        = 32.0f;
    const float FAHR_PER_CELSIUS  = 9.0f / 5.0f;
    const float CELSIUS_PER_FAHR  = 5.0f / 9.0f;
    const float MAX_F_TEMP        = 100.0f;
    const float MIN_F_TEMP        = 0.0f;
    
    char k = 'K';
    char m = 'M';
    char f = 'F';
    char c = 'C';
    
    float unit;
    float amount = 0;
    
    int main()
    {
       cout << " Input a type (K, M, F, C) : " ;
       cin  >> unit;
       cout << " Input an amount : " ;
       cin  >> amount;
    
       if ( unit == 'K' )
       {
          if ( amount > 0 )
          {
             cout << " Distance " << amount << " miles is " 
                  << amount * MILES_PER_KM << " kilometers.";
          }
          
       }
       return 0;
    }

  2. #2
    Join Date
    Feb 2014
    Posts
    3

    Re: Need urget c++ help (nested if-else)

    //---------------------------------------------------------------------
    //
    // Name:
    //
    // Course: CS 1430, Section 2
    //
    // Purpose: A program that converts between kilometers and miles
    // or between Fahrenheit temperature and Celcius.
    //
    // Input: A character value of 'K' , 'M' , 'F' or 'C'.
    // An int or float representing the amount.
    //
    // Output: A correct conversion between 'K' and 'M' or 'F' and 'C'
    // An error message shown for incorrect input of character or
    // an error if the temperature or distance is out of range.
    //---------------------------------------------------------------------

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;

    const float MILES_PER_KM = 1.0f / 1.61f;
    const float KM_PER_MILE = 1.61f;
    const float FREEZING_F = 32.0f;
    const float FAHR_PER_CELSIUS = 9.0f / 5.0f;
    const float CELSIUS_PER_FAHR = 5.0f / 9.0f;
    const float MAX_F_TEMP = 100.0f;
    const float MIN_F_TEMP = 0.0f;

    char K = 'K' ;
    char M = 'M' ;
    char F = 'F' ;
    char C = 'C' ;

    char unit ;
    float amount ;

    int main()
    {
    cout << "Input a type and an amount (K, M, F, C): " ;
    cin >> unit >> amount ;

    if ( unit == 'K' )
    {
    if ( amount >= 0 )
    {
    cout << "Distance " << amount << " kilometers is "
    << amount * MILES_PER_KM << " miles." ;
    }
    else if ( amount < 0 )
    {
    cout << "UNABLE TO PROCESS: Negative distances are "
    << "not supported. " ;
    }
    }
    else if ( unit == 'M' )
    {
    if ( amount >= 0 )
    {
    cout << "Distance " << amount << " miles is "
    << amount * KM_PER_MILE << " kilometers." ;
    }
    else if ( amount <= 0 )
    {
    cout << "UNABLE TO PROCESS: Negative distances are "
    << "not supported. " ;
    }
    }
    else if ( unit == 'F' )
    {
    if ( amount <= MAX_F_TEMP && amount >= MIN_F_TEMP )
    {
    cout << "Temperature " << amount << " F is "
    << ( amount - FREEZING_F ) * CELSIUS_PER_FAHR << " C." ;
    }
    else
    {
    cout << "UNABLE TO PROCESS: Temperature too hot or too cold. "
    << endl; cout << "Please enter a temperature comfortable"
    << " when wearing a sweater. " << endl ;
    }
    }
    else if ( unit == 'C' )
    {
    cout << "Temperature " << amount << " C is "
    << amount * FAHR_PER_CELSIUS + FREEZING_F << " F." ;
    }
    else
    {
    cout << "UNABLE TO PROCESS: " << unit
    << " is not a recognized measurement." << endl;
    cout << "Please enter either K, M, F, or C." << endl;
    }


    return 0;
    }

    Even tho you guys werent very helpful, here is the finished product if you were curious

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,750

    Re: Need urget c++ help (nested if-else)

    Even tho you guys werent very helpful
    Some of us live in countries with different time-zones to you. I've only picked up this thread 10 hours after you posted it and I would certainly have provided some guidance for you.

    It's no good saying that you have an assignment to complete by a certain time and expect immediate help. We all here will provide what we can to help but we have our jobs as well.

    PS as per your first post, please use code tags when posting code. The code in your post #2 is just about unreadable.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Feb 2014
    Posts
    3

    Re: Need urget c++ help (nested if-else)

    Quote Originally Posted by 2kaud View Post
    Some of us live in countries with different time-zones to you. I've only picked up this thread 10 hours after you posted it and I would certainly have provided some guidance for you.

    It's no good saying that you have an assignment to complete by a certain time and expect immediate help. We all here will provide what we can to help but we have our jobs as well.

    PS as per your first post, please use code tags when posting code. The code in your post #2 is just about unreadable.
    I can understand that certain people couldnt read it, but it had over 50 views before the deadline and nobody offered so much as a tid bit of help. I meant to post the second one in code block but i simply forgot.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,750

    Re: Need urget c++ help (nested if-else)

    Quote Originally Posted by bensfriend1 View Post
    I can understand that certain people couldnt read it, but it had over 50 views before the deadline and nobody offered so much as a tid bit of help. I meant to post the second one in code block but i simply forgot.
    A lot of those 'views' would be from guests who aren't actually members of CodeGuru. As I write this post, there are 4 members on line - including you and me - and 753 guests. Also as there are many different forums (covering c#, visual basic, assembler, java .net etc etc) on-line members at any one time may not be c++ people. Although I'm a member and on-line now if a Visual Basic question was asked similar to yours, I would not be in a position to respond as I'm a c++ member.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Need urget c++ help (nested if-else)

    Quote Originally Posted by bensfriend1 View Post
    I can understand that certain people couldnt read it, but it had over 50 views before the deadline and nobody offered so much as a tid bit of help. I meant to post the second one in code block but i simply forgot.
    Instead of complaining about others, why don't you have a good look at yourself first? You were able to solve the problem yourself (or at least, without help from anyone here) in 2 hours. Yet you did not want to spend that time and effort before asking for help here. What if somebody would have spend 15 minutes typing a long reply to your OP just before you posted your solution? They would have wasted their time on you, that's what.

    There are a lot of very experienced people here who provide their help to anyone free of charge. But you cannot demand to receive help, because no one is obliged to help you. It's all done out of free will. And saying your question is "urgent" does not entitle you to receive more help or receive help faster than anyone else posting here. Your deadline is your problem.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Need urget c++ help (nested if-else)

    Quote Originally Posted by bensfriend1 View Post
    I can understand that certain people couldnt read it, but it had over 50 views before the deadline and nobody offered so much as a tid bit of help.
    This is the real world. If you want anybody to cooperate, you need to offer something that would make others be interested in the cooperation. "Urgent assignment" is the worst advertisement to your issue here on the forum. Offering money is the second worst.

    When I see another assignment tried to be accomplished by having community involved I always go by, as I'm sure the best favor I can do to a student is leaving him alone facing the problem he is supposed to solve on his own. Ability to accomplish tasks being not relying on anybody is a great skill for your future career, of course if you plan to make one. And this is exactly what assignments are about.

    But I always am willing to help in solving real engineer issues, especially the ones that catch me. Yours did not, sorry.
    Last edited by Igor Vartanov; February 16th, 2014 at 01:24 PM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center