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.
/*---------------------------------------------------------*/
/* Search */
/* */
/* This program searches an array made from a data file for characters */
/* */
/* */
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
usingnamespace 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;
}
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.
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.