Converting string from a txt file to int?

How will I add the existing content of the text file to the newly inputed date(hoursworked & minsWorked) to compute the total number of hours works please help me I'm just a beginner in using Visual basic C++. I dont know wht to do please help me guys.

I hope you can help me please this is in rush

#include <iostream>
#include <fstream>
#include<string>
#include <cstdlib>
using namespace std;

bool parseTime(char* _timeStr, int& _hour, int& _min)
{
// Make sure a pointer was passed in
if (NULL == _timeStr)
return false;

// Hours starts at the string passed in, minutes we figure out in a second
char* hourStr = _timeStr;
char* minStr = NULL;

// Loop through the string looking for the : character
char* curChar = _timeStr;
while (*curChar)
{
if (':' == *curChar)
{
// Found our minutes string. NULL terminate the hour string and set the minute string.
*curChar = '\0';
minStr = curChar + 1;
break;
}

++curChar;
}

// If no minute string was found, error
if (NULL == minStr)
return false;

// Convert the strings to integers and store them
_hour = atoi(hourStr);
_min = atoi(minStr);

// Restore the : character (could also use the "curChar" variable for this, but this way is more fun)
*(minStr - 1) = ':';

// Test for invalid values
if (_hour < 0 || _hour > 23)
return false;
if (_min < 0 || _min > 59)
return false;

// Theoretical success!
return true;
}

int main ()
{
char time1[80];
char time2[80];

std::ofstream outfile;
string line;
string name;
int totaltimeused;
ofstream myfile;
myfile<<totaltimeused;
myfile.close();
cout<<"Enter your name :";
cin>>name;
if(name=="a")
{

ifstream myfiles ("herald.txt");
if (myfiles.is_open())
{
while ( getline (myfiles,line) )
{
cout << line << '\n';
}
myfiles.close();
}else{
cout << "Unable to open file"; }

cout<<"Time in: ";
cin >> time1;
cout<<"Time out: ";
cin >> time2;

int hour1, min1;
int hour2, min2;

if (!parseTime(time1, hour1, min1))
{
cout << "Error parsing time 1: " << time1 << ". Invalid time format.\n";
return(0);
}

if (!parseTime(time2, hour2, min2))
{
cout << "Error parsing time 2: " << time2 << ". Invalid time format.\n";
return(0);
}

// Hours worked and minutes worked
int hoursWorked = hour2 - hour1;
int minsWorked = min2 - min1;


// It is possible that a full hour was not worked, meaning the minutes would be negative.
if (minsWorked < 0)
{
// If that is the case, deduct one hour and add 60 minutes. Since minutes is negative, this
// yields the actual minutes worked (IE, -4 becomes 56).
--hoursWorked;
minsWorked += 60;
}


cout << "Time worked: " << hoursWorked << " hours, " << minsWorked << " minutes.\n";


myfile.open ("herald.txt",std::ios_base::in);
myfile<< hoursWorked<< "hours,"<< minsWorked <<"minutes.\n";
myfile.close();

return 0;
}
}
For starters, to make your code easier to read use the <> button on the right of your text box under format. Your code will look more like
1
2
3
4
5
#include <iostream>
 #include <fstream>
 #include<string>
 #include <cstdlib>
 using namespace std;
I will take a look at your code real quick to see if I can be of any help, but here is my disclaimer, I am not, nor have I ever been, a licensed programmer. I am only a beginner but I will do my best.
First off, I do not see where you are initially opening your file. But, if I am understanding your question correctly, I believe you are looking for ios::app so for example: myfiles.open("herald.txt", ios::app); this will allow you to add information to an existing file.

sir with this I open my .txt

ifstream myfiles ("herald.txt");
if (myfiles.is_open())
{
while ( getline (myfiles,line) )
{
cout << line << '\n';
}
myfiles.close();
}else{
cout << "Unable to open file"; }
sir dub1987 can you help me please to create a simple program that computes
time like a Daily Time Record that will save the records to a text file.
May be like this format:
example.

Enter your name: GERALD
Date Morning Afternoon
In Out In Out
1 8:00 AM 12:00 AM 8:00PM 5:00PM
total:4:00 total:4:00
total per day:8:00hours

