简体   繁体   English

指针家庭作业

[英]Pointers Homework

If anyone can help me finish this and point out what had to be done to make the program work, that would be great. 如果有人能帮助我完成这一点并指出必须采取哪些措施才能使程序正常运行,那就太棒了。

-ask for how many days user wants to enter from 1-365 (validate) - 用户想要从1-365进入的天数(验证)

-ask for temperature for each days between -60 and 90 degrees Celsius (loop, validate) -ask每天的温度在-60到90摄氏度之间(循环,验证)

-convert each value to Fahrenheit (function) - 将每个值转换为华氏度(函数)

-output results (function) - 输出结果(功能)

Problem 问题

-the users should input a int Celsius number, a whole number but it converts to a double fahrenheit number ex. - 用户应该输入一个int Celsius数字,一个整数,但它会转换为华氏数的两倍。 4 celsius converts to 39.20 fahrenheit 4摄氏度转换为39.20华氏度

  • for this code... 这个代码......

    cout << "Celsius temperature for Day " << i+1 << " : "; cout <<“Celsius temperature for Day”<< i + 1 <<“:”;

    *(days + 1) = GetValidInt("", TEMP_MIN, TEMP_MAX); *(天+ 1)= GetValidInt(“”,TEMP_MIN,TEMP_MAX);

it outputs and waits for the input on a new line...how can i make it wait for input on the same line? 它输出并等待新线路上的输入...如何让它等待同一线路上的输入?

    #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.
            }

You are outputting a line break before requesting the number: 您在请求号码之前输出换行符:

  cout << endl << prompt << " ";  // Output a line break ????
  cin >> validNumber;       // try to get i

Just remove the first line above. 只需删除上面的第一行。

Your problem is this line: 你的问题是这一行:

cout << endl << prompt << " ";

in getValidDouble . getValidDouble It outputs a newline character before getting the value from the user, after you've output your own prompt. 在输出自己的提示 ,它会从用户获取值之前输出换行符。

Why are you not actually using the prompt parameter to ask for something? 为什么你实际上没有使用 prompt参数来询问什么? It's obviously there for a reason :-) 这显然有一个原因:-)

That way, it would be output after the newline and your input would be on the same line. 这样,它将在换行符后输出,您的输入将在同一行。 In other words, something like changing: 换句话说,就像改变一样:

cout << "Celsius temperature for Day " << i+1 << " : ";
*(days + 1) = GetValidInt("", TEMP_MIN, TEMP_MAX);

into: 成:

#include <sstream>
:
std::stringstream ss;
ss << "Celsius temperature for Day " << (i+1) << " : ";
*(days + 1) = GetValidInt (ss.str(), TEMP_MIN, TEMP_MAX);

That should fix your immediate problem, the newline after the prompt. 这应该可以解决您的问题,即提示后的换行符。 You also have some other problems, such as passing wrong data types to functions but I'll leave them for you to fix since it'll make you a better programmer. 您还有一些其他问题,例如将错误的数据类型传递给函数,但我会留下它们供您修复,因为它会让您成为更好的程序员。 One hint, compile with warnings on, and read what the compiler tells you. 一个提示,编译警告,并阅读编译器告诉你的内容。

TRY and I do mean try, xcode doesn't flag any errors but don't have the time to run it and debug it, this (obviously its not complete, just cut&paste it on top of yours) 尝试,我的意思是尝试,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;
}

I didnt change anything else. 我没有改变任何其他东西。 I dont know if this is really what you need, i mean it gets the job done (probably) or at least it will with a few changes. 我不知道这是否真的是你需要的,我的意思是它完成了工作(可能),或者至少它会有一些变化。 But I dont know if you are supposed to be using anything in particular. 但我不知道你是否应该特别使用任何东西。

Main changes: 主要变化:

  • The outputFunc is now void and couts in it. outputFunc现在是空的并且在其中有couts。 You can either do this or have it return a std::string and output that. 你可以这样做或者让它返回一个std :: string并输出它。 But you cant have it return an int, and output inside it (well you CAN, but its not what you need here). 但你不能让它返回一个int,并输出它(你可以,但它不是你需要的)。
  • newDay is out of context in outputFunc, so you need to pass it. newDay在outputFunc中不在上下文中,因此您需要传递它。 But anyway its the counter you need to send. 但无论如何它是你需要发送的柜台。

  • calcCelcius (which i'd named deg2far or celc2far) now returns a double and takes an int (still inline) calcCelcius(我将其命名为deg2far或celc2far)现在返回一个double并取一个int(仍为内联)

  • Remember that you are storing values in i+1 (starting with 0) therefore your first value is in days[1] ... output loop should start at 1. 请记住,您将值存储在i + 1(从0开始),因此您的第一个值是以天为单位[1] ...输出循环应从1开始。

There might be some other changes (obviously the prototype for the funcs) but those are the major. 可能还有其他一些变化(显然是funcs的原型),但那些是主要的。

i'm not really sure if line 54 string prompt = "Celsius temperature for Day " + (i+1); 我不确定第54行string prompt = "Celsius temperature for Day " + (i+1); will work, maybe you need to declare it separately. 会工作,也许你需要单独声明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM