C++ vector subsctript out of range - c++

In file called abc.txt, I have input following text:
sample text
sample text
sample text
sample text
sample text
Firstly I created variable(named text) for saving text- read from the file. Then program reads file abc.txt. I created vector named: ArrSent for saving each line from the file abc.txt. After loop while ends program close the file abc.txt. Then program have to output all sentences from vector ArrSent to the screnn.I have this kind of problem: after end of program, appears alert with message: vector subscript out of range. I have no idea why..
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
void function()
{
string text;//variable to save text from file
ifstream myfile("abc.txt");//reading from file colled abc.txt
vector<string> ArrSent;
if (myfile.is_open())
{
//cout <<"myplik.good()= "<< myfile.good() << endl;
while (myfile.good())
{
getline(myfile, text);
ArrSent.push_back(text);
}
myfile.close();
}
for (int i = 0; i <= ArrSent.size(); i++)
{
cout << ArrSent[i] << endl;
}
}
int main()
{
function();
system("pause");
return 0;
}

It is wrong here
for (int i = 0; i <= ArrSent.size(); i++)
{
cout << ArrSent[i] << endl;
}
should be
for (int i = 0; i < ArrSent.size(); i++)
{
cout << ArrSent[i] << endl;
}
The reason for that is that in C/C++, vector/array are zero based. That is to say that, if you have a vector, my_vector, size of 10, it goes like, my_vector[0], my_vector[1], ... my_vector[9]. There is no my_vector[10].
A better way to iterate through it , could be (C++11)
for (const auto & v : ArrSent)
{
cout << v << endl;
}
or
for (vector<string>::const_iterator i = ArrSent.begin(); i != ArrSent.end(); ++i)
cout << *i << endl;
As pointed out by WhozCraig, the while loop for reading is also buggy, a better version could be
while (getline(myfile, text))
{
ArrSent.push_back(text);
}
A Word About function
Notable: Your function name is function. While that may be descriptive, you should know that standard library headers can freely include other standard library header (and very frequently do just that). One such header in the standard library is <functional> which declares, as luck would have it, std::function.
Why would you care? Because your using namespace std; brings everything in std out in the open with no namespace qualifier requirements, including potentially std::function (whether or not you included <functional>).
Which means although this will still compile:
void function()
{
// .. stuff
}
This may not:
int main()
{
function(); // HERE
//... other stuff
}
This doesn't know whether you're calling your function or attempting to instantiate a single temporary of type std::function<> (which it can't anyway, as no template parameters are described). The result may be a ambiguous.
You can fix this by using ::function(); in your main(), but it would be better if you got developed the habit of not slurping in the entire std library in via using namespace std;, and/or not using names of common types/ids from the standard library.

Related

How to open a file outside working directory in c++ using <iostream> library?

I'm trying to make my own cmd in Windows. I need an alternative to the fstream library which I'm not familiar with. I have heard about the iostream ibrary for file input/output also if there is such a thing can you show me an example? I'm also getting an error with this code in Code::Blocks:
60: No function matching for call to std::basic_ifstream< char >::open(std::__cx::string&)
Here is my code:
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <fstream>
#include <conio.h>
void processor_xH(std::string);
void motherboard_xH(std::string);
void prccept(std::string);
void o_app(std::string);
//Contains global variables and functions
std::string command;
int main()
{
//Check whether the system("") command is availible or exit...
if(system(NULL)) std::cout << "Ok..." << std::endl;
else std::cout << "Sorry, an error occured..." << std::endl;
//Now set the color settings and output directory
system("CLS"); system("COLOR 50");
std::cout << "#jedaiCoder $go-do- ";
//Take command as input string
std::getline(std::cin, command);
processor_xH(command);
//Check for specific commands in string
getchar(); return 0;
}
void processor_xH(std::string input)
{
std::string srchI[]={"prccept", "o-app"};
for(int x=0; x<=1; x++)
{
if((input.find(srchI[x]))!=std::string::npos)
{
//Then find the command
motherboard_xH(srchI[x]);
}
}
}
void motherboard_xH(std::string x)
{
if(x=="prccept") prccept(x);//Command for printing file or text
if(x=="o-app") o_app(x); //Open file for reading writing
}
void prccept(std::string x)
{
}
void o_app(std::string x)//Guys the error is over here
{
std::string y=x.substr(7, 8);
x=x.substr(9, 12);
if(y=="-w" || y=="-r")
{
std::ifstream file;
file.open(x);
}
}
This project also might contain some other errors so please do point them out.
Also please excuse my using of the system("") command as I am a beginner and I know SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5|BACKGROUND RED) but I want to set background color for the whole program.
You may be using an older compiler that doesn't like open(std::string&).
Try this instead:
file.open(x.c_str());
Also consider passing strings by reference. If you are not changing the string, pass by constant reference. If you like using a copy of the parameter's value, then use a different variable in your function and assign it a copy of the parameter.
BTW, the idiom for a for loop is:
for (index = 0; index < quantity; ++index)
So if there are two strings, the statement becomes:
for(int x=0; x < 2; x++)

