该代码不会显示任何输入的数字。如何使它显示任何输入?

时间:2019-04-13 18:12:54

标签: c++ arrays

我正在c中使用day,hour和minutes创建3d数组,用户可以在其中输入任何数字,并且它将显示为day:hour:minute,问题是代码将不会显示任何给定的数字。

我尝试将for循环中的天,小时和分钟设置为1,并且可以,但是只有输入1时,其他任何数字都不会运行

int main()
{
    float temp[365][24][23] = { 0.0 };// this sets the array to have no more then 365 days-change temp to days
    char stop = 'n';
    int hour = 1;
    int min = 1;
    int dayz = 1; // will change i to temp
    do
    {
        std::cout << "please give me a day from 1-365" << std::endl;//tells users that 1-365 days are needed
        std::cin >> dayz;

        std::cout << "plese give me a number from 1-24 hours  " << std::endl;//tells users that 1-365 days are neede
        std::cin >> hour;

        std::cout << "please give me the minute" << std::endl;//tells users that 1-365 days are needed
        std::cin >> min;

        std::cout << "enter the temperature for today" << " = "; //prompt users to keep on giving temperatures
        std::cin >> temp[dayz][hour][min];

        std::cout << "If you want to end the program type N.If you dont then type any number" << std::endl; // tells users after date and temperatures are entered if the they want to stop type 0 and if not press anynumber
        std::cin >> stop;
    } while (stop != 'N' && stop != 'n');

    for (dayz = 0; dayz <=364; ++dayz)// makes the calendar and keeps it less then 366 issue is that it will show the same date multiple time
    {
        for (hour = 0; hour <=23; ++hour)// keeps the calendar orderly      
        {
            for (min = 0; min <= 22; ++min)
            {
                if (temp[dayz][hour][min] != 0.0)
                {

                    std::cout << dayz << ":" << hour << ":" << min << " = " << temp[dayz][hour][min] << "/t "; //will display all the input
                }
            }
            return 0;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您在内部循环之后写了return 0;,我想这不是您想要的。并且您应该使用std::string stop = "n";并比较stop != "N" && stop != "n"

答案 1 :(得分:0)

因此,我找到了一种解决方法,只需摆脱for循环,然后将for循环的min转换为if语句。例如。

int main()
{
    float temp[364][23][59] = { 0.0 };//-1073741571 i get this error when i put minute in 59
    char stop = 'n';
    int hour = 1;
    int min = 1;
    int dayz = 1; // when i dont get the error code the program doesnt show
    do
    {
        std::cout << "please give me a day from 1-365" << std::endl;//tells users that 1-365 days are needed
        std::cin >> dayz;

        std::cout << "plese give me a number from 1-24 hours  " << std::endl;//tells users that 1-365 days are neede
        std::cin >> hour;

        std::cout << "please give me the minute" << std::endl;//tells users that 1-365 days are needed
        std::cin >> min;

        std::cout << "enter the temperature for today" << " = "; //prompt users to keep on giving temperatures
        std::cin >> temp[dayz][hour][min];

        std::cout << "If you want to end the program type N.If you dont then type any number" << std::endl; // tells users after date and temperatures are entered if the they want to stop type 0 and if not press anynumber
        std::cin >> stop;
    } while (stop != 'N' && stop != 'n');

        if  (min < 61 )
            {

                    std::cout << dayz << ":" << hour << ":" << min << " = " << temp[dayz][hour][min] << "/t "; //will display all the input
                    return 0;

            }
}

答案 2 :(得分:0)

尝试一下。

int main(){
    float temp[365][24][23] = { 0.0 };// this sets the array to have no more then 365 days-change temp to days
    char stop = 'n';
    int hour = 1;
    int min = 1;
    int dayz = 1; // will change i to temp
    do{
        std::cout << "please give me a day from 1-365" << std::endl;//tells users that 1-365 days are needed
        std::cin >> dayz;

        std::cout << "plese give me a number from 1-24 hours  " << std::endl;//tells users that 1-365 days are neede
        std::cin >> hour;

        std::cout << "please give me the minute" << std::endl;//tells users that 1-365 days are needed
        std::cin >> min;

        std::cout << "enter the temperature for today" << " = "; //prompt users to keep on giving temperatures
        std::cin >> temp[dayz][hour][min];

        std::cout << "If you want to end the program type N.If you dont then type any number" << std::endl; // tells users after date and temperatures are entered if the they want to stop type 0 and if not press anynumber
        std::cin >> stop;
    } while (stop != 'N' && stop != 'n');

    for (dayz = 0; dayz <=364; ++dayz)// makes the calendar and keeps it less then 366 issue is that it will show the same date multiple time
    {
        for (hour = 0; hour <=23; ++hour)// keeps the calendar orderly      
        {
            for (min = 0; min <= 22; ++min){
                if (temp[dayz][hour][min] != 0.0)
                    std::cout << dayz << ":" << hour << ":" << min << " = " << temp[dayz][hour][min] << "/t "; //will display all the input
            }
        }
    }
     return 0;
}