assigning values from text files.

Hello, I need to have a project where the console looks like this. (https://gyazo.com/5bce349994b0d7f72cdb706092e8fdb3) I currently have this code.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>



using namespace std;

int main() {

	int distance = 0;

	ifstream file2;
	ifstream file3;
	string fileName = " ";
	string line;





	do {
		cout << "Please enter a file name." << endl;
		getline(cin, fileName);
	} while (fileName != "file1"&& fileName != "file2"&& fileName != "file3");

	cout << "Reading File... Done" << endl;
	if (fileName == "file1") {
		ifstream file1;
		file1.open("input1.txt");
		int x1;
		int x2;
		int y2;
		int y1;
		file1 >> x1 >> x2 >> y1 >> y2;
		if (file1.is_open()) {
			while (getline(file1, line)) {
				cout << line << endl;
			}
			
			while (file1) {
				cout  << x1 <<  y1 << x2 << y2 << endl;
				file1 >> x1 >> x2 >> y1 >> y2;
			}
			file1.close();
		}
		
	}
	else
			cout << "File1 is not open" << endl;

		
	

	if (fileName == "file2") {
		ifstream file2("input2.txt");
		if (file2.is_open()) {
			while (getline(file2, line)) {
				cout << line << endl;
			}
			file2.close();
		}


	}
	else cout << "File2 is not open" << endl;

	if (fileName == "file3") {
		ifstream file3("input3.txt");
		if (file3.is_open()) {
			while (getline(file3, line)) {
				cout << line << endl;
			}
			file3.close();
		}


	}
	else cout << "File3 is not open" << endl;



	system("pause");



I need the user to be able to pick an input/text file they want to choose and then make those text files into values to then calculate the distance formula with them. It would be greatly appreciated for helping.
Why did you start a new thread? Did you figure out the problem that @Ganado was trying to help you with?

The next question is, why do you bother asking the user to enter a file name if you don't use that name when you open the file? The user enters "file1" and you open "input1.txt". That doesn't really make sense.

I assume all of the input files will contain similarly formatted data. Instead of having separate blocks of code for input1.txt, input2.txt and input3.txt, you should have a single block of code (or even a function if you've learned how to use them) to do the work. Just pass in the chosen file name and do all of the common processing in a single place. You only need 1 input file stream.

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
int main() {

	int distance = 0;

	string fileName = " ";
	string inputStreamName = " ";

	do {
		cout << "Please enter a file name." << endl;
		getline(cin, fileName);
	} while (fileName != "file1"&& fileName != "file2"&& fileName != "file3");

	cout << "Reading File... Done" << endl; // huh?  you haven't read the file yet!

	if (fileName == "file1")
	{
		inputStreamName = "input1.txt";
	}
	else if (fileName == "file2")
	{
		inputStreamName = "input2.txt";
	}
	else if
	{
		inputStreamName = "input3.txt";
	}

	// This should be code common to all input files!!
	ifstream input;
	input.open(inputStreamName);
	string line;
	int x1;
	int x2;
	int y2;
	int y1;
	input >> x1 >> x2 >> y1 >> y2;
	if (input.is_open()) {
		while (getline(input, line)) {
				cout << line << endl;
		}
	// etc. 


However, to make sure you have things working, start by hard-coding the file name to one of the data files. Leave the user interface until you have the guts of the program working. In other words, comment out lines 5-26 (of my code) and replace them with

string inputStreamName = "input1.txt";

Get your code working and then add in the ability to choose the input file.

Some hints. Your distance will need to be a floating point number -- I recommend a double.

Start by simply reading in a line and writing it back out. In line 36 (of your code) you read in a line (in piecemeal fashion) and don't do anything with it. Then in lines 38 and 39 you read in the entire file line by line and print out each line except the first, which was read in line 36. Lines 42-45 also seem to be what you want, but you've already exhausted your file, so there is nothing left to read.

Figure out what you are trying to do. Simplify your code to do 1 thing at a time, and add on a new capability only after the previous step works.


Last edited on
Oh my. Thanks a lot for all of this. I will give this a try tonight. I started a new thread because I figured out my older problem. Once again thank you for this. It will help a lot. I will let you know what goes on tonight.
Thanks again.
Topic archived. No new replies allowed.