-1

I'm new to C++ and i wrote today the following program:

#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();
    }
}

when i try to run it throught ctrl+F9 or ctrl+F5 the program is not running, and i get the following error:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>Source.cpp
1>Project1\Project1\Source.cpp(1,1): warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format
1>Project1\Project1\Source.cpp(1,10): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>Project1\Project1\Source.cpp(2,1): error C2447: '{': missing function header (old-style formal list?)
1>Project1\Project1\Source.cpp(6,504): error C2065: 'time': undeclared identifier
1>Project1\Project1\Source.cpp(6,510): error C2065: 'array': undeclared identifier
1>Project1\Project1\Source.cpp(6,521): error C2062: type 'int' unexpected
1>Project1\Project1\Source.cpp(6,527): error C2143: syntax error: missing ';' before '{'
1>Project1\Project1\Source.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 ==========

also when i try to run in through debugger ctrl+F5 i get an error message: enter image description here

i tried to reopen the project, and to run it in online shell but still it does not work, can somebody help me detect the problem?

bulba421
  • 1
  • 1
  • 3
    `warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format` Did you try it? – Thomas Sablik Jul 10 '20 at 15:14
  • `notepad++` will help you fix the file format problem or `unix2dos` – drescherjm Jul 10 '20 at 15:14
  • Even the Macintosh hasn't used "Mac file format" for two decades. – Eljay Jul 10 '20 at 15:15
  • 2
    The next problem is the combination of `using namespace std;` and `time`. You are redeclaring [std::time](https://en.cppreference.com/w/cpp/chrono/c/time) – Thomas Sablik Jul 10 '20 at 15:16
  • 3
    `typedef struct item` is not needed in `c++` just use `struct item` – drescherjm Jul 10 '20 at 15:16
  • 3
    And `array` can clash with `std::array` because of that `using namespace std;` as well. – Jesper Juhl Jul 10 '20 at 15:17
  • 4
    Related: [https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – drescherjm Jul 10 '20 at 15:18
  • ***i tried to reopen the project, and to run it in online shell but still it does not work, can somebody help me detect the problem?*** When you get compiler errors you need to fix them. No amount of restarting the project will fix the issue. The compiler told you several things that were wrong with your code. However the main issue happened to be you were using a bad practice of `using namespace std;` and naming two of your types the same as ones that are in the standard library causing a confilct. – drescherjm Jul 10 '20 at 17:44
  • A program will run better after it *compiles* with no errors. – Thomas Matthews Jul 10 '20 at 18:05
  • My advice is to solve the errors starting with the first error. – drescherjm Jul 10 '20 at 19:14
  • I changed the format to unix with notepad++ and it fixed the problem, thank you all for the other comments aswell! – bulba421 Jul 10 '20 at 19:44

1 Answers1

-1

The name you tried to give to the structure time is already taken by the standard library.

You should give it another name.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Hello, thank you for the comment. I tried to change the name of the struct but the problem still exist. – bulba421 Jul 10 '20 at 18:43
  • @DanAverin you have to fix the other bugs that the compiler is telling you about. You will not be able to execute your program while the number of errors is greater than 0. And even then you may have other logical errors not detected by the compiler. – drescherjm Jul 10 '20 at 18:54