12-和24小时的时钟在C++和当我运行我的程序,并输入我的决定,它只是重复菜单任何建议?[关闭]

imzjd6km  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(97)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
昨天就关门了。
Improve this question
下面是我的12小时制和24小时制时钟的代码。这些时钟不必是实时的,也不必是滴答的。我需要它们来打印随机时间,并能够与可能希望添加一小时一分钟或一秒的用户进行交互。选项1应显示12小时制格式选项2应显示24小时制格式。小时格式选项3应允许用户添加小时选项4应允许用户添加分钟选项5应允许用户添加秒

#include <iostream>
#include <string>

using namespace std;
// Functions to convert a number to a two-digit string 
string twoDigitString(unsigned int n) {
    if (n < 10) {
        return "0" + to_string(n);
    }
    else {
        return to_string(n);
    }
}

string nCharString(size_t n, char c) {
    if (n >= 0) {
        string result;
        for (size_t i = 0; i < n; ++i) {
            result += c;
        }
        return result;
    }
    else {
        return "";
    }
}
// Function to display time in a 12 hour format 
string formatTime12(unsigned int h, unsigned int m, unsigned int s) {
    string period = "AM"; // default period 

    if (h == 0) {
        h = 12;
    }

    else if (h >= 12) {
        period = "PM";

        if (h > 12) {
            h -= 12;
        }
    }

    string formattedTime = twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s) + " " + period;
    
    return formattedTime;
}

// Function to display time in a 24 hour format 
string formatTime24(unsigned int h, unsigned int m, unsigned int s){ 
    string period = "AM";

    if (h == 0) {
        h = 24;
    }

    else if (h >= 12) {
        period = "PM";

    }
    string formattedTime = twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s) + " " + period;

    return formattedTime;
}

int main() {
    unsigned int hours = 12;
    unsigned int minutes = 0;
    unsigned int seconds = 0;

    int choice;

    do {
        //display menu for time format 
        cout << "1. 12- hour format" << endl;
        cout << "2. 24- hour format" << endl;
        cout << "3. Add Hour" << endl;
        cout << "4. Add Minute" << endl;
        cout << "5. Add second" << endl;
        cout << "6. Exit" << endl;

        cin >> choice;

        switch (choice) {
        case 1:
            cout << "12- hour format" << formatTime12(hours, minutes, seconds) << endl;
            break;
        case 2:
            cout << "24- hour format" << formatTime24(hours, minutes, seconds) << endl;
            break;
        case 3:
            hours = (hours + 1) % 24;
            break;
        case 4:
            minutes = (minutes + 1) % 60;
            break;
        case 5:
            seconds = (seconds + 1) % 60;
            break;
        case 6:
            cout << "Exiting" << endl;
            break;

        default:
            cout << "Invaild input, please try again." << endl;

        }
    } while (choice != 0);
    

    return 0;
}

字符串
enter image description here
一开始,我没有使用while循环,当我运行代码时,它会提示菜单,但当我做出选择时,它想退出,然后我添加了while循环,现在当我做出决定时,它只是重复上面所示的菜单。

xfyts7mz

xfyts7mz1#

while语句中的表达式更改为:
while ((choice >= 1) && (choice <= 5));
您可能希望在终止菜单后暂停执行:

std::cout << "Paused. Press ENTER to continue.\n";
std::cin.ignore(100000, '\n');

字符串

m3eecexj

m3eecexj2#

问题是choice != 0

当用户选择退出选项(选项6)时,菜单不会退出。
我添加了while循环,现在当我决定[退出]时,它只是重复菜单,如下所示。
这是你写的while循环。

do {
        // ...
    } while (choice != 0);

字符串
退出的唯一方法是将choice设置为0,但这并不好,因为您希望在choice6时退出。
所以,把测试改为6!就这样。

do {
        // ...
    } while (choice != 6);


现在,当您输入6时,程序正确退出,但当您做出无效选择时,例如choice 0,它会继续循环。

1. 12- hour format
2. 24- hour format
3. Add Hour
4. Add Minute
5. Add second
6. Exit
0
Invaild input, please try again.
1. 12- hour format
2. 24- hour format
3. Add Hour
4. Add Minute
5. Add second
6. Exit
6
Exiting


很接近,但没有雪茄
你也可以尝试

do {
        // ...
    } while ((choice >= 1) && (choice <= 5));


然而,这并不好。当用户选择6时,它会正确退出,但当用户做出无效选择时,它也会退出。因此,07等也被认为是“退出”。
在这次运行中,我选择了0,程序退出。菜单没有重新显示。

1. 12- hour format
2. 24- hour format
3. Add Hour
4. Add Minute
5. Add second
6. Exit
0
Invaild input, please try again.

几个24小时格式的bug

一个错误是在24小时格式中添加了“AM”或“PM”,因此,将其从函数formatTime24中删除。
另一个更微妙,与使用“24:00:00”表示午夜有关。这可能是一个bug,但也可能不是。
以下是维基百科24小时格式的部分描述。
一天中的时间以24小时表示法书写,格式为hh:mm(例如01:23)或hh:mm:ss,其中hh(00到23)是自午夜以来经过的完整小时数,mm(00到59)是自上一个完整小时以来经过的完整分钟数,ss(00到59)是自上一个完整分钟以来经过的秒数。
根据维基百科,24:00:00可以用来表示午夜,但它与00:00:00的意思不同。
在24小时制中,一天从午夜00:00或0:00开始,一天的最后一分钟从23:59开始。在方便的情况下,24:00也可以用来指给定日期结束时的午夜[3] -也就是说,一天的24:00与第二天的00:00是相同的时间。
总的来说,最好使用“00:00:00”表示午夜。如果你这样做,那么formatTime24简化为这样。

string formatTime24(unsigned int h, unsigned int m, unsigned int s) {
    return twoDigitString(h) + ":" + twoDigitString(m) + ":" + twoDigitString(s);
}

捕获非数字输入

我通常使用类似function get_int的东西来从键盘获取整数。它捕获错误的输入,并循环直到用户输入一个有效的整数。
你可以在程序中通过检查cin >> choice是否成功来完成类似的操作。在下面的代码片段中,if (std::cin >> choice)...表示“if(cin >> choice)didn 't fail.”当你输入非数字时,例如“What?”或“x1”,就会发生失败。
用这个替换cin >> choice;

for(;;)
    {
        std::cout << "\nChoice? ";
        if (std::cin >> choice)
            break;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Invaild input, please try again.\n";
    }


现在,当你不小心按错键时,你的程序不会在无限循环中运行。

1. 12- hour format
2. 24- hour format
3. Add Hour
4. Add Minute
5. Add second
6. Exit

Choice? Whoops!
Invaild input, please try again.

Choice? 6
Exiting

相关问题