My loop is not saving lines in text file. HELP.

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
int sec;
sec = 60;
int min;
min=59;
int hour;
hour = time-1;

				
while(true){	

Sleep(1000);
												
sec = sec-1;
												
if(sec==0){
min=min-1;
sec=60;
}
if(min==0){
hour = hour-1;
min = 59;
}
system("cls");
cout<<"\n\n\t\tTime: "<<
cout<<"\n\t\t"<<hour<<" hrs "<<min<<" mins "<<sec<<" sec"<<endl;
											
					
myfile2.open("pc1.txt");
											
myfile2 << "off";
myfile2 <<hour;
myfile2 <<min;
myfile2 <<sec;
												
myfile2.close();
Sleep(1000);											
}


Hi guys. My loop is not saving what i want to save in my text file. Help.
Last edited on
Well. You are closing the file inside the loop aswell. You open the file, then close it. Move the

myfile2.close(); outside the loop.

Also, never use loops like this while(true){
Its not good, use actual conditions.
Last edited on
I moved it. The text file is empty now :(
Can you post the entire 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
void login(){
	
	create c;
	
	char usernameConfig[15];
	char passwordConfig[15];
	char username[15];
	char password[15];
	char sub[15], concat[15];
	int time;
	int load=0.00;
	int ctr;
	int timeConfig;
	int loadConfig=0.00;
	int newTime;
	int newLoad=0.00;
	bool repeat = true;
	char choicee;
	
	cout<<"\n\n\t\tEnter username: ";
	cin.getline(username,15);
	
	cout<<"\n\n\t\tEnter password:";
	cin.getline(password,15);

	ifstream newfile;
	ifstream myfile;
	ifstream myfile1;
	ofstream myfile2;
	
	newfile.open("counter.txt");
	if(newfile.is_open()){
		newfile >> ctr;
		newfile.close();
	}


	for(int x=1; x<ctr+1; x++){
		
		strcpy(concat,"account");
		strcat(concat,"[");
		itoa (x,sub,10);
		strcat(concat,sub);
		strcat(concat,"].txt");
	
       	        myfile.open(concat);
			
		if(myfile.is_open()){
				
				
		myfile >> usernameConfig;

		myfile >> passwordConfig;

				
				
		myfile.close();
			
}
				
if(strcmp(username,usernameConfig)==0 && strcmp(password, passwordConfig)==0){
										
		strcpy(concat,"bcount");
		strcat(concat,"[");
	        itoa (x,sub,10);
		strcat(concat,sub);
                strcat(concat,"].txt");

		myfile1.open(concat);
										
		if(myfile1.is_open()){
		myfile1 >> time;
		myfile1 >> load;
		myfile1.close();
		}


		cout<<"\n\n\t\tWelcome, "<<username;
	        cout<<"\n\t\tTotal time: ";
		cout<<time<<"hrs";
		cout<<"\n\t\tTotal mount payed: ";
		cout<<"P "<<load<<".00";
										
		cout<<"\n\n\t\t[A]	Start session";
		choicee = getch();
												
		switch(choicee){
													
		case 'a':

int sec;
sec = 60;
int min;
min=59;
int hour;
hour = time-1;

				
while(true){	

Sleep(1000);
												
sec = sec-1;
												
if(sec==0){
min=min-1;
sec=60;
}
if(min==0){
hour = hour-1;
min = 59;
}
system("cls");
cout<<"\n\n\t\tTime: "<<
cout<<"\n\t\t"<<hour<<" hrs "<<min<<" mins "<<sec<<" sec"<<endl;
											
					
myfile2.open("pc1.txt");
											
myfile2 << "off";
myfile2 <<hour;
myfile2 <<min;
myfile2 <<sec;
												
myfile2.close();
Sleep(1000);											
}
				

getch();
           
 }


This is just a part of the class i'm making. Which is too long.. Btw i'm making a pc rental system and it's due later im goin insane lol
Its cuz of this while(true){ Why is it even true? That makes no sense. You need to tell the program when you want the while loop to exit.
How? The loop serves as my timer. Idgi. I'm sorry X_X
When you do while(true) you get an infinite loop. You never want that. It will never exit the loop. You have to tell the loop to exit at some point, otherwise it wont work.
Can you give an example? I can't see the logic :(
for example. If you change the loop to this

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
int counter = 0;
while(counter < 100){	

Sleep(1000);
												
sec = sec-1;
												
if(sec==0){
min=min-1;
sec=60;
}
if(min==0){
hour = hour-1;
min = 59;
}
system("cls");
cout<<"\n\n\t\tTime: "<<
cout<<"\n\t\t"<<hour<<" hrs "<<min<<" mins "<<sec<<" sec"<<endl;
											
					
myfile2.open("pc1.txt");
											
myfile2 << "off";
myfile2 <<hour;
myfile2 <<min;
myfile2 <<sec;
												
myfile2.close();
Sleep(1000);
counter++;										
}


Your program should work, although maybe not exactly as intended, cuz its a bit of a weird setup.
Topic archived. No new replies allowed.