Help with code!

So I have to ask the user what file they want to open up and what file they want to write too. With the file they open up I have to put those values into an array and do a bubble sort and then write the final sorted list to the file that they want to write too. I'm stuck, I don't know how to get the info from the file they open to my program then back out. Here's what I have so far. I'm just a beginner and my code is probably horrible, so please no bashing. Thank you in advance!


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

	char *textFile = "";
	char *outputFile = "";

	ofstream oFile;
	ifstream iFile;

	const int Info = 1000;
	int current, size[Info];
	int MaxNow, startValue, compareValue, temp;
	string Line;



	cout << "Please enter the name of the file you wish to open. \n" ;
	cin >> textFile;
	cout << "Please enter the name of the file you wish to wrtie to. \n" ;
	cin >> outputFile;

	iFile.open(textFile);
	oFile.open(outputFile);

	if (iFile.good() && oFile.good()) {

		while (iFile.good() && oFile.good()){

			getline(iFile, Line);


		}

	}
My advise:
Your program is too complicated for someone starting out, simplify.
The code above doesn't compile, you should strip out everything and start with

1
2
3
4
5
int main()
{
cout << "Hello World!" << endl;
return 0;
}


If you started with the above code, you'd know it's int main() not int Main().

Code one step, then test, code another... make sure it does what you want, make sure you know what it does....

As I said, too complicated, forget about the output file until your able to read the input file. You can't do anything with output until you have input and sort it.

Someone who has to do a "bubble sort" shouldn't be having these problems opening input files.

write a program flowchart
always put return 0; at the end of main.
always close the files you open.
use comments

You have a good start, took me a few mins to find the errors, for the most part it looks like it should work. There is not just one way to do something. Below is what I found worked for me.

I stripped out everything but what you needed to read a file, since that is what your having trouble with. I think the next thing you will want to do is read it into an array, then sort it, then output the array to a file. Should be fun, enjoy!

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

int main()
{
	string textFile;
	ifstream iFile;
	string Line;
	cout << "Please enter the name of the file you wish to open:\n";
	cin >> textFile;
	iFile.open (textFile.c_str());

	if (iFile.good())
	{
		while (iFile.good())
		{
		getline(iFile, Line);
// do something with input here
		cout << Line << endl;
		}
	}
	else // Let user know if can't open the input file.
	{
	cout << "Unable to open file." << endl;
	}

iFile.close();
return 0;
}
Last edited on
Ok, thank you, but then how would I put the values into an array and use the sorter? Isn't this just reading the file then going through line by line and writing each line down?
That's all it does, why don't you show me out to read a file into the array. Really, it's not hard. You can do it!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
const int Info = 1000;
	int current, size[Info];
	int MaxNow, startValue, compareValue, temp;


// If the file is good
// while the file is good


for (current = 0; current < Info; current++) {
		iFile >> size[Info];
	}
			getline(iFile, Line);



I think?? Idk, like I think i understand arrays but reading the array values in from a .txt file is just confusing to me :(
Last edited on
Have you tested that ?

I think you have the general idea, but seem that you might have some typos.

*edit* not sure that typos is the correct term. Lines 10-13 above look ok, I"m not sure what your doing with lines 1-3....


I have to go to bed after this. will read your reply tomorrow.



Remember think simple, what is the easiest way to do something?
When you have a couple years under your belt, then feel free to get fancy.
The bubble sort will be fancy enough....


1
2
3
4
5
int main(){
// Create an array
	string array[5];
// Create a loop counter for the array
	int loop=0;


Then
1
2
3
4
5
		getline(iFile, Line);
// copy line to array
	array[loop]=Line; //and output it
// increase loop count, incase there is another line.
        loop++;


// remove this line
cout << Line << endl;


// add this after the if/else code above iFile.close();
1
2
for (int out=0;out<=loop-1;out++)
{cout << array[out] << endl;}

Last edited on
Oh hell, I'll just post it. Please post your bubble sort tomorrow so I can see how your doing.

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

	string array[5];
	int loop=0;
	string textFile;
	ifstream iFile;
	string Line;
	cout << "Please enter the name of the file you wish to open:\n";
	cin >> textFile;
	iFile.open (textFile.c_str());

	if (iFile.good())
	{
		while (iFile.good())
		{
		getline(iFile, Line);
		array[loop]=Line;		// copy Line to array
		loop++;  // Update Loop Counter
		}
	}
	else // Let user know if can't open the input file.
	{
	cout << "Unable to open file." << endl;
	}

// Dump the array to screen
	for (int out=0;out<=loop-1;out++) // less 1 due to loop counter method
		{cout << array[out] << endl;}

iFile.close();
return 0;
}
Last edited on
Ok for the bubble sort this is what i have so far. (fingers crossed)

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

int currentMax, compareValue, temp, startValue;


//.... put this inside of the loop?????


for (startValue = 0; startValue < listSize-1; startValue++) {
		// Find the maximum for slot startValue
		currentMax = startValue;
		for (compareValue = startValue+1; 
			compareValue < listSize; 
			compareValue++) {
				
// check to see if current value is larger than current min

				if (values[compareValue] > values[currentMax]) {
					currentMax = compareValue;
				}
		}

		// Swap max value

		temp = values[startValue];
		values[startValue] = values[currentMax];
		values[currentMax] = temp;

	}



And i ran the program so far and found out that the file is "opening" however, when the loop is going through its not reading any of the lines in the .txt file. So would this be because of the file directory isnt included? I have checked my .txt file and there are lines in it.


Also, for the array, can I make the size as large as I want to or would that slow it down too much and cause errors?
the file is "opening" however, when the loop is going through its not reading any of the lines in the .txt file.


Which loop are you talking about ?
Line 17 will read the entire line one at a time for your file.
getline(iFile, Line);
to test that it's working put this after line 17
cout << Line << endl;

When cin ask for the file name, it has to be in the same directory, or the user has to type the path. c:\temp\myfile.txt
If it's not in the same directory, and they didn't put in a path, it should report "Unable to open file."
The Only problem I can think of is that it will not work with filenames with spaces in the file name.

Found this for you to read on bubbles.
http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
Sorry it took so long to get back to you, I was caught up in school all day.

As for the not reading any info, its still not doing it, at the getline(iFile, Line);
part, Line is returning ""

But it is saying that the file is good and opened so I made a mistake somewhere.
Ok I'm just an idiot, I didn't have my .txt files saved in right location in my project. So now when I run it through in debugging mode it does return values when I hover over "Line" everytime it goes through the loop, but the cout still isnt working, its not printing anything in the console window. I'm working on it though!
Ok lol... I just fixed that part too, so now I have it dumping the array onto the console screen. Just need to work on the bubble sort. and then the teacher added something else on there too... Not to sure how I'm going to do it yet :/
Topic archived. No new replies allowed.