Leading zeros not working in an output file

So I'm writing a program that involves reading military time (hh:mm:ss) and I can't for the life of me figure out why I can't get the leading zeros to work when it sends to an output file. The code SHOULD work because when I use regular "cout" and print to the screen, it prints the leading zeros.

1
2
  //input file
05:24:05 07:04:09


Will give me

1
2
//output file
5:24:5 7:4:9


But using regular cout works fine.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int MAX = 50;

void convertCase(string& name, ofstream& out);
int convertHours(int hh);
int convertMinutes(int mm);

int main()
{
	string filename, filenameout;
	string fname;
	string start;
	string end;
	ifstream in;
	ofstream out;
	int totalSec[MAX];
	int hh, hh2, mm, mm2, ss, ss2, seconds, seconds2;
	int i = 0;
	
	cout << "Enter name of input file" << endl;
	cin >> filename;
	in.open("input.txt");

	cout << "Enter name of output file" << endl;
	cin >> filenameout;
	out.open("output.txt");
	
	in >> fname;
	while (in)
	{ 
		convertCase(fname, out);
		
		in >> hh;
		in.ignore(100, ':');
		in >> mm;
		in.ignore(100, ':');
		in >> ss;
		in >> hh2;
		in.ignore(100, ':');
		in >> mm2;
		in.ignore(100, ':');
		in >> ss2;
		seconds = convertHours(hh) + convertMinutes(mm) + ss;
		seconds2 = convertHours(hh2) + convertMinutes(mm2) + ss2;
		totalSec[i+0] = seconds2 - seconds;
		out << setw(2) << setfill('0') << hh << ":" 
		<< setw(2) << setfill('0') << mm << ":" << setw(2) << setfill('0') << ss << "   ";
		out << setw(2) << setfill('0') << hh2 << ":" 
		<< setw(2) << setfill('0') << mm2 << ":" << setw(2) << setfill('0') << ss2 << "   ";
		out << setfill(' ');
		out << totalSec[i + 0] << endl;
		
		in >> fname;
		i++;
	}
	
	in.close();
	out.close();

	return 0;
}
void convertCase(string& name, ofstream& out)
{
	name[0] = toupper(name[0]);
	for (int i = 1; i < name.length(); i++)
	{
		name[i] = tolower(name[i]);
	}
	out << left << setw(15) << name << " ";
}
int convertHours(int hh)
{
	return hh * 3600;
}
int convertMinutes(int mm)
{
	return mm * 60;
}
Last edited on
Nevermind, found the error. It was set to left justified from one of the functions. So I just needed to make it right justified.
Topic archived. No new replies allowed.