d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Computer Programming > Need Help With Simple Program
Add Reply New Topic New Poll
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 06:51pm
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;

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;
}


This post was edited by bensfriend1 on Feb 14 2014 07:08pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 14 2014 10:38pm
Seems like you missed your due date. Here is an overly engineered version:

Code
#include <iostream>
#include <exception>

class UnableToProcessException : public std::exception
{


public:
UnableToProcessException(std::string ss) : s(ss) {}
~UnableToProcessException() throw () {}
const char* what() const throw()
{
return (std::string("UNABLE TO PROCESS: ") + s).c_str();
}

private:
std::string s;
};

class Measurement
{

public:
Measurement() {}
Measurement(float _amount, char _type) : amount(_amount), type(_type) {}
~Measurement() throw () {}
float amount;
char type;
};

class UnitConverter
{
public:
Measurement convert(Measurement) throw(UnableToProcessException);
private:
static const float MILES_PER_KM = 1.0f / 1.61f;
static const float KM_PER_MILE = 1.61f;
static const float FREEZING_F = 32.0f;
static const float FAHR_PER_CELSIUS = 9.0f / 5.0f;
static const float CELSIUS_PER_FAHR = 5.0f / 9.0f;
static const float MAX_F_TEMP = 100.0f;
static const float MIN_F_TEMP = 0.0f;

void assertValidData(Measurement) throw(UnableToProcessException);
char getConvertedType(char) throw(UnableToProcessException);


};

void UnitConverter::assertValidData(Measurement measurement) throw(UnableToProcessException)
{

if(measurement.type == 'F' && (measurement.amount < MIN_F_TEMP || measurement.amount > MAX_F_TEMP))
{
throw UnableToProcessException("Temperature too hot or too cold.\nPlease enter a temperature comfortable when wearing a sweater.");
}
else if((measurement.type == 'M' || measurement.type == 'K') && measurement.amount < 0)
{
throw UnableToProcessException("Negative distances are not supported.");
}

}

char UnitConverter::getConvertedType(char type) throw(UnableToProcessException)
{

char converted;

switch(type)
{
case 'K':
converted = 'M';
break;
case 'M':
converted = 'K';
break;
case 'F':
converted = 'C';
break;
case 'C':
converted = 'F';
break;
default:
throw UnableToProcessException(std::string("")+type+" is not a recognized measurement.");
}

return converted;
}

Measurement UnitConverter::convert(Measurement measurement) throw(UnableToProcessException)
{

Measurement convertedMeasurement;

assertValidData(measurement);


switch(measurement.type)
{
case 'K':
convertedMeasurement.amount = measurement.amount * MILES_PER_KM;
break;
case 'M':
convertedMeasurement.amount = measurement.amount * KM_PER_MILE;
break;
case 'F':
convertedMeasurement.amount = (measurement.amount - FREEZING_F) * CELSIUS_PER_FAHR;
break;
case 'C':
convertedMeasurement.amount = (measurement.amount * FAHR_PER_CELSIUS) + FREEZING_F;
break;
default:
throw UnableToProcessException(std::string("")+measurement.type+" is not a recognized measurement.\nPlease enter either K, M, F, or C.");

}

convertedMeasurement.type = getConvertedType(measurement.type);
assertValidData(convertedMeasurement);

return convertedMeasurement;




}


class Driver
{

public:
void run();
private:
std::string getFormatString(char);



};

std::string Driver::getFormatString(char type)
{
std::string s;

//nested if statement to satisfy the "requirement"
if(type == 'K' || type == 'M')
{
s = "Distance";
if(type == 'K')
{
s+= " %g kilometers is %g miles.";
}
else if(type == 'M')
{
s+= " %g miles is %g kilometers.";
}
}
else if(type == 'C' || type == 'F')
{
s = "Temperature";
if(type == 'C')
{
s+= " %g C is %g F.";
}
else if(type == 'F')
{
s+= " %g F is %g C.";
}
}

return s;

}
void Driver::run()
{
char type;
float amount;
const char* format;
std::cout << "Input a type and an amount (K, M, F, C): ";
std::cin >> type >> amount;


Measurement measurement, convertedMeasurement;
UnitConverter uc = UnitConverter();

try
{
measurement = Measurement(amount,type);


convertedMeasurement = uc.convert(measurement);

format = getFormatString(type).c_str();

printf(format,measurement.amount,convertedMeasurement.amount);
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
}




}


int main()
{
Driver d;

d.run();

}


This post was edited by Minkomonster on Feb 14 2014 11:04pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 14 2014 10:42pm
Quote
Seems like you missed your due date.


this is why i hate it when people make multiple topics and dont follow up. he got his solution in the CHD topic.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 14 2014 10:54pm
Quote (carteblanche @ Feb 14 2014 11:42pm)
this is why i hate it when people make multiple topics and dont follow up. he got his solution in the CHD topic.


Meh, oh well. I was half hoping he would take my solution line for line, and try to submit it as his own. lol
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 15 2014 01:53pm
even though I have this done now, if anyone would like to post some informational sources for beginners to learn about C++ I would definitely appreciate it and read into them!
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 15 2014 10:26pm
Quote (Minkomonster @ 14 Feb 2014 23:38)
Seems like you missed your due date. Here is an overly engineered version:

Code
#include <iostream>
#include <exception>

class UnableToProcessException : public std::exception
{
 
 
  public:
          UnableToProcessException(std::string ss) : s(ss) {}
  ~UnableToProcessException() throw () {}
  const char* what() const throw()
  {
        return (std::string("UNABLE TO PROCESS: ") + s).c_str();
  }
 
  private:
          std::string s;
};

class Measurement
{
     
