Writing text to a textfile

Jan 12, 2011 at 9:09am
I'm relatively new to C++ and I'm working on the textfile concept now.. I'm having a problem opening textfiles and for some reasong the textfile is not saving in the project location. I'm using the Visual Studio. Anybody can explain to me what I'm doing wrong?

Here's the program code:

#include <iostream>
#include <fstream>
#include <string>

class mafia;

using namespace std;

/*int addweapon (int weapontype, string myfile){

}*/


int main ()
{
ofstream myfile;
myfile.open ("Iventory.txt", ios :: out);
myfile << "Writing this to a file.\n";
myfile.close();
/////// ?????? (this aint on the program dw)

char c;
char w;
char a;
char b;
char v;
char t;

cout << " \n\n\n\t\t-----------|| Welcome to Mini Mafia Wars ||-----------\n";

cout << " \n\tChoose an option from the menu by inputting the respective\n\tcharacter assigned to the option.\n\n";
cout << " \t---------------------------------------------\n\n";

cout << " \n\tA.) Buy weapon\n";
cout << " \n\tB.) Buy armour\n";
cout << " \n\tC.) Buy animal\n";
cout << " \n\tD.) Buy boost\n";
cout << " \n\tE.) Buy vehicle\n";
cout << " \n\tPress Q to quit the program --- |||";

w = 0;
a = 0;
b = 0;
v = 0;
t = 0;


c = 0;
while (c != 'Q' && c != 'q') {

cout << " \n\n\n\tChoice: ";
cin >> c;

switch (c){

case 'a' :
case 'A' :

cout << " \n\n\n\tChoose a weapon from the menu by inputting the respective\n\tcharacter assigned to the option.\n\n";
cout << " \t---------------------------------------------\n\n";

cout << "\n\t A.) Baseball bat\n";
cout << "\n\t B.) Crowbar\n";
cout << "\n\t C.) Crossbow\n";
cout << "\n\t D.) Metallic staff\n";
cout << "\n\t E.) Hand grenade\n";
cout << "\n\t F.) Machine gun\n";
cout << "\n\t G.) Back to Main Menu\n\n";



while (w != 'G') {

cout << " \n\n\tChoice: ;
cin >> w;

switch (w){

case 'a' :
case 'A' : break;

case 'b' :
case 'B' : break;

case 'c' :
case 'C' : break;

case 'd' :
case 'D' : break;

case 'e' :
case 'E' : break;

case 'f' :
case 'F' : break;

case 'g' :
case 'G' : main(); break;

default : cout << "\n\tERROR! => Only characters assigned to menu must be chosen!";

}
}
break;


case 'b' :
case 'B' :

cout << " \n\n\n\n\tChoose a type of weapon from the menu by inputting the respective\n\tcharacter assigned to the option.\n\n";
cout << " \t---------------------------------------------\n\n";

cout << "\n\t A.) Bullet-proof vest\n";
cout << "\n\t B.) Body armour\n";
cout << "\n\t C.) Tactical vest\n";
cout << "\n\t D.) Back to Main Menu\n\n";

while (a != 'D') {

cout << " \n\n\tChoice: ";
cin >> a;

switch (a){

case 'a' :
case 'A' : break;

case 'b' :
case 'B' : break;

case 'c' :
case 'C' : break;

case 'd' :
case 'D' : main(); break;

default : cout << "\n\tERROR! => Only characters assigned to menu must be chosen!";

}
}
break;


case 'c' :
case 'C' :

cout << " \n\n\n\n\tChoose a type of animal from the menu by inputting the respective\n\tcharacter assigned to the option.\n\n";
cout << " \t---------------------------------------------\n\n";

cout << "\n\t A.) Red-tailed hawk\n";
cout << "\n\t B.) Falcon\n";
cout << "\n\t C.) Dog\n";
cout << "\n\t D.) White Tiger\n";
cout << "\n\t E.) Back to Main Menu\n\n";

while (t != 'E') {

cout << " \n\n\tChoice: ";
cin >> t;

switch (t){
case 'a' :
case 'A' : break;

case 'b' :
case 'B' : break;

case 'c' :
case 'C' : break;

case 'd' :
case 'D' : break;

case 'e' :
case 'E' : main(); break;

default : cout << "\n\tERROR! => Only characters assigned to menu must be chosen!";

}
}
break;



case 'd' :
case 'D' :

cout << " \n\n\n\n\tChoose a type of boost from the menu by inputting the respective\n\tcharacter assigned to the option.\n\n";
cout << " \t---------------------------------------------\n\n";

cout << "\n\t A.) Extra pair of eyes\n";
cout << "\n\t B.) Hot coffee\n";
cout << "\n\t C.) 2x Mastery Boost\n";
cout << "\n\t D.) Back to Main Menu\n\n";

while (b != 'D') {

cout << " \n\n\tChoice: ";
cin >> b;

switch (b){
case 'a' :
case 'A' : break;

case 'b' :
case 'B' : break;

case 'c' :
case 'C' : break;

case 'd' :
case 'D' : main(); break;

default : cout << "\n\tERROR! => Only characters assigned to menu must be chosen!";

}
}
break;



case 'e' :
case 'E' :

cout << " \n\n\n\n\tChoose a type of vehicle from the menu by inputting the respective\n\tcharacter assigned to the option.\n\n";
cout << " \t---------------------------------------------\n\n";

cout << "\n\t A.) Motor-cycle\n";
cout << "\n\t B.) Delivery truck\n";
cout << "\n\t C.) Town car\n";
cout << "\n\t D.) Speed boat\n";
cout << "\n\t E.) Sports car\n";
cout << "\n\t F.) Luxury car\n";
cout << "\n\t G.) Back to Main Menu\n\n";

while (v != 'G') {

cout << " \n\n\tChoice: ";
cin >> v;

switch (v){

case 'a' :
case 'A' : break;

case 'b' :
case 'B' : break;

case 'c' :
case 'C' : break;

case 'd' :
case 'D' : break;

case 'e' :
case 'E' : break;

case 'f' :
case 'F' : break;

case 'g' :
case 'G' : main(); break;

default : cout << "\n\tERROR! => Only characters assigned to menu must be chosen!";

}
}
break;



case 'q' : void exit(); break;
case 'Q' : void exit(); break;


default : cout << "\n\tERROR! => Only characters assigned to menu must be chosen!";



}

}

}

Please help, I'm in desperate need haha -_-
Cheers.










Jan 12, 2011 at 10:55am
Well where is it writing to? I'd expect it to write to the directory you designated as the working directory for the programme, which would not be the "project" directory (i.e. not the same directory as the one in which your source code sits).

I haven't used VS for a while, but as I recall, one of the options in that mess of menus designates the working directory.
Jan 12, 2011 at 1:52pm
Please us code tags.
Like this
Jan 12, 2011 at 2:49pm
i solved it now thanks anyways hehe :)

tho i do have another problem -_- i'm doing this project at uni, and we have to make this "mini mafia wars" game.. Basically you have a menu and when you select a weapon or a type of armour etc.. it increments this choice onto the textfile..

ex: (this is the textfile output)

3 (displays total no. of items)

0 (weapon choice)
2 (crowbar)
4 (boost choice)
3 (2x mastery boost)
3 (animal choice)
1 (falcon)

please note that the words in the brackets are only comments
i know it is a bit confusing but i'm totally lost -_-

I want to use the functions (like the addweapons one) for the process of incrementing the choice count onto the textfile but I have no idea how to do this and whenever I try, I either give up or so confused that i simply just stop -_-

btw i know its stupid but how do you use code tags? i've never been in a forum for programming before :S
Topic archived. No new replies allowed.