该代码不会在其中显示任何数字输入.如何使它显示任何输入

the code will not display any number input into it. how to make it so it will display whatever is the input?

本文关键字:任何 显示 何使它 输入 数字输入 代码 在其中      更新时间:2023-10-16

>我正在使用天,小时和分钟在c ++中做一个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;
        }
    }
}

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

试试这个。

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;
}

所以,我找到了一种方法来修复它,只需摆脱 for 循环并将 min 的 for 循环转换为 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;
            }
}