-4

I am attempting to run a code that takes a user inputted standard time and convert it to military time. For the user input, the hours can be typed as one or two digits, and AM/PM can be typed in any way. Given that, the code I have is as follows:

#include <iostream>
#include <string>

using namespace std;

string time, hour, minute, amPm, miltime;

char amPmCheck;

int main()
{

    cout<<"Enter time:\n";

    cin>>time;

    colon = time.find(':');

    space = time.find (' ');

    hour = time.substr(0, colon);

    minute = time.substr(colon + 1, space);

    amPm = time.substr(space, back);

    amPmCheck = amPm[0];

    timeConversion(hour, minute, amPmCheck);

    return 0;
}


void timeConversion(hour, minute, amPmCheck)
{
    if(amPmCheck == 'a'||'A')
    {
        if(int(hour) == 12)
        {
            hour.assign('00');
        }
        else if (int(hour) <= 9)
        {
            hour.insert(0, '0');
        }
    }
    else
        if(int(hour) < 12)
        {
            hour.assign(12+int(hour));
        }

    miltime = hour + minute

    cout<< "Corresponding military time is", miltime;
}

However when attempting to compile the code, I get the following errors from my chosen compiler:

main.cpp:50:25: warning: multi-character character constant [-Wmultichar]
             hour.assign('00');
                         ^~~~
main.cpp:15:8: error: 'std::string time' redeclared as different kind of symbol
 string time, hour, minute, amPm miltime;
        ^~~~
In file included from /usr/include/pthread.h:24:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr.h:148,
                 from /usr/include/c++/6/ext/atomicity.h:35,
                 from /usr/include/c++/6/bits/ios_base.h:39,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from main.cpp:10:
/usr/include/time.h:192:15: note: previous declaration 'time_t time(time_t*)'
 extern time_t time (time_t *__timer) __THROW;
               ^~~~
main.cpp:44:21: error: variable or field 'timeConversion' declared void
 void timeConversion(hour, minute, amPmCheck)
                     ^~~~
main.cpp:44:35: error: 'amPmCheck' was not declared in this scope
 void timeConversion(hour, minute, amPmCheck)
                                   ^~~~~~~~~

I am still relatively new to programming, so any information on what my mistakes are and what the simplest way to fix them is would be appreciated.

Cynderias
  • 5
  • 3
  • You need to put types for the parameters here; `void timeConversion(hour, minute, amPmCheck)` and move the whole function definition before `main()`. – πάντα ῥεῖ May 08 '19 at 12:28
  • 4
    You should get a [book](https://stackoverflow.com/a/388282). It will greatly speed up your learning progress. – nwp May 08 '19 at 12:30
  • `string time` rename `time` to something else. The name `time` is already taken. For example use `timestr`. – KamilCuk May 08 '19 at 12:31
  • Please learn the type system of C++. – Quimby May 08 '19 at 12:31
  • The compiler actually tells you exactly what is happening: `'std::string time' redeclared as different kind of symbol` followed by `previous declaration 'time_t time(time_t*)'` . So the identifier `time` was already used for some other purpose. For the future, please read compiler error messages VERY carefully. They used to be quite cryptic, bit the modern compilers got much better at it. –  May 08 '19 at 12:35

2 Answers2

0

There are few issues here, first:

  • change

void timeConversion(hour, minute, amPmCheck)

to

void timeConversion(string & hour, string & minute, string & amPmCheck)

if you want to deal with function parameters

  • or change the same function (in your case better option) to:

void timeConversion()

since you are dealing with global variables anyway.

  • put timeConversion function before main or use pre-declaration like this:

void timeConversion();

also before main function.

PS. Please read good book about C++.

Diodacus
  • 663
  • 3
  • 8
0

For starters there is no need to declare these variables as namespace variables.

string time, hour, minute, amPm, miltime;

char amPmCheck;

They should be declared as local variables if the function main.

And there are no declarations of variables colon and space.

This statement

amPm = time.substr(space, back);

does not make sense. The variable amPm is declared as having type char instead of the type std::string.

The function declaration

void timeConversion(hour, minute, amPmCheck)

is wrong. You did not specify the types of the function parameters.

Statements like this

    if(int(hour) == 12)

does not make sense. You have to use at least the standard function std::stoi to convert an object of type std::string to an object of type int.

In the statement

hour.assign('00');

there is used a multibyte char literal. It seems you mean double quotes

hour.assign("00");

The same is valid for the statement

hour.insert(0, '0');

This statement

hour.assign(12+int(hour));

also does not make sense.

There are too many errors in your code.

You should at first read the documentation of the class std::basic_string to know how to use its nethods.

And do not use the derictive

using namespace std;

It can be the reason of the error of conflicted declarations. See for example the error message of your compiler

main.cpp:15:8: error: 'std::string time' redeclared as different kind of symbol
 string time, hour, minute, amPm miltime;
        ^~~~
In file included from /usr/include/pthread.h:24:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr.h:148,
                 from /usr/include/c++/6/ext/atomicity.h:35,
                 from /usr/include/c++/6/bits/ios_base.h:39,
                 from /usr/include/c++/6/ios:42,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from main.cpp:10:
/usr/include/time.h:192:15: note: previous declaration 'time_t time(time_t*)'
 extern time_t time (time_t *__timer) __THROW;
               ^~~~

Use instead qualified names for standard names declared in the namespace std.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335