如果有人能帮助我完成这项工作,并指出必须采取哪些措施才能使该计划有效,那就太棒了。
- 用户想要从1-365进入的天数(验证)
- 每天在-60到90摄氏度之间的温度(循环,验证)
- 将每个值转换为华氏度(函数)
- 输出结果(功能)
问题
- 用户应输入一个int Celsius数字,一个整数,但它会转换为一个两倍的华氏数字 恩。 4摄氏度转换为39.20华氏度
此代码......
cout<< "摄氏温度为白天" << i + 1<< " :&#34 ;;
*(天+ 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.
}
答案 0 :(得分:1)
您在请求号码前输出换行符:
cout << endl << prompt << " "; // Output a line break ????
cin >> validNumber; // try to get i
只需删除上面的第一行。
答案 1 :(得分:1)
你的问题就在这一行:
cout << endl << prompt << " ";
getValidDouble
中的。它在从用户获取值之前输出换行符,在之后输出您自己的提示。
为什么你实际上没有使用 prompt
参数来询问什么?显然有一个原因: - )
这样,它将在换行符之后输出,您的输入将在同一行。换句话说,就像改变:
cout << "Celsius temperature for Day " << i+1 << " : ";
*(days + 1) = GetValidInt("", TEMP_MIN, TEMP_MAX);
成:
#include <sstream>
:
std::stringstream ss;
ss << "Celsius temperature for Day " << (i+1) << " : ";
*(days + 1) = GetValidInt (ss.str(), TEMP_MIN, TEMP_MAX);
这应该可以解决您的问题,提示后的换行符。您还有一些其他问题,例如将错误的数据类型传递给函数,但我会让它们为您修复,因为它会让您成为更好的程序员。一个提示,编译时带有警告,读取编译器告诉你的内容。
答案 2 :(得分:0)
尝试,我的意思是尝试,xcode没有标记任何错误,但没有时间运行它并调试它,这(显然它不完整,只是剪切和粘贴在你的上面)
//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);
void outputFunc (int*,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
numDays = GetValidInt("How many days do you wish to enter? ", DAYS_MIN, DAYS_MAX); //Validation - Must be numeric || Between 1 - 365
//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++){
string prompt = "Celsius temperature for Day " + (i+1);
*(days + 1) = GetValidInt(prompt, TEMP_MIN, TEMP_MAX); //Validation - Between TEMP_MIN and TEMP_MAX
}
//for loop
for(int i = 1; i < numDays; i++){
cout << "" ;
outputFunc(days,i); //we send the whole array
}
//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(int celsius){
return (celsius*9/5)+32; //parentheses not really needed
}
//Output Function
void outputFunc (int* days, int a){
int temp = days[a];
cout << right << setw(15) << "Day " << a << setw(10) << calcCelsius(temp) << char(248) << "F" << setw(10) << &days << char(248) << "C" << endl;
}
我没有改变任何其他事情。我不知道这是否真的是你需要的,我的意思是它完成了工作(可能),或者至少它会有一些变化。但我不知道你是否应该特别使用任何东西。
主要变化:
newDay在outputFunc中脱离了上下文,所以你需要 通过它。但无论如何它是你需要发送的计数器。
calcCelcius(我将其命名为deg2far或celc2far)现在返回一个 double并采用int(仍为内联)
请记住,您在i + 1中存储值(从0开始) 因此,您的第一个值是以天为单位[1] ...输出循环应该开始 在1。
可能还有其他一些变化(显然是funcs的原型),但那些是主要的。
我不确定第54行string prompt = "Celsius temperature for Day " + (i+1);
是否可行,也许您需要单独声明它。