0
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(){
	fstream First("firstt.txt");
	fstream File("file.txt");
	string firsttime;
	string blah1 = "enable";
	string blah2 = "disable";
	getline(First, firsttime);

	if(firsttime!="not"){
		First << "not";
		cout << "first time settings (to be changed at file.txt)" << endl << "allow every one enter? enter disable(for no) and enable(for yes)" << endl;
		string allow;
		int end=0;
		while(end!=1){
			cin >> allow;
			if(allow==blah1){
				File << "allow? \n";
				File << "enable";
				end = 1;
				return 0;
			}else if(allow==blah2){
				File << "allow? \n";
				File << "disable";
				end = 1;
				return 0;
			}else{
				cout << "sorry what? please enter again" << endl;
			}
		}
	} else{

		string canIS;
		for(int i =1; i<=2;i++){
			getline(File, canIS, '\n');
		}

		if(canIS=="enable"){
			cout<<"hey you hit the second line ";
			cout << canIS << endl;
		} else{
			cout << "sorry the admin do not allow to you\n";
		}	
	}	


  system("pause");
}

thats my code its a basic "settings" program and look on the top of it you will see
the fstream "First" and it suppose to write into it "not" (in the first IF)
and it dosent.... any ideas why? thanks anyway

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by venomxxl
0

Actually, I wasn't completely right in my post above. The destructor also closes the stream. That means you don't have to close the stream by calling close method. Guess I'm still learning :)

This works correctly (though you have to delete or clear the files to change settings):

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(){
	fstream First;
	fstream File;

	string firsttime;
	string blah1 = "enable";
	string blah2 = "disable";

	First.open("firstt.txt", ios_base::in); // won't delete previous data saved in firstt.txt
	First >> firsttime;
	First.close();

	if(firsttime!="not"){
		First.clear();
		First.open("firstt.txt", ios_base::out | ios_base::trunc); // will delete previous data saved in firstt.txt
		First << "not";
		First.close();
		File.open("file.txt", ios_base::out | ios_base::trunc); // will delete previous data saved in file.txt
		cout << "first time settings (to be changed at file.txt)" << endl << "allow every one enter? enter disable(for no) and enable(for yes)" << endl;
		string allow;
		int end=0;
		while(end!=1){
			cin >> allow;
			if(allow==blah1){
				File << "allow? \n";
				File << "enable";
				end = 1;
				File.close();
				return 0;
			}else if(allow==blah2){
				File << "allow? \n";
				File << "disable";
				end = 1;
				File.close();
				return 0;
			}else{
				cout << "sorry what? please enter again" << endl;
			}
		}
	} else{

		string canIS;
		File.open("file.txt", ios_base::in); // won't delete previous data saved in file.txt
		for(int i =1; i<=2;i++){
			getline(File, canIS, '\n');
		}
		File.close();

		if(canIS=="enable"){
			cout<<"hey you hit the second line ";
			cout << canIS << endl;
		} else{
			cout << "sorry the admin do not allow to you\n";
		}	
	}	


  system("pause");
}

Edited by venomxxl: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.