如果有人能帮我完成这个并指出必须做些什么才能使程序运行,那就太好了。
-询问用户想输入多少天从1-365(验证)
-询问-60 到 90 摄氏度之间每一天的温度(循环,验证)
-将每个值转换为华氏度(函数)
-输出结果(函数)
问题
-用户应该输入一个整数摄氏度数,一个整数但它会转换为双华氏度数 前任。 4 摄氏度转换为 39.20 华氏度
对于这段代码...
cout << "当天的摄氏温度 "<< i+1 << ": ";
*(天 + 1) = GetValidInt("", TEMP_MIN, TEMP_MAX);
它在新的一行输出并等待输入...我怎样才能让它在同一行等待输入?
#include <IOSTREAM> // input/outout stream
#include <IOMANIP> // for setw manipulator
#include <CFLOAT> // for limits of a double DBL_MIN and DBL_MAX
#include <CLIMITS> // for limits of a int INT_MIN and INT_MAX
using namespace std;
//Function Prototypes
double GetValidDouble(string prompt = "", const double MIN = DBL_MIN, const double MAX = DBL_MAX);
int GetValidInt(string prompt = "", const int MIN = INT_MIN, const int MAX = INT_MAX);
int outputFunc (int*);
double calcCelsius(double*);
int main() {
//Constants
const int TEMP_MIN = -90;
const int TEMP_MAX = 60;
const int DAYS_MIN = 1;
const int DAYS_MAX = 365;
//Pointer
int numDays;
int *days;
//Determine the number of days to get input for
//Validation - Must be numeric || Between 1 - 365
numDays = GetValidInt("How many days do you wish to enter? ", DAYS_MIN, DAYS_MAX);
//Array Allocation
cout << "TEMPRETURE REPORTER" << endl;
cout << "====================" << endl;
cout << "Please enter the tempreture for each day." << endl;
try{
days = new int[numDays];
for(int i = 0; i < numDays; i++){
cout << "Celsius temperature for Day " << i+1 << " : ";
*(days + 1) = GetValidInt("", TEMP_MIN, TEMP_MAX);
//Validation - Between -90.00C and 60.00C
}
//for loop
for(int i = 0; i < numDays; i++){
cout << "" << outputFunc(&days);
}
//output function
delete[] days;
}
catch(bad_alloc){
cout << "\nCould not allocate that amount memory.";
}
cout << endl << endl;
system("pause");
return 0;
}
//An inline function is a function upon which the compiler has been requested to perform inline expansion.
//In other words, the programmer has requested that the compiler insert the complete body of the function
//in every place that the function is called, rather than generating code to call the function in the one place it is defined.
inline double calcCelsius(double* celsius){
double fahrenheit = 0;
fahrenheit = (celsius*9/5)+32;
return fahrenheit;
}
//Output Function
int outputFunc (int* days){
double fahrenheit = 0;
//PROCESS
//double //&days int
fahrenheit = calcCelsius(&days); //Calling calcCelsius
//OUTPUT
cout << right << setw(15) << "Day " << numDays << setw(10) << fahrenheit << char(248) << "F" << setw(10) << numDays << char(248) << "C" << endl;
}
double GetValidDouble(string prompt, const double MIN, const double MAX){
double validNumber = 0.0; // holds the user input
string rubbish; // holds garbage input.
cout << endl << prompt << " ";
cin >> validNumber; // try to get input
if(cin.fail()){ // if user input fails...
cin.clear(); // reset the cin object
cin >> rubbish; // cleans garbage from cin.
// report the problem to the user.
cerr << "\nInvalid input. Please try again and enter a numeric value.\n";
// Try again by calling the function again (recursion)
validNumber = GetValidDouble(prompt, MIN, MAX);
}
else if(validNumber < MIN || validNumber > MAX){// if value is outside range...
// report the problem to the user.
cerr << "\nInvalid input. Please try again and enter a value between "
<< MIN << " and " << MAX << ".\n";
// Try again by call the function again (recursion)
validNumber = GetValidDouble(prompt, MIN, MAX);
}
return validNumber; // returns a valid value to the calling function.
}
int GetValidInt(string prompt, const int MIN, const int MAX){
double validNumber = 0.0; // holds the user input
validNumber = GetValidDouble(prompt, MIN, MAX); // get a number
if((int) validNumber < validNumber) // if the value is not a whole number...
{
cerr << "\nInvalid input. Please try again and enter a whole number.\n";
// Try again by calling the function again (recursion)
validNumber = GetValidInt(prompt, MIN, MAX);
}
return (int) validNumber; // returns a valid value to the calling function.
}
最佳答案
您在请求数字之前输出一个换行符:
cout << endl << prompt << " "; // Output a line break ????
cin >> validNumber; // try to get i
只需删除上面的第一行。
关于c++ - 指点功课,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9824471/