d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Computer Programming > Need Help With Simple Program
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 07:44pm
Quote (bensfriend1 @ Feb 14 2014 09:39pm)
I know what those mean I was having trouble with getting all my variables and stuff set up because i dont really understand what the difference between char commands and like int or float is.


Those are called TYPES and char, int, float is the type name. Char is a single character, int is a integer, floats are integers with decimal percision, strings are an array of characters (more than one character).

If you are confused about variables I suggest a quick read of: http://www.cplusplus.com/doc/tutorial/variables/

edit: Also that code would not compile, you are missing a closing brace near the end.

This post was edited by AbDuCt on Feb 14 2014 07:44pm
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 07:47pm
Quote (AbDuCt @ Feb 14 2014 07:44pm)
Those are called TYPES and char, int, float is the type name. Char is a single character, int is a integer, floats are integers with decimal percision, strings are an array of characters (more than one character).

If you are confused about variables I suggest a quick read of: http://www.cplusplus.com/doc/tutorial/variables/

edit: Also that code would not compile, you are missing a closing brace near the end.



I was just copying what i had to show what i had changed, I have quite a bit more of the code written now that I seemed to eclipse my hurdle. If it is at all possible would you be willing to look at this code when I am done with it? I really appreciate the help your providing and I will read that article when I finish this section of the program. Thanks!
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 07:48pm
Quote (bensfriend1 @ Feb 14 2014 09:47pm)
I was just copying what i had to show what i had changed, I have quite a bit more of the code written now that I seemed to eclipse my hurdle.  If it is at all possible would you be willing to look at this code when I am done with it?  I really appreciate the help your providing and I will read that article when I finish this section of the program.  Thanks!


Yea I can give it a look when you're done.

This post was edited by AbDuCt on Feb 14 2014 07:48pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 14 2014 07:49pm
Quote (bensfriend1 @ Feb 14 2014 08:47pm)
I was just copying what i had to show what i had changed, I have quite a bit more of the code written now that I seemed to eclipse my hurdle.  If it is at all possible would you be willing to look at this code when I am done with it?  I really appreciate the help your providing and I will read that article when I finish this section of the program.  Thanks!


why does he need to look at it when you're done? it'll all work when you're done, no need for anyone else to look at it.
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 08:10pm
Quote (AbDuCt @ Feb 14 2014 07:48pm)
Yea I can give it a look when you're done.


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

char 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 << " 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 * FAHR_PER_CELSIUS - FREEZING_F << " 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 * CELSIUS_PER_FAHR + FREEZING_F << " F.";
}
else
{
cout << " UNABLE TO PROCESS: " << unit
<< "is not a recognized measurement. ";
}


return 0;
}


Below is Your Output, with line numbers added:

1: Input a type (K, M, F, C) : Input an amount : Distance 2.5 miles is 4.025 kilometers.
**********************************************************

Below is the Correct Output, with line numbers added:

1: Input a type and an amount (K, M, F, C): Distance 2.5 miles is 4.025 kilometers.
**********************************************************

this is what my grader said, how do i get that initial line into one line that accepts two inputs?

This post was edited by bensfriend1 on Feb 14 2014 08:18pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 08:28pm
Not sure if this is what you mean, but you can add multiple variables to a single cin call.

Code
cin >> unit >> amount;
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 08:33pm
Quote (AbDuCt @ Feb 14 2014 08:28pm)
Not sure if this is what you mean, but you can add multiple variables to a single cin call.

Code
cin >> unit >> amount;


PERFECT!!! thanks so much, just going through and fixing the little things now so the grader gives 0 differences. I REALLY appreciate the help thanks so much!!!
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 09:19pm
Here is the finished code, can anyone tell if theres any unnecessary/ unused code in this cuz those are worth the most points off

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

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;
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 09:31pm
If you use your operators correctly you can eliminate all your redundant error checking.

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

if (unit == 'K' && amount >= 0) {
cout << "Distance " << amount << " kilometers is "
<< amount * MILES_PER_KM << " miles.";
}
else if (unit == 'M' && amount >= 0) {
cout << "Distance " << amount << " miles is "
<< amount * KM_PER_MILE << " kilometers.";
}
else if (unit == 'F' && amount <= MAX_F_TEMP && amount >= MIN_F_TEMP) {
cout << "Temperature " << amount << " F is "
<< ( amount - FREEZING_F ) * CELSIUS_PER_FAHR << " C.";
}
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 and a value above 0" << endl;
}

return 0;
}


It limits your specific error messages for each if branch but it reduces the amount of code. Only do this if your project does not state you need proper error messages.

You can also remove your <string> include as you are not using any string manipulation functions.

Also double check it compiles and works properly after any changes.

edit:: Also your Celsius to Fahrenheit if branch does not have any error checking. Not sure if this was by design.

edit2:: I am also not a fan of the expanded style of C I much rather condense everything as shown in the edited code. Although again it is up to how you are being taught style wise. Be sure you wont lose marks for using your own coding style.

This post was edited by AbDuCt on Feb 14 2014 09:41pm
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 09:39pm
#include <iostream>
#include <iomanip>
#include <string>

@abduct what do the first two mean besides just the string? can i remove those as well?

And the C to F code doesnt need error checking as far as i can tell on the project outline

I believe I need the redundant error and more code because the project really wants the proper error messages.
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll