#Basic Syntax

11 messages · Page 1 of 1 (latest)

next hazel
#

I'm new to programming in C++ and I'm trying to solve the image problem however I don't know what I'm doing wrong
´´´#include <iostream>
using namespace std;

int main(void)
{
int hours, minutes,datetime;
hours=15;
minutes=35;
datetime=" "
if(hours==0)
{
hours=12;
}
if(hours>12)
{
hours=hours-12;
}
if(hours==12)
{
hours=12;
}
if(hours<12)
{
datetime=AM;
}
else if(hours>=12)
{
datetime=PM
}
cout<< hours<<":"<<minutes<<datetime;

return 0;
}´´´

frank yarrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

mossy fjord
#
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int hours = 15, minutes = 35;
    std::string datetime;
    
    if(hours==0)
    {
        hours=12;
    }
    if(hours>12)
    {
      hours=hours-12;
    }
    if(hours==12)
    {
        hours=12;
    }
    if(hours<12)
    {
        datetime = "AM";
    }
    else if(hours>=12)
    {
        datetime = "PM";
    }
   cout<< hours<<":"<<minutes<<datetime;
    
    return 0;
}```
#

first of all you don't need to put any parameters in the main() function

#

Second of all you have to declare the type you want when making a variable

#

for datetime you should use string

#

which is in the <string> header

#

other than that it works

mossy fjord
next hazel
#

Thank you!

#

!solved