File Creating and Reading Program.

I am suppost to make this for my class and i need help.. This is what i have so far i am not currently working on read files yet but writing files and i am having some problems. This is what my output looks like
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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include "conio.h"
using namespace std;
void filewrite()
{
	ofstream myfile;
	myfile.("Test.txt");
	myfile<<"testing testing!\n";
	myfile.close();
}

void createfile()
{
	
	string extention;
	string filename1;
	string Filewrite;
	system("cls");
cout<< " Creating File to Read Later on from this Win32 Console or from Documents"<<endl;
cout<< "\n\n What kind of extention will you use? ('example-.txt, .exe, .doc ')"<<endl;
cin>>extention;
system("cls");
cout<< " Please Give the Name of the File ! "<<endl;
cout<< " Name:";
cin>>filename1;
Sleep(2000);
cout<< " Please wait while the system Process"<<endl;
Sleep(2000);
system("cls");
cout<< " Writing on "<<filename1<<extention<<" , Please Write what you would like to write on the file"<<endl;
cin>>Filewrite;
filewrite();
_getch();
}
int quit()
{
	cout<<"Press Enter again to Quit"<<endl;
	_getch();
	return 0;
}
void read()
{
	cout<< "Read"<<endl;
	_getch();
}
int main()
{
main:
	system("cls");
	system("color 0a");
	main;
	int end;
	int end2;
	int choicemain;
	cout<< " CHOOSE YOUR PATH "<<endl;
	cout<< "\n\n\n\n1. Create a File"<<endl;
	cout<< "\n2. Read contents of a File"<<endl;
	cout<< "\n3. Quit"<<endl;
	cin>>choicemain;
	switch(choicemain)
	{
	case 1:{  createfile();}
		   break;
	case 2:
		{  
			read();
		}
		break;
	case 3:
		{ 
			cout<<"Press Enter again to Quit"<<endl;
	_getch();
	return 0;
		}
		break;
	default: {
		cout<< " Wrong Selection"<<endl;
		Sleep(2000);
		goto main;
			 }
			 break;
	}
end:
	system("cls");
	
	cout<< "Would you like to do it again? (1 = Yes) (2 = Quit)"<<endl;
	cin>>end;
	switch(end)
	{
	
	case 1:{
		 goto main;
		 break;
	}
	case 2:{
			quit();
			break;
		   }
	default: { cout<<" Wrong selection "<<endl;
		Sleep(2000);
		goto end;
			 }
			 break;
	}

	
}
	


can anyone tell me how to write the file because i am lost and have no idea how to do this... Sorry i don't have any comments
so.many.issues.

Number 1:
conio.h is (notstandard) not in ""s it's in <> The difference being the way the headers are searched for...

Number 2:
this : myfile.("Test.txt"); is wrong.
you can either: myfile.open("blah.blah");
or : ofstream myfile("blah.blah");

Number 3:
1
2
Sleep(2000);
cout<< " Please wait while the system Process"<<endl;
WTF???

Number 4:
You just totally the main. The whole main.

Recommendation
Try this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <string>
using namespace std;

int main(){
  fstream file;
  string filename = "output.txt";
               //This is simply a default initializer, since we haven't gotten the file name from the user

  //Receive file's path/name.extension from user here store in "filename" string

  file.open(filename.c_str(),ios::out);  
              // change to (filename.c_str(),ios::out|ios::app) if you want to append

  string output = "hello world"; 

  //Receive output from user here store in "output" string

  file << output;
  file.close();
}
Answers: Number 1 - I need it to put _getch (); and its causing no problem .
Number 2 - This Fixed my Problem.
Number 3 - just wanted to make it seem professional
Number 4 - I need a Menu for my class project an interface
Last edited on
@Number 1: I know. You're compiler is being a champ.
@Number 2: I figured.
@Number 3: goto = not super professional (IMHO)
@Number 4: I know. That's what the //comments were referring to.
"conio" <- Like that is what the CodeBlocks compiler uses. It takes it just like that, even though I have no other header files.
Topic archived. No new replies allowed.