and it will be save to a .txt file and once the program will be run again it will
remember the total hours per day and will ask again the user to input for the other day.
Last edited on
look sir at this program

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>
#include<string>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y);
int choice;
float in,out,totalhours,inmin,outmin,totalmin,converter=60,temp1;
float in2,out2,totalhours2,inmin2,outmin2,totalmin2,converter2=60,temp2,daytotalhours,daytotalminutes; 
	void add()
	{
		
	gotoXY(30,1);cout<<"DAILY TIME RECORD";
	gotoXY(20,2);cout<<"MORNING"; gotoXY(50,2);cout<<"AFTERNOON"; 
	gotoXY(12,4);cout<<"Time-in"; gotoXY(28,4);cout<<"Time-out";
	gotoXY(42,4);cout<<"Time-in"; gotoXY(60,4);cout<<"Time-out";
	gotoXY(13,5);cin>>in; gotoXY(14,5);cout<<":"; gotoXY(15,5);cin>>inmin;
	gotoXY(29,5);cin>>out; gotoXY(31,5);cout<<":"; gotoXY(32,5);cin>>outmin;

		if(outmin<inmin)
	{
		temp1=inmin-outmin;
		converter-=temp1;
		totalhours=out-in;
		totalhours-=1;
     gotoXY(19,7);cout<<"Hours used";
	gotoXY(21,8);cout<<totalhours; gotoXY(22,8); cout<<":"; cout<<converter; 
	}
	else
	{
	totalhours=out-in;
	totalmin=outmin-inmin;

	if(totalmin>0&&totalmin<10)
	{
     gotoXY(19,7);cout<<"Hours used";
	gotoXY(22,8);cout<<totalhours;gotoXY(23,8); cout<<":";	gotoXY(24,8);cout<<"0"; ; cout<<totalmin; 
	}
	else
	{
		 gotoXY(19,7);cout<<"Hours used";
	gotoXY(22,8);cout<<totalhours; gotoXY(23,8); cout<<":"; cout<<totalmin; 
	}	
	}
	
	//afternoon
	gotoXY(43,5);cin>>in2; gotoXY(45,5);cout<<":"; gotoXY(46,5);cin>>inmin2;
	gotoXY(61,5);cin>>out2; gotoXY(63,5);cout<<":"; gotoXY(64,5);cin>>outmin2;

		if(outmin2<inmin2)
	{
		temp2=inmin2-outmin2;
		converter2-=temp2;
		totalhours2=out2-in2;
		totalhours2-=1;
     gotoXY(50,7);cout<<"Hours used";
	gotoXY(53,8);cout<<totalhours2; gotoXY(54,8); cout<<":"; cout<<converter2; 
	}
	else
	{
	totalhours2=out2-in2;
	totalmin2=outmin2-inmin2;
	
	if(totalmin2>0&&totalmin2<10)
	{
	gotoXY(50,7);cout<<"Hours used";
	gotoXY(53,8);cout<<totalhours2; gotoXY(54,8); cout<<":";	gotoXY(55,8);cout<<"0"; ; cout<<totalmin2; 
	}
	else
	{
	gotoXY(50,7);cout<<"Hours used";
	gotoXY(53,8);cout<<totalhours2; gotoXY(54,8); cout<<":"; cout<<totalmin2; 
	}	
	}
	daytotalhours=totalhours+totalhours2;
	daytotalminutes=totalmin+totalmin2;
	gotoXY(31,9);cout<<"Total Hours Used";
	gotoXY(37,10);cout<<daytotalhours<<":00";
	
	}
int main()
{
	gotoXY(33,2);cout<<"-----MENU-----";
	gotoXY(33,4);cout<<"(1) Add Record";
	gotoXY(33,5);cout<<"(2) View";
	gotoXY(33,6);cout<<"(3) Delete";
	gotoXY(33,7);cout<<"(4) Quit";
	gotoXY(33,8);cout<<"Choice:";cin>>choice;
	if(choice==1)
	{
		system("cls");
		add();
	}
	
	
	
return 0;
	
}

void gotoXY(int x, int y) 
{ 
CursorPosition.X = x; // Locates column
CursorPosition.Y = y; // Locates Row
SetConsoleCursorPosition(console,CursorPosition); // Sets position for next thing to be printed 
}
Are you trying to keep a running total of the hours or are you trying to make it more of a list format?
Topic archived. No new replies allowed.