我认为我过得太快了,我还在第一道菜,而且还很早。我在这个网站上搜索和搜索,但即使是类似问题的答案也没有立即点击。
我想要发生的一切就是保持我的用户输入变量并将它们传递给他们计算然后打印,甚至把它写出来的声音如此基本但是我被卡住....
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function Prototypes
void getTime12();
void getTime24();
void convert24to12();
void convert12to24();
void print24();
void print12();
void menu();
int main()
{
int choice;
do
{
// Display menu options
menu();
cin >> choice;
if (choice == '1')
{
getTime12();
convert12to24();
print24();
}
else if (choice == '2')
{
getTime24();
convert24to12();
print12();
}
} while (choice != '99');
system("pause");
return 0;
}
void menu()
{
//Declare variabls to return
cout << endl;
cout << "1. To convert time from 12-hour notation to 24-hour notation." << endl;
cout << "2. To convert time from 24-hour notation to 12-hour notation." << endl;
cout << "99. To quit the program." << endl << endl;
cout << "Your choice (1, 2, 99): ";
cout << endl << endl;
}
//function to get the hours
void getTime12()
{
static int get12Hrs, get12Mins, get12Secs;
static string AMPM = "AM";
//ask for the hour using cin
cout << "Enter the hours: ";
cin >> get12Hrs;
cout << endl;
cout << "Enter minutes: ";
cin >> get12Mins;
cout << endl;
cout << "Enter seconds: ";
cin >> get12Secs;
cout << endl;
//ask user for AM PM input
cout << "Enter AM or PM: ";
cin >> AMPM;
//For PM
if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
{
AMPM = "PM";
}
}
void getTime24()
{
static int get24Hrs, get24Mins, get24Secs;
static string AMPM = "AM";
//ask for the hour using cin
cout << "Enter the hours: ";
cin >> get24Hrs;
cout << endl;
cout << "Enter minutes: ";
cin >> get24Mins;
cout << endl;
cout << "Enter seconds: ";
cin >> get24Secs;
cout << endl;
//ask user for AM PM input
cout << "Enter AM or PM: ";
cin >> AMPM;
//For PM
if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
{
AMPM = "PM";
}
}
//Function to change to 24-hour notation
void convert24to12()
{
if (get24Hrs < 12)
{
get24Hrs = get24Hrs;
get24Mins = get24Mins;
get24Secs = get24Secs;
}
if (AMPM == "PM")
{
get24Hrs = get24Hrs - 12;
get24Mins = get24Mins;
get24Secs = get24Secs;
}
}
//Function to change to 12-hour notation
void convert12to24()
{
if (get12Hrs > 12)
{
get12Hrs = get12Hrs;
get12Mins = get12Mins;
get12Secs = get12Secs;
}
if (AMPM == "PM")
{
get12Hrs = get12Hrs + 12;
get12Mins = get12Mins;
get12Secs = get12Secs;
}
}
void print12()
{
cout << "The 12-hour time is " << get24Hrs << ":";
cout.width(2);
cout.fill('0');
cout << get24Mins << " " << get24Secs;
cout << AMPM << endl;
cout << endl << endl;
}
void print24()
{
cout << "The 24-hour time is " << get12Hrs << ":";
cout.width(2);
cout.fill('0');
cout << get12Mins << " " << get12Secs;
cout << AMPM << endl;
cout << endl << endl;
}
答案 0 :(得分:0)
您的代码中存在很多问题,因此我会尽力而为:
在main()
中,choice
是int
,但您要检查它是character
1还是2还是99.这需要ASCI CODE
值那些等于(我认为)49,50,...等的字符。如果你想检查整数值,只需删除括号。
此外,您需要在main()
中声明所有变量,然后在每次函数调用时传递所需的变量。
还要尝试找到更好的命名,这样事情就不会让人感到困惑。 (见下文)
尝试执行std::cout
中的所有main()
语句以及函数中的计算。只是为了更好的练习。
这段代码是您的代码但是编译,您需要自己完成剩下的工作,如果需要我会帮忙。此代码按值传递变量,按引用传递。通过在Visual Studio中使用F-10和F-11逐步/步入代码,确保逻辑正确。将鼠标悬停在每个语句的所有变量上,以查看流是否符合您的要求。祝你好运!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function Prototypes
void getTime12(string, int,int, int);
void getTime24(string, int, int, int);
void convert12to24(string, int, int, int);
void convert24to12(string, int, int, int);
void print24(string, int, int, int);
void print12(string, int, int, int);
void menu();
int main()
{
static string AMPM = "AM";
int get12Hrs = 0, get12Mins = 0, get12Secs = 0;
int get24Hrs = 0, get24Mins = 0, get24Secs = 0;
int choice;
do
{
// Display menu options
menu();
cin >> choice;
if (choice == 1)
{
getTime12(AMPM, get12Hrs, get12Mins, get12Secs);
convert12to24(AMPM, get12Hrs, get12Mins, get12Secs);
print24(AMPM, get12Hrs, get12Mins, get12Secs);
}
else if (choice == 2)
{
getTime24(AMPM, get24Hrs, get24Mins, get24Secs);
convert24to12(AMPM, get24Hrs, get24Mins, get24Secs);
print12(AMPM, get24Hrs, get24Mins, get24Secs);
}
} while (choice != 99);
system("pause");
return 0;
}
void menu()
{
//Declare variabls to return
cout << endl;
cout << "1. To convert time from 12-hour notation to 24-hour notation." << endl;
cout << "2. To convert time from 24-hour notation to 12-hour notation." << endl;
cout << "99. To quit the program." << endl << endl;
cout << "Your choice (1, 2, 99): ";
cout << endl << endl;
}
//function to get the hours
void getTime12(string AMPM, int get12Hrs, int get12Mins, int get12Secs)
{
//ask for the hour using cin
cout << "Enter the hours: ";
cin >> get12Hrs;
cout << endl;
cout << "Enter minutes: ";
cin >> get12Mins;
cout << endl;
cout << "Enter seconds: ";
cin >> get12Secs;
cout << endl;
//ask user for AM PM input
cout << "Enter AM or PM: ";
cin >> AMPM;
//For PM
if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
{
AMPM = "PM";
}
}
void getTime24(string AMPM, int get24Hrs, int get24Mins, int get24Secs)
{
//ask for the hour using cin
cout << "Enter the hours: ";
cin >> get24Hrs;
cout << endl;
cout << "Enter minutes: ";
cin >> get24Mins;
cout << endl;
cout << "Enter seconds: ";
cin >> get24Secs;
cout << endl;
//ask user for AM PM input
cout << "Enter AM or PM: ";
cin >> AMPM;
//For PM
if (AMPM == "PM" || AMPM == "Pm" || AMPM == "pm")
{
AMPM = "PM";
}
}
//Function to change to 24-hour notation
void convert24to12(string AMPM, int get24Hrs, int get24Mins, int get24Secs)
{
if (get24Hrs < 12)
{
get24Hrs = get24Hrs;
get24Mins = get24Mins;
get24Secs = get24Secs;
}
if (AMPM == "PM")
{
get24Hrs = get24Hrs - 12;
get24Mins = get24Mins;
get24Secs = get24Secs;
}
}
//Function to change to 12-hour notation
void convert12to24(string AMPM, int get12Hrs, int get12Mins, int get12Secs)
{
if (get12Hrs > 12)
{
get12Hrs = get12Hrs;
get12Mins = get12Mins;
get12Secs = get12Secs;
}
if (AMPM == "PM")
{
get12Hrs = get12Hrs + 12;
get12Mins = get12Mins;
get12Secs = get12Secs;
}
}
void print12(string AMPM, int get24Hrs, int get24Mins, int get24Secs)
{
cout << "The 12-hour time is " << get24Hrs << ":";
cout.width(2);
cout.fill('0');
cout << get24Mins << " " << get24Secs;
cout << AMPM << endl;
cout << endl << endl;
}
void print24(string AMPM, int get12Hrs, int get12Mins, int get12Secs)
{
cout << "The 24-hour time is " << get12Hrs << ":";
cout.width(2);
cout.fill('0');
cout << get12Mins << " " << get12Secs;
cout << AMPM << endl;
cout << endl << endl;
}