C++程序未运行

C++ program is not running

本文关键字:运行 程序 C++      更新时间:2023-10-16

我是C++的新手,今天我编写了以下程序:

#include <iostream>
using namespace std;
typedef struct item
{
int hour;
int minute;
int second;
void print()
{
cout << hour << ":" << minute << ":" << second << endl;
}
}time;
time nirmul_time(time a);
time* timing_array(int size);
void print_times(time* array, int size);
int main()
{
time* our_array;
int size;
cout << "Give me the size of the array: " << endl;
cin >> size;
our_array = timing_array(size);
print_times(our_array, size);
delete[]our_array;
}
time nirmul_time(time a)
{
a.minute = a.minute + (a.second / 60);
a.second = a.second % 60;
a.hour = a.hour + (a.minute / 60);
a.minute = a.minute % 60;
a.hour = a.hour % 24;
return a;
}
time* timing_array(int size)
{
int i = 0;
bool overflow = false;
time* array;
array = new time[size];
for (i; i < size; i++)
{
cout << "enter the hours: " << endl;
cin >> array[i].hour;
if (array[i].hour > 23)
overflow = true;
cout << "enter the minutes: " << endl;
cin >> array[i].minute;
if (array[i].minute > 59)
overflow = true;
cout << "enter the seconds: " << endl;
cin >> array[i].second;
if (array[i].second > 59)
overflow = true;
if (overflow)
{
nirmul_time(array[i]);
}
overflow = false;
}
return array;
}
void print_times(time* array, int size)
{
int i = 0;
for (i; i < size; i++)
{
array[i].print();
}
}

当我尝试通过ctrl+F9或ctrl+F5运行它时,程序没有运行,我得到以下错误:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>Source.cpp
1>Project1Project1Source.cpp(1,1): warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format
1>Project1Project1Source.cpp(1,10): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>Project1Project1Source.cpp(2,1): error C2447: '{': missing function header (old-style formal list?)
1>Project1Project1Source.cpp(6,504): error C2065: 'time': undeclared identifier
1>Project1Project1Source.cpp(6,510): error C2065: 'array': undeclared identifier
1>Project1Project1Source.cpp(6,521): error C2062: type 'int' unexpected
1>Project1Project1Source.cpp(6,527): error C2143: syntax error: missing ';' before '{'
1>Project1Project1Source.cpp(6,527): error C2447: '{': missing function header (old-style formal list?)
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

此外,当我尝试通过调试器ctrl+F5运行时,我会收到一条错误消息:在此处输入图像描述

我试着重新打开这个项目,并在online shell中运行它,但仍然不起作用,有人能帮我检测问题吗?

您试图赋予结构time的名称已被标准库使用。

你应该给它取另一个名字。