Temperature conversion table

Hi everyone. I'm fairly new to C++ and I'm wondering if anyone can help me with this code. My assignment was to write a program that creates and displays a table of temperature conversions. Get the starting temperature from the keyboard in degrees Celsius (do not allow input of a value below absolute zero). Also get an integer value to represent the number of degrees to increment for each of a 20 row table (do not allow the increment value to be less than one. This is what I have so far

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double C,F,K,R,n,i;
cout <<"Enter starting temperature in Celsius: ";
cin >> C;
while (C <= -273.15)
{
cout <<"ERROR: Temp must be >= -273.15: ";
cin >> C;
}
{
n = n--;
cout << "Enter increments in degrees Celsius: ";
cin >> n;
}
{
i= 1; i++;
F= C * 1.8+32;
K= C + 273.15;
R= C * 1.8+32+459.67;
}
{
cout <<" #\t Cels\t Fahr\t Kelv\t Rank" <<endl;
}
cout <<" " << n << setw(4) << " " << C << setw(4) << " " << F
<< setw (6) << " " << K << setw (6) << " " << R << endl;
}

But when I put this through it only brings up the specfic thing I asked for and not the whole table. The program needs to output like this

Enter starting temperature in Celsius: -500
ERROR: Temp must be >= -273.15: -273.15
Enter increments in degrees Celsius: 100
# Cels Fahr Kelv Rank
1 -273.15 -459.67 0.00 0.00
2 -173.15 -279.67 100.00 180.00
3 -73.15 -99.67 200.00 360.00
4 26.85 80.33 300.00 540.00
5 126.85 260.33 400.00 720.00
6 226.85 440.33 500.00 900.00
7 326.85 620.33 600.00 1080.00
8 426.85 800.33 700.00 1260.00
9 526.85 980.33 800.00 1440.00
10 626.85 1160.33 900.00 1620.00
11 726.85 1340.33 1000.00 1800.00
12 826.85 1520.33 1100.00 1980.00
13 926.85 1700.33 1200.00 2160.00
14 1026.85 1880.33 1300.00 2340.00
15 1126.85 2060.33 1400.00 2520.00
16 1226.85 2240.33 1500.00 2700.00
17 1326.85 2420.33 1600.00 2880.00
18 1426.85 2600.33 1700.00 3060.00
19 1526.85 2780.33 1800.00 3240.00
20 1626.85 2960.33 1900.00 3420.00
Press any key to continue . . .

Can anyone help me?
Last edited on
You will need a for loop to make your program iterate 19 more times.
1
2
3
4
5
6
7
8
for(int index = 0; index < 19; ++index){

// Increment your temperature depending on the user input
// Calculate Fahr, Kelv, Rank
cout <<" " << n << setw(4) << " " << C << setw(4) << " " << F
<< setw (6) << " " << K << setw (6) << " " << R << endl;

}


while (C <= -273.15)

This should be just be < since you accept -273.15 as an input.
Last edited on
Take a look at this
www.cplusplus.com/articles/z13hAqkS/
I have done this for Dr. Tyson Mcmillan, in Fort Worth,//Program created by Thomas Cato and Cameron Swink

//Program created by Thomas Cato and Cameron Swink
#include <iostream>
#include "Input_Validation_Extended.h" //Provided by Dr. T
using namespace std; 