      public:
            Measurement() {}
            Measurement(float _amount, char _type) : amount(_amount), type(_type) {}
            ~Measurement() throw () {}
            float amount;
            char type;
};

class UnitConverter
{
  public:
          Measurement convert(Measurement) throw(UnableToProcessException);
  private:
        static const float MILES_PER_KM      = 1.0f / 1.61f;
        static const float KM_PER_MILE      = 1.61f;
        static const float FREEZING_F        = 32.0f;
        static const float FAHR_PER_CELSIUS  = 9.0f / 5.0f;
        static const float CELSIUS_PER_FAHR  = 5.0f / 9.0f;
        static const float MAX_F_TEMP        = 100.0f;
        static const float MIN_F_TEMP        = 0.0f;
       
        void assertValidData(Measurement) throw(UnableToProcessException);
        char getConvertedType(char) throw(UnableToProcessException);
       
       
};

void UnitConverter::assertValidData(Measurement measurement) throw(UnableToProcessException)
{
   
    if(measurement.type == 'F' && (measurement.amount < MIN_F_TEMP || measurement.amount > MAX_F_TEMP))
    {
        throw UnableToProcessException("Temperature too hot or too cold.\nPlease enter a temperature comfortable when wearing a sweater.");
    }
    else if((measurement.type == 'M' || measurement.type == 'K') && measurement.amount < 0)
    {
        throw UnableToProcessException("Negative distances are not supported.");
    }
   
}

char UnitConverter::getConvertedType(char type) throw(UnableToProcessException)
{
   
    char converted;
   
    switch(type)
    {
        case 'K':
            converted = 'M';
            break;
        case 'M':
            converted = 'K';
            break;
        case 'F':
            converted = 'C';
            break;
        case 'C':
            converted = 'F';
            break;
        default:
              throw UnableToProcessException(std::string("")+type+" is not a recognized measurement."); 
    }
   
    return converted;
}

Measurement UnitConverter::convert(Measurement measurement) throw(UnableToProcessException)
{
   
    Measurement convertedMeasurement;
     
    assertValidData(measurement);
       
   
    switch(measurement.type)
      {
        case 'K':
              convertedMeasurement.amount = measurement.amount * MILES_PER_KM;
              break;
        case 'M':
              convertedMeasurement.amount = measurement.amount * KM_PER_MILE;
              break;
        case 'F':
              convertedMeasurement.amount = (measurement.amount - FREEZING_F) * CELSIUS_PER_FAHR;
              break;
        case 'C':
              convertedMeasurement.amount = (measurement.amount * FAHR_PER_CELSIUS) + FREEZING_F;
              break;
        default:
                throw UnableToProcessException(std::string("")+measurement.type+" is not a recognized measurement.\nPlease enter either K, M, F, or C.");
       
      }
     
      convertedMeasurement.type = getConvertedType(measurement.type);
      assertValidData(convertedMeasurement);
     
      return convertedMeasurement;
     
     
     
     
}


class Driver
{
     
      public:
            void run();
      private:
              std::string getFormatString(char);
   
           
     
};

std::string Driver::getFormatString(char type)
{
  std::string s;
   
    //nested if statement to satisfy the "requirement"
  if(type == 'K' || type == 'M')
  {
          s = "Distance";
      if(type == 'K')
      {
              s+= " %g kilometers is %g miles.";
      }
      else if(type == 'M')
      {
            s+= " %g miles is %g kilometers.";
      }
  }
  else if(type == 'C' || type == 'F')
  {
      s = "Temperature";
        if(type == 'C')
        {
            s+= " %g C is %g F.";
        }
        else if(type == 'F')
        {
            s+= " %g F is %g C.";
        }
  }
   
  return s;
 
}
void Driver::run()
{
    char type;
    float amount;
    const char* format;
    std::cout << "Input a type and an amount (K, M, F, C): ";
    std::cin >> type >> amount;
   
 
    Measurement measurement, convertedMeasurement;
    UnitConverter uc = UnitConverter();
   
    try
    {
          measurement = Measurement(amount,type);
             
         
          convertedMeasurement = uc.convert(measurement);
                 
          format = getFormatString(type).c_str();
         
          printf(format,measurement.amount,convertedMeasurement.amount);
    }
    catch(std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
   
   
   
     
}


int main()
{
    Driver d;
   
    d.run();
 
}


Wow.. that is something else. I like how it's so many lines but then outputs that last bit. B) I will reverse engineer this program and learn some new stuff. :D

This post was edited by NinjaSushi2 on Feb 15 2014 10:28pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 15 2014 11:28pm
Quote (NinjaSushi2 @ Feb 15 2014 11:26pm)
Wow.. that is something else. I like how it's so many lines but then outputs that last bit.  B) I will reverse engineer this program and learn some new stuff. :D


It is overly engineered. Don't read too much into the LOC. I did that on purpose.
Member
Posts: 3,580
Joined: Aug 17 2013
Gold: 275.01
Feb 16 2014 03:15pm
Quote (Minkomonster @ Feb 16 2014 02:28am)
It is overly engineered. Don't read too much into the LOC. I did that on purpose.


Well that's a sexy way of implementing a converter o.o I wish I applied myself like this everytime I wrote code -.- Thanks for the motivation
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 16 2014 03:37pm
Quote (SCVonSteroids @ Feb 16 2014 04:15pm)
Well that's a sexy way of implementing a converter o.o I wish I applied myself like this everytime I wrote code -.- Thanks for the motivation


It just takes the very simple problem and complicates it by creating 4 separate classes, and using crude exception handling to check for proper input. If you look at the solution from the other thread, it can be done in the main method in under 20 lines of code. Mine is a bit more than that lol.

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll