0

What i Hope to accomplish

So i have a class with date and time and i am using a struct in which year, month, date are integers I want when the user enters a number for example 3 it must output 03 but must be in an int format without using the IOMANIP library or fmt library as when i use these library i get 'error time does not have a type'

Please Help How can i get a 0 infront of months and days to be stored in a variable whilst still being an int;

I have tried the setw But does not work as mentioned above

So what i have tried was to store it in a pointer and vector

I have a class called vehicle with struct date and time

struct time
{
    int hh;
    int mm;
    int ss;
    char col1;
    char col2;
};
struct date
{
  int day;
  int month;
  int year;
  char sym1;
  char sym2;
};

class vehicle
{
    string pltno;
    int type;
    date dt;
    time arrive;
    time departure;

Code I have

vector<vehicle> vehleft(100);
int static totalvehicle=0,totalcar=0,totalbike=0,totalamt=0,i=0,z=0;

fstream file;

void vehicle::addVehicle()
{

      vehicle *v = new vehicle;
      cin.ignore();
      cout<<"Enter vehicle number : ";
      std::getline(cin, v->pltno);
      cout<<"Enter arrival time in hours minutes and seconds : ";
      cin>>v->arrive.hh>>v->arrive.col1>>v->arrive.mm>>v->arrive.col2>>v->arrive.ss;
      cout<<"Enter date in day month and year: ";
      cin>>v->dt.day>>v->dt.sym1>>v->dt.month>>v->dt.sym2>>v->dt.year;

What i tried to do was use the setw and setfill to add a 0 infront but when i do so i get an error that 'time is not a type' this happens whenever i include the ctime library or Iomanip library.

Basically I want v->dt.day and v->dt.month to assign the user input as of this moment if the number is 3 it will stay as 3 and not 03 which is the format i want

Update

When I input 10 for example in month when it is outputed it says 0 instead of 10?

  • 1
    Try these: `printf("0x%08x", 0x45);` or `std::cout << "0x" << hex << 25 << "\n";` Also try `setw(4) << setfill('0')` – Thomas Matthews May 01 '22 at 17:01
  • 1
    When using `std::setw(2)`, don't forget to specify [`std::setfill('0')`](https://en.cppreference.com/w/cpp/io/manip/setfill) as well. Please provide a [mre] so we can understand why you get that error message. – heap underrun May 01 '22 at 17:06
  • 2
    That error you say you get sounds more like a compile error, like you need to forward declare the time type. You may need to get a https://stackoverflow.com/help/minimal-reproducible-example – Niall May 01 '22 at 17:06
  • @heapunderrun When i Use Setw I have an issue because when i use the Library i get time does not have a type – Users_DVdsa123 May 01 '22 at 17:51
  • Divide-and-conquer. The description *"the user enters a number for example **3** it must output **03** but must be in an int format"* covers at least two major steps: 1) the user enters a number that gets stored in an `int`, and 2) the `int` is output. If the value is being stored as `3`, then you can skip the user input step. Try to come up with a [mre] to illustrate your issue ("illustrate" meaning that the textual description is still primary) . This example might be as simple as `int main() { int month = 3; /* output */ }` where you replace the comment with your current attempt at output. – JaMiT May 01 '22 at 18:00
  • Hmm... without seeing your code, this is only a guess, but you would probably be better served asking about the error you get when you use the `` header, than by asking for a way to accomplish something without using the header. – JaMiT May 01 '22 at 18:03
  • @JaMiT I have tried to improve upon my question Please have a look if u don't mind – Users_DVdsa123 May 01 '22 at 18:09
  • @Users_DVdsa123 What does the definition of `time` look like? `time` is a very unfortunate type name since it's also the name of a function in the standard library. Please show a [mre] – Ted Lyngmo May 01 '22 at 18:14
  • @TedLyngmo Let me update the question with time struct as well – Users_DVdsa123 May 01 '22 at 18:15
  • 1
    Ok, please add the code where you've tried to use the I/O manipulators too. Btw, is there a reason why you are not using `` `time_point`s for all of this? – Ted Lyngmo May 01 '22 at 18:17
  • @TedLyngmo I have added some more code – Users_DVdsa123 May 01 '22 at 18:23
  • @Users_DVdsa123 Yes, but none of it tries to use `setw` and `setfill` that you have a problem with. Have you tried renaming your `time` type to something else too? As I said, it's a bad name since the standard library contains a function with that name. Try to provide a [mre]. That is, a short program that you would expect to be able to compile. – Ted Lyngmo May 01 '22 at 18:25
  • 2
    @TedLyngmo THANK YOU SOO MUCH i just changed the time name to something random like lime and included the library it worked really appreciated it – Users_DVdsa123 May 01 '22 at 18:30
  • 1
    Another entry in the [what's wrong with "using namespace std"](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) files – Nathan Pierson May 01 '22 at 20:11

1 Answers1

1

setw works in conjunction with setfill so something like

std::cout << std::setw(2) << std::setfill('0') << day; 

might work for you. See also https://en.cppreference.com/w/cpp/io/manip/setfill

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67