从24小时格式转换为12小时格式



我正在做一个简单的转换程序,遇到了很多我不太理解的错误。这是我迄今为止完成的代码。我使用visual Studio 2019,这些都是我收到的错误消息。

error C2062: type 'char' unexpected
error C2182: 'input': illegal use of type 'void'
error C2440: 'initializing': cannot convert from 'initializer list' to 'int'
message : The initializer contains too many elements
error C2065: 'AMPM': undeclared identifier
error C2182: 'convert': illegal use of type 'void'
error C2440: 'initializing': cannot convert from 'initializer list' to 'int'
message : The initializer contains too many elements
1> error C2065: 'AMPM': undeclared identifier
: error C2182: 'output': illegal use of type 'void'
1>C:UsershpsourcereposProject5Project5Source.cpp(17,31): error C2440: 'initializing': cannot convert from 'initializer list' to 'int'
: message : The initializer contains too many elements
: error C2065: 'choice': undeclared identifier
: error C2065: 'choice': undeclared identifier
: error C2065: 'choice': undeclared identifier
1>Done building project "Project5.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <iostream>
#include <cmath>
using namespace std;
void input(int& hours24, int& minutes);
void convert(int& hours, char& AMPM);
void output(int hours, int minutes, char AMPM);
int main() {
cout << "-----------------------------Convert from 24-hour format to 12 hour format--------------------n";
int hour, min,
char choice, AMPM;
do {
void input(hour, min);
void convert(hour, min, AMPM);
void output(hour, min, AMPM);
cout << "/n";
cout << "To complete press y or Y: "; cin >> choice;
} while (choice == 'Y' || choice == 'y');
}
void input(int& hours24, int& minutes) {
cout << "Enter your time in hours and minutes"; cin >> hours24; cin >> minutes;
}
void convert(int& hours, char& type) {
if (hours > 12) {
hours -= 12;
type = 'P';
}
else {
type = 'A';
}
}
void output(int hours, int minutes, char type) {
cout << "In 12 hour notation: " << hours << ":" << minutes << " " << type << "";
}
#include <iostream>
#include <cmath>

using namespace std;
void input(int& hours24, int& minutes);
void convert(int& hours, char& AMPM);
void output(int hours, int minutes, char AMPM);
int main() {
cout << "-----------------------------Convert from 24-hour format to 12 hour format--------------------n";
int hour, min; // different types. should add simicolon here.
char choice, AMPM;
do {
// just write the name of the functions without their return type.

input(hour, min);
// don't pass "min" here.
convert(hour, AMPM);
output(hour, min, AMPM);
cout << "/n";
cout << "To complete press y or Y: "; cin >> choice;

} while (choice == 'Y' || choice == 'y');
}

void input(int& hours24, int& minutes) {
cout << "Enter your time in hours and minutes"; cin >> hours24; cin >> minutes;
}
void convert(int& hours, char& type) {
if (hours > 12) {
hours -= 12;
type = 'P';
}
else {
type = 'A';
}
}
void output(int hours, int minutes, char type) {
cout << "In 12 hour notation: " << hours << ":" << minutes << " " << type << "";
}

以下是修复的代码,每个修复上面都有一个注释。

void input(hour, min);
void convert(hour, min, AMPM);
void output(hour, min, AMPM);

这不是你调用函数的方式,试试这个

input(hour, min);
convert(hour, min, AMPM);
output(hour, min, AMPM);

然后

cout << "/n";

换行符是n,就像这个

cout << "n";

最新更新