int main()
{
ifstream inFile;
int even=0,odd=0,x,y;
inFile.open ("hw4dataf11.txt");
if(inFile.fail()) //is it ok?
{ cout<<"file did not open please check it\n";
system("pause");
return 1;
}
inFile>>x;
while(!inFile.eof())
{y=x%2;
if (y==0)
even++;
if (y==1)
odd++;
inFile>>x;
}
cout<<"Number of even numbers "<<even<<endl;
cout<<"Number of odd numbers "<<odd<<endl;
system("pause");
return 0;
}
Don't know why it wont read my file! Can anyone help??
It just can't find the file to open. Without the path before the file name the system takes the file from the current directory (in the code::blocks environment it is where the project file *.cbp resides).
So either put the file where it can be found or write the whole path before that file
/*
Help!
I am trying to read in a ".txt" file and manipulate it so i can identify the words
but i get an error code whenever i try to use a multi lined document
*/
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <cctype>
#include <algorithm>
#include <iostream>
int main(){
//bool istream keep track of if istream fails
bool istream = false;
string input = "", line = "";
//vector of string pointers
vector <string*> words;
//vector int pointers
vector <int> count;
//keep track of vector length
int vlength = 0;
//total number of word in a file
int tWords = 0;
cout << "Input file to be read." << endl;
cin >>input;
//in filestream
fstream in (input);
//running file
if (in.is_open()){
while (in.good()){
//string for temp word storage
string neoWord = "";
//string pointer
string *ptr;
//getline
getline (in,line);
//seperate line to words
int lastSpace = 0;
//loops through entire string
for (int length = 0; length <= line.length(); length ++){
//flags spaces as word end ponts
if (line.substr(length,1) == " " || length == line.length()){
neoWord = (line.substr(lastSpace,length-lastSpace));
//through out " "
if (neoWord == " "){
continue;
}
//get rid of non alphnumeric bookends
//the problem is with the line bellow
while (!isalnum(neoWord[0])){neoWord = neoWord.substr(1,neoWord.length()-1);}
if(!isalnum(neoWord[neoWord.length()-1])){neoWord =
neoWord.substr(0,neoWord.length()-1)}
length ++;
lastSpace = length;
//lower word case
int neoLength = neoWord.length();
for(int i=0; i < neoLength; i++){
//std::transform(neoWord.begin(), neoWord.end(), neoWord.begin(),
std::tolower);
//transfrom word to lower case
if(isupper(neoWord[i])){std::transform(neoWord.begin(), neoWord.end(),
neoWord.begin(), std::tolower);}
}
//compares word with words already in the vector
if (vlength == 0){
//get memory size of neoWord
ptr = new string[neoWord.length()];
//make that meomry hold neo word
*ptr = neoWord;
//put adress in words
words.push_back(ptr);
//increase count
count.push_back(1);
vlength ++;
} else {
bool repeat = false;
for(int compare = 0; compare < vlength; compare ++){
if (*words[compare] == neoWord){
//assign repeat to trure
repeat = true;
//increase count
count[compare] ++;
//exit loop
break;
}
}
if(!repeat){
//get memory size of neoWord
ptr = new string[neoWord.length()];
//make that memory hold neoWord
*ptr = neoWord;
//put adress in words
words.push_back(ptr);
//increase count
count.push_back(1);
vlength ++;
}
}
}
}
in.close();
}
}
/* I have done testing with break points and have determined that problem occurs whenever i try to read in a line that is after use of the "return" key
like this one.
What should I do?