Skip to main content
gamedev.net
🔒 Locked

Need help

Started by demonrealms 4 replies 800+ views
Original Post
demonrealms
demonrealms
Hey, I''m having trouble with writing to files. I''m trying to create a log system where you can keep important events down. Well, String doesn''t work, that just takes 1 word. char doesn''t work same thing. this is my code so far.
int main()
{
	string cmd1;
	display.display1 = 0;
	while(display.display1 == 0)
	{
		ifstream a_file("pass.txt");
		char string1[50];
		string pass;
		a_file >> string1;
		a_file.close();
		cout << " -----------------------------" << endl;
		cout << "| Welcome to the Demon Realms |" << endl;
		cout << "| Please enter password....   |" << endl;
		cout << " -----------------------------" << endl;
		cout << "Demon Realms Studios>> ";
		cin >> pass;
		if(pass == string1)
		{
			display.display1=(display.display1 + 1);
			cout << "Correct" << endl;
		}
		else
		{
			cout << "Incorrect" << endl;
		}
	}
	cout << "Demon Realms Studios>>" << endl;
	ofstream b_file("log.txt");
	cout << ": ";
	const int max = 50;
	char event[max];
	cin.getline(event, max);
	b_file << event;
	b_file.close();
	return(0);
} 
but all it does is come up and say press anykey to continue and doesn''t let me actually type anything. Any sugustions?
demonrealms
demonrealms
Hey, well it debugs fine so I have no idea whats going on.
demonrealms
demonrealms
Okey, now I''m beginning to think that my complier just syas well I fell like peeving ryan off and never work!
I''ve been working in this stupid thing for a month now!
Theres got to be a way where input will accept MORE then one word. If you have a space it just stops writing to the damn file right then and there. Heres the code
else if(cmd1 == "add")
{
char path[50];
cout << "Enter path name" << endl << ": ";
cin >> path;
cout << "What do you wish to add?" << endl;
const long max = 100;
char event[max];
cout << endl << ": ";
cin >> event;
cin.getline(event, max);
ofstream e_file(path);
e_file << event;
e_file.close();
}
WHATS GOING ON????
ShadowWolf
ShadowWolf
I think you need to use cin.get() to get more than 1 word. Cin (I believe) breaks at the first whitespace.
demonrealms
demonrealms
Hey, it''s not working so I might as well give the whole code. That might make it easier:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main()
{
cout << "What do you wish to do???" << endl;
string cmd1;
cout << ": ";
cin >> cmd1;
if(cmd1 == "new")
{
cout << "What group do you wish to be?" << endl;
string newusr;
cout << ": ";
cin >> newusr;
ofstream a_file("newusr.bat");
a_file << "md " << newusr << endl << "del newusr.bat" << endl;
a_file.close();
cout << "NOTE: Now please listen carefully," << endl;
cout << "Execute newusr.bat then type in c to continue." << endl;
string confirm;
cout << ": ";
cin >> confirm;
if(confirm == "c")
{
ofstream b_file("log.txt");
b_file << "Welcome from Demon Realms Studios!" << endl;
b_file.close();
cout << "Processing... Done." << endl;
cout << "Now your almost done. Just click on move.bat now and" << endl;
cout << "then your all set!" << endl;
ofstream c_file("move.bat");
c_file << "move log.txt " << newusr << endl << "del log.txt" << endl << "del move.bat" << endl;
c_file.close();
return(main());
}
else
{
cout << "Huh??" << endl;
}
}
else if(cmd1 == "add")
{
char path[50];
cout << "Enter path name" << endl << ": ";
cin >> path;
cout << "What do you wish to add?" << endl;
const int max = 100;
char event[max];
cout << endl << ": ";
cin >> event;
cin.getline(event, max);
ofstream e_file(path);
e_file << cin.getline(event, max);
e_file.close();
}
else
{
cout << "Huh??" << endl;
}
return(0);
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.