int main () 
{ 
	int decision =0; 
	double degree =0.0; 
	double converted = 0.0;
	//double used to use integers with decimals
	
	
	while ( decision!= 3) //Loop to continue program
	{
	
	cout << "For Celsius to Fahrenheit Conversion enter 1" <<endl; 
	cout << "For Fahrenheit to Celsius Conversion enter 2" <<endl; 
	cout << "To exit, enter 3: " <<endl;
	cout << "Please enter decision: " << endl;
	/*
	Above are output statements for temperature conversions
	These statements provide instructions for making a decision
	This allows for assignment of which particular method being used
	*/
	decision = validateInt(decision);

	
	
	
	
	
	

	

	if (decision == 1) // celcius conversion for decision 1
	{
		
		degree = validateDouble (degree); // doesn't allow user to enter characters
	
		
		converted =	(degree * (9.0/5.0)) + 32.0;
		cout << "You have decided to convert to Fahrenheit" << endl; 
		cout << "Your degree is " <<converted<< " Fahrenheit\n" << endl;



		
	}
	
	else if (decision == 2) //Fahrenheit conversion for decision 2
	{
	
	decision = validateDouble(degree); // doesn't allow user to enter characters
		
	converted =	(degree -32)* (5.0/9.0);
	cout << "You have decided to convert to Celsius" <<endl;
	cout << "Your degree is " <<converted<< " Celsius\n" << endl;	
 
		
	}
	else if (decision == 3)
	{
		cout << "Thank you for trying our program,\nGood Bye!" << endl;
	}
	else //default clause with error message
	{
		cout << "\n\nError, Something went wrong" << endl; 
	}
	}


return 0;// exit success 
}



You will have to tweek it to not allow negative numbers, but that should be easy. The header is needed as well, and depending on the software you will need to add it to the project. The header allows you to recieve only the needed data. Example: it will not allow you to enter a character for an 'Int' nor will it let you enter a integer for a 'char' and so on. I hope this helps! I literally made this account just to share this info, so I hope it reaches you in time.

Header found on stackoverflow (Created by Dr. Tyson McMillan):
//From: http://stackoverflow.com/questions/514420/how-to-validate-numeric-input-c 
#include <ios>  // Provides ios_base::failure
#include <iostream>  // Provides cin
#include<string> //for string manipulation
#include<sstream> //for string manipulation and comparison
using namespace std; 

//Function Prototypes
int validateInt(int &); //use the validation method to vaildate and return a data type integer pass by reference &
double validateDouble(double &); //use the validation method to vaildate and return a data type double pass by reference &
char validateChar(char &); //use the validation method to vaildate and return a data type char pass by reference &
string validateString(string &); //use the validation method to vaildate and return a data type string pass by reference &

template <typename T>
T getValidatedInput()
{
    // Get input of type T
    T result;
    cin >> result;

    // Check if the failbit has been set, meaning the beginning of the input
    // was not type T. Also make sure the result is the only thing in the input
    // stream, otherwise things like 2b would be a valid int.
    if (cin.fail() || cin.get() != '\n')
    {
        // Set the error state flag back to goodbit. If you need to get the input
        // again (e.g. this is in a while loop), this is essential. Otherwise, the
        // failbit will stay set.
        cin.clear();

        // Clear the input stream using and empty while loop.
        while (cin.get() != '\n')
            ;

        // Throw an exception. Allows the caller to handle it any way you see fit
        // (exit, ask for input again, etc.)
        throw ios_base::failure("Invalid input.");
    }

    return result;
}

//Function Definitions
int validateInt(int &intInput)
{
	while (true)
    {
        cout << "Please enter decision: ";

        try
        {
            intInput = getValidatedInput<int>();
        }
        catch (exception e)
        {
            cerr << e.what() << endl;
            continue;
        }

        break;
    }
    
    return intInput; 
}

double validateDouble(double &doubleInput)
{
	while (true)
    {
        cout << "Enter a grade with or without decimals to determine letter grade: ";

        try
        {
            doubleInput = getValidatedInput<double>();
        }
        catch (exception e)
        {
            cerr << e.what() << ": Invalid input."<< endl;
            continue;
        }

        break;
    }
    
    return doubleInput; 
}

char validateChar(char &charInput)
{ 
    while (true)
    {
        cout << "Read steps above to determine decision: ";

        try
        {
            charInput = getValidatedInput<char>();
        }
        catch (exception e)
        {
            cerr << e.what() << ": Invalid input."<< endl;
            continue;
        }

        break;
    }
    
    return charInput; 
}

string validateString(string &stringInput)
{
    while (true) //use cin, getline() for this 
    {
        cout << "Enter a word (no spaces): ";

        try
        {
            stringInput  = getValidatedInput<string>();
        }
        catch (exception e)
        {
            cerr << e.what() << ": Invalid input."<< endl;
            continue;
        }

        break;
    }
    
    return stringInput; 
}
Topic archived. No new replies allowed.