Error Vector subscript out of range

I believe my error is within my writeline function, when I attempt to write the contents of the vector to the new file using a while loop.
//Read from txt file, write to new text file
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
void readline();
void sortline(vector<string>& sortthis);
void writeline(vector<string>& list);
int main()
{
readline();
system("pause");
return 0;
};
void readline()
{
string line;
vector<string> lines;
ifstream myfile("classes.txt");
if (myfile.is_open())
{
while (myfile.good())
{
getline(myfile, line);
lines.push_back(line);
};
myfile.close();
}
cout << "readline() has run" << endl;
sortline(lines);
writeline(lines);
};
void sortline(vector<string>& sortthis)
{
sort(sortthis.begin(), sortthis.end());
};
void writeline(vector<string>& list)
{
ofstream myfile2("new.txt");
if (myfile2.is_open())
{
int i = 0;
while(i !=list.size()-1)
{
myfile2 << list[i] << endl;
i++;
};
myfile2.close();
};
cout << "writeline() has run" << endl;
};
this is a project from a semester ago that i'm revisiting. I wrote the program on my mac, now i'm trying to run it on my windows comp with visual studio. I'll describe what I'm attempting to do, I apologize if my choice of words is terrible in advance. anywhere I put a * is where I'm not sure what is happening, but I'll take a stab at it.. any explanations of my code is very appreciated!!
my readline() function does the following: creates a string called line, creates a vector of string type called lines, **input the file classes.txt and establish myfile as it's object, then open myfile for writing, **use the while loop to write the lines from the txt into the myfile object, then close myfile, print out a statement to let the user know readline() has run, then **pass the vector called lines into the sortline function, and then pass lines into the writeline function.
** sortline takes in a vector of strings as its arg, and assigns it the object sortthis?? then I'm not sure what happens, but it looks like i applied a sorting algorithm, anybody have any thoughts?
and finally we get to my writeline function which takes in a vector of strings as its arg and assigns them the name lines (is that correct?) i then want to establish a new out file stream to a new textfile called "new.txt" with an object name myfile2, if myfile2 is open, then i want to write all the lines from the vector of strings(which contain the contents of the original text file) into myfile2, which will write them to the new.txt file, then close myfile2, print a message stating the function has run, and that is all.
The way you loop through list in writeline is not safe. You should use a for loop or a while loop with iterator. As it is, your code probably doesn't do what you intend it to do even if there are several elements in list. Consider the following:
std::vector<std::string> vLines;
vLines.push_back("Hello");
vLines.push_back("File");
vLines.push_back("World");
std::ofstream of("file.txt");
int i = 0;
while (i != vLines.size() - 1)
{
of << vLines[i] << std::endl;
++i;
}
Even with several elements in vLines, this will only actually print output 2 elements into of.
i will be 0 which is not 2, so "Hello" will be output to of.
i will be 1 which is not 2, so "File" will be output to of.
i is now 2, which is equal to 2, so "World" will not be output to of.
That's with elements. If there are 0 elements in vLines, you will be indexing out of bounds (which I suspect is what you are doing, hence your error):
std::vector<std::string> vLines;
std::ofstream of("file.txt");
int i = 0;
while (i != vLines.size() - 1)
{
of << vLines[i] << std::endl;
++i;
}
i will be 0, which is not equal to -1, so the code will run and try to output vLines[0] to of, but there is no vLines[0]! I suspect this is what you are experiencing.
This will go away if you use a proper range-based loop instead (credit to #WhozCraig for C++11 solution):
for (auto const& s : vLines)
of << s;
Or if you don't have C++11 you can still mimic a proper range-based loop with the following:
for (int i = 0; i < vLines.size(); ++i)
of << vLines[i] << std::endl;
Or an iterator:
for (auto it = vLines.begin(); it != vLines.end(); ++it)
of << *it << std::endl;
You will now output all elements in your std::vector to your std::ofstream as well as properly handle situations where there are no elements.

Checking if one document has the contents of the other c++

I am writing a code to check to see if one document (text1.txt) contains a list of banned words (bannedwords.txt) in it.
For example, the text1 document contains lyrics to a song and i want to check whether the word pig from the banned document is included in it. I then want the out put to be similar to:
"pig" found 0 times
"ant" found 3 times
This is what I have come up with so far but cannot seem to put the array of banned words into the search. Any help would be amazing :D
Thanks Fitz
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool CheckWord(char* filename, char* search)
{
int offset;
string line;
ifstream Myfile;
Myfile.open(filename);
if (Myfile.is_open())
{
while (!Myfile.eof())
{
getline(Myfile, line);
if ((offset = line.find(search, 0)) != string::npos)
{
cout << "The Word " << search<< " was found" << endl;
return true;
}
else
{
cout << "Not found";
}
}
Myfile.close();
}
else
cout << "Unable to open this file." << endl;
return false;
}
int main()
{
ifstream file("banned.txt");
if (file.is_open())//file is opened
{
string bannedWords[8];//array is created
for (int i = 0; i < 8; ++i)
{
file >> bannedWords[i];
}
}
else //file could not be opened
{
cout << "File could not be opened." << endl;
}
ifstream text1;//file is opened
text1.open("text1.txt");
if (!text1)//if file could not be opened
{
cout << "Unable to open file" << endl;
}
CheckWord("text1.txt", "cat");
system("pause");
}
Your main() function is reading the contents of banned.txt into an array of 8 std::string named bannedWords.
The array bannedWords is not being used anywhere after that. C++ doesn't work by magic, and compilers are not psychic so cannot read your mind in order to understand what you want your code to do. If an array (or its elements) are not accessed anywhere, they will not be used to do what you want with them.
You need to pass strings from the bannedWords array to CheckWord(). For example;
CheckWord("text1.txt", bannedWords[0].c_str());
will attempt to pass the contents of the first string in bannedWords to CheckWord().
However, that will not compile either unless you make the second parameter of CheckWord() (named search) be const qualified.
Or, better yet, change the type of the second argument to be of type std::string. If you do that, you can eliminate the usage of c_str() in the above.
I don't claim that is a complete solution to your problem - because there are numerous problems in your code, some related to what you've asked about, and some not. However, my advice here will get you started.
Your question is really vague; it looks like you need to spend some time to pin down your program structure before you could ask for help here.
However, since we were all new once, here's a suggestion for a suitable structure:
(I'm leaving out the file handling bits because they're irrelevant to the essential structure)
//Populate your array of banned words
std::string bannedWords[8];
int i;
for (int i = 0; i < 8; ++i)
{
file >> bannedWords[i];
}
//Load the entire file content into memory
std::ifstream in("text1.txt");
std::string fileContents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
So now the entire file content is in the string "fileContents", and the 8 banned words are in "bannedWords". I suggest this approach because otherwise you're opening, reading, and closing the file for every word. Hardly a good design.
Now you've got to check each word against the file content. There's some more sophisticated ways to do this, but your simplest option is a loop.
//Loop through each banned word, and check if it's in the file
for (int i = 0; i < 8; i++)
{
if (fileContents.find(bannedwords[i]) != std::string::npos)
{
//Do whatever
}
}
Obviously you'll need to do the find a little differently if you want to count the number of occurrences, but that's another question.

How to read a txt file with characters (without spacing) and store into a 2D character array with 15x15 in visual c++?

I'm kind of a newbie in C++ programming.. My command prompt output is a big bulk (repeated) of the characters I have in my txt file. I create a 2d array map[15][15] and try to read the txt file. the reading part is ok but now I dunno how to put them in a 2D character array..
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char map[15][15];
int alp = 0;
int i = 0;
int main()
{
ifstream in;
//string s;
in.open("city.txt");
if(!in.is_open())
{
cout << "File open error! " << endl;
}
else
{
while(!in.eof())
{
//getline(in, s);
in >> map[i];
i++;
alp++;
//if(in.eof()) break;
cout << map[i] << endl;
}
}
for(i = 0; i <= alp; i++)
{
cout << map[i];
}
in.close();
return 0;
}
eof() will only return true only after the first failed read operation. That operation will not be caught by your code. You could have a test for eof directly after the read and then break if eof(), but that's not elegant:
IO operations on streams return a ref to the given stream. Streams have a meaningful conversion to bool. True indicates that the read was successful, e.g. eof wasn't reached yet, and the read target contains a new correct input value. The idiomatic way of using this feature is "while(in >> map[i])".
As to the algorithm: You say that there are no spaces and I assume it's all ascii chars, so it boils down to double looping over the array's lines and columns with two for loops. Inside those loops would be a line reading each character explicitly with get() like
if(!cin.get(map[i][j])) {/* unexpected eof/io error, abort or whatever */ }
As the data is textual, and separated into lines, I suggest you read directly into the sub-array using e.g. std:istream::getline:
for (size_t i = 0; in.getline(map[i], 15); ++i)
;

output/input mutiple files to desktop

working on some code that will place (output/input) 5 different files onto my desktop. Finally, got it down to one error message which is "error <23>: C2109: subscript an array or pointer type is required". Its with myfile.open; I've tried -> operator. Not exactly how to make this into an array, if that is what I'm suppose to do as I have tried making string into char and warnings occur. Can anybody let me know how I can modify my code to correct this? I'm relatively new to C++ and programming, only a few months.
#include <iostream>
#include <fstream>
using namespace std;
struct pizza{
string FILENAMES[9];
};
int main ()
{
int i;
char FILENAMES;
pizza greg = {"file1.doc", "file2.doc", "file3.doc", "file4.doc", "file5.doc"};
cout << "Input is invalid. Program will end. " << "\n" ;
for (i = 0; i < 5; i++)
{
const char *path="/Desktop/Libraries/Documents" ;
ofstream myfile(path);
myfile.open (FILENAMES[i]) ;
myfile << "How you like math?\n" ;
myfile.close();
};
return 0;
}
Your suggestions helped a lot, and my program is now up and running. (no pun intended, haha.)
The loop should really look something like this:
const char *path="/Desktop/Libraries/Documents";
for (int i = 0; i < 5; ++i)
{
std::string name(path + greg.FILENAMES[i]);
std::ofstream myfile(name.c_str());
if (myfile) {
myfile << "How you like math?\n" ;
}
else {
std::cerr << "ERROR: failed to open '" << name << "' for writing\n";
}
}
char FILENAMES;
FILENAMES is not an array. Even if it were, you would have to make it an array of strings or a two dimensional array of characters to do what you intend here.
What you probably intend to do is access the field inside greg.
myfile.open (greg.FILENAMES[i]);