Code Checker! Should i improve anything here?

I have the following code here. Just want the forum members to check if everything looks ok and whether I can improve on something here or not. one more thing is that I added a comment below in my code regarding if the user entered in a string with spaces. I would love to get help on that scenario please. The assignment doesn't ask me but i'm looking to gain more insight of c++. Thanks guys!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void openfile (string &filename, string filename2, ifstream &in,ofstream &out )

{
	//Make an input file
	//Note: filename is a string variable. Suppose user enters in myname.txt hello. I want to get rid of hello? and possibly not use hello to get stored in variable filename2. How can I?
	cout<<"Enter Input File name: ";
	cin>>filename;

	//Convert the file in to c string
	in.open(filename.c_str());

	//make an output file
	cout<<"\nEnter Output File Name: ";
	cin>>filename2;
	out.open(filename2.c_str());


}
If you want to include the spaces in the file name, use getline (but be aware of mixing cin >> and getline in the same code).

If you want to discard everything after the first word, you can use this:
1
2
cin >> filename; // This only gets the first word
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Also need to #include <limits> 


Basically, that tells cin to scrap everything that's in the input buffer, until it reaches a newline character (which comes from pressing Enter after your input).

If you're too lazy to #include <limits> and/or type all of that, you could probably just do something like
cin.ignore(9999, '\n');
which will get rid of 9999 characters in the input buffer or until it hits a newline.
(But note that if you were to type, say, 10000 characters, it wouldn't be able to get rid of all of them.)
Long double main, understood you on this part and would look forward to use these functions, but in my following which is the same as this, though I added more stuff. I don't know what is wrong. I made hello.txt as my input file. Made myname.txt as outputfile. At first when I was looking for both files, I only found myname.txt and no hello. So I deleted that myname.txt file and made it manually both of them. "hello.txt" and myname.txt." Also, I got an error, when I delted those files.

What was the case over here and why wouldn't the program run and promted me an error. I thought the program would ask the user everytime to make a file of input and output.

Pursuing further, I manually mdae them both and ran the program which worked. I inputted both file names. myname.txt was made, but once again hello.txt was not made. For hello.txt my manual was there. For myname.txt my manual and the automated one was there. So i'm performing this program now and I don't get an output on the myname.txt. Can I know whats wrong?

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

void openfile (string &filename, string filename2, ifstream &in,ofstream &out )

{
	//Make an input file
	//Note: filename is a string variable. Suppose user enters in myname.txt hello. I want to get rid of hello? and possibly not use hello to get stored in variable filename2. How can I?
	cout<<"Enter Input File name: ";
	cin>>filename;

	

	//Convert the file in to c string
	in.open(filename.c_str());

	//make an output file
	cout<<"\nEnter Output File Name: ";
	cin>>filename2;
	out.open(filename2.c_str());


}
void studenttestreader (int testscores[],int size,ofstream &out, ifstream&in)
{
	//Total number of students not defined. There would be less then 50 though. So have numberofstudents declared to 0;
	int numberofstudents=0;

	in>>testscores[numberofstudents];

	//while file is open

	while (in>>testscores[numberofstudents] && numberofstudents<size)
	{
		in>>testscores[numberofstudents];
		out<<testscores;
	
	
	
	}
	
	



}

void closefiles(ifstream &in, ofstream &out)
{
	in.close();
	out.close();


}
int _tmain(int argc, _TCHAR* argv[])
{
	//Declare variables
	string filename, filename2;
	ofstream output;
	ifstream input;
	//call function
	openfile(filename,filename2,input,output);

	//Call function to read the scores
	int testscorereader[50];
	studenttestreader(testscorereader,50,output,input);


	//close the files
	closefiles(input,output);
Hello?
Topic archived. No new replies allowed.