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

Thanks!

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 06:13pm
Member
Posts: 15,942
Joined: Aug 11 2007
Gold: 8,221.76
Feb 14 2014 06:32pm
i see u posted in homework help

but id have tried programming for a question like this, bit more than just something quick. althought doubt theyd just do it for u, prob just help u out a bit.

and been forever since i last tried some C++ , was like high school lol. and didnt do too well then... didnt pay attention
Member
Posts: 22,458
Joined: Dec 6 2008
Gold: 14.00
Trader: Trusted
Feb 14 2014 06:43pm
why not just do
String unit = "
if(unit == k){
shit;
} else {
if(unit== m){
else;
} else {
if(!unit.Contains(k, m) {
return;
}

my rusty java.. lel
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 06:46pm
Quote (Candyzcanes @ Feb 14 2014 06:43pm)
why not just do
String unit = "
if(unit == k){
shit;
} else {
if(unit== m){
else;
} else {
if(!unit.Contains(k, m) {
return;
}

my rusty java.. lel


this looks like it would work, but I have to do it the very specific way my professor wants it and C++ compared to your java are different even though im sure they get the same result.

Quote (noob_whacker @ Feb 14 2014 06:32pm)
i see u posted in homework help

but id have tried programming for a question like this, bit more than just something quick. althought doubt theyd just do it for u, prob just help u out a bit.

and been forever since i last tried some C++ , was like high school lol. and didnt do too well then... didnt pay attention


theres a programming section? please link me up! unless thats just the miscellaneous help for computers now.
Member
Posts: 105,139
Joined: Apr 25 2006
Gold: 10,475.00
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 07:05pm
Quote (Ghot @ Feb 14 2014 06:49pm)


Thanks!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 14 2014 07:09pm
Quit making so many threads. I hate it when i start typing a long response to one thread, but someone already answered it in another thread or via PM. I don't bother anymore.
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 07:12pm
Quote (carteblanche @ Feb 14 2014 07:09pm)
Quit making so many threads. I hate it when i start typing a long response to one thread, but someone already answered it in another thread or via PM. I don't bother anymore.

I didnt mean to make so many but I procrastinated way too long on this because I thought it was easier than it was...add that to the fact that I didnt know there was a programming section or homework help and you get the situation I am in with all the threads i created.

If you do know how to help though please shoot me a PM because I really just need to get kickstarted and I think I can finish this.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 07:36pm
What are you confused about?

Code

if(condition) {
do condition true
} else if(condition) {
do condition based on another different trueth
} else if(condition) {
do condition based on another different trueth
} else {
do something if none of the conditions are true
}


It's piss simple stuff.
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Feb 14 2014 07:39pm
Quote (AbDuCt @ Feb 14 2014 07:36pm)
What are you confused about?

Code
if(condition) {
  do condition true
} else if(condition) {
  do condition based on another different trueth
} else if(condition) {
  do condition based on another different trueth
} else {
  do something if none of the conditions are true
}


It's piss simple stuff.



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.

heres my newer code that has a little more promise than above considering it gives me an output that i kind of want...
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' )
{

return 0;
}
Go Back To Programming & Development Topic List
1235Next
Add Reply New Topic New Poll