I have NO idea what I am doing wrong

Program is to:

1. read a set of integers from a file into an array
2. perform a selection sort on the array
3. search the array for a value input by the user. If the value is in the array, display "Value found" on the screen. If the value is not in the array, display "Value not found" on the screen.

and this, is what I have thus far:


http://pastebin.com/3kCWMfFD


<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
/*---------------------------------------------------------*/
/* Search  */
/* */
/* This program searches an array made from a data file for characters   */
/*  */
/*  */

#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;

void main ()
{
	string string;
	double x;
	int i = 13;
	int cha[i];
	int count;
	ifstream myfile;
	myfile.open ("char2.txt");
	if (myfile.is_open())
		
	while(!myfile.eof())
			
	getline(myfile,string);
				
	if( ! myfile.fail () ) cout << "there's something wrong with the file!\n";

	for(count = 0; count < i; count++) if( !(myfile >> cha[i]) ) break;	
		

	cout << "Pick a letter betweent 'a' and 'z' to search for \n";
	cin >> "x";
	int flag = 0;
	for (int i = 0, i < 13, i++);  
	if ( x == cha[i])
	
	cout << "Letter found." << endl;
  
  	else
  
    	cout << "Letter not found." << endl;
  



	myfile.close();
	Sleep (20000);
	return 0;
}

</code>

Help?
Last edited on
That is an unconventional way to handle files in C++. I think you should print the array to confirm what values you've read.
meaning? Like print the array in the code or with a cout <<?

I have been struggling with array's. I've read and re-read the chapter, and have been at this for probably 4 hours.
Yes. Either that or check the values in a debugger.

If the values aren't being read, then nothing else will work, right?
correct. But, I feel there are other structural faults causing it to fail. Little bastard wont even compile! :/

I'm working on re-writing it..
He was being nice, You screwed up your file handling and it's so messy that no one with 1/2 a brain would waste breath trying to understand it.

If you cout some of your values, you should see where the problem is.

My solution was to delete everything after the while and start fresh, more or less.

The main problem I found was after your if, while and else statements, there were no {}. That not only tells the computer where to begin and end, it shows me that you know where it begins and ends and without it your screwing up.

The below code will compile, now add your statements one at a time, TEST it to be sure that it works then add another.

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
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;

int main ()
{
	string string;
	double x=0;
	int i = 13;
	int cha[i];
	int count=0;
	ifstream myfile;
	myfile.open ("ff.txt");
	if (myfile.is_open())
	{	
	while(!myfile.eof())
    {
	getline(myfile,string);
    cout << string << endl;  // Test
    }
    }
  	else
    {
        cout << "file not open" << endl;
    }

	myfile.close();
	return 0;
}


Have a nice day!
Last edited on
While rewriting, consider this.

The C++ I/O Streams library treats data as streams and sources/targets of data as sinks. So when you use a file stream, don't think of it as a file. think of it as a sink that handles a stream of data.

In practice that means, don't check for End Of File, don't check if it's open. And yes, the examples given in the tutorials of this site are wrong.

Here's an example that reads a file.
1
2
3
4
5
6
    std::vector<std::string> lines;
    std::string line;

    std::ifstream myfile("ff.txt");
    while (std::getline(myfile, line))
        lines.push_back(line);


Also, you have to learn to debug your code. You have to confirm that each step is correct. Debugging is an essential programming skill.
Topic archived. No new replies allowed.