C++ :: Read Some Numbers From Input File - Vector Subscript Out Of Range
Nov 3, 2013
My project's goal is to read some numbers from input file,then create a grid.my input file is:
2 3
3 4 5
1 2 4
first line is rows and colums. second and third line the grid location values.My code is
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include<vector>
using namespace std;
[code].....
View 7 Replies
ADVERTISEMENT
Aug 18, 2014
I keep getting this "Debug Assertion Failed" error that says:
expression: vector subscript out of range
I tried to make the loop the same as the vector size, but I'm still getting the same errors.
void Grid::output(ostream & out) {
vector<vector<int>> grid(4);
int rows, columns;
out << " 0 1 2 3 " << endl;
out << " +---------+" << endl;
for( rows=0; rows<grid.size(); ++rows ) // make each row
[code]....
View 3 Replies
View Related
Feb 13, 2013
When i build this program i get no errors, however a message appears saying vector subscript out of range. What does this mean and what can i do to mkae my program work
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <time.h>
#include <vector>
using namespace std;
[Code] .....
View 19 Replies
View Related
Oct 17, 2014
I will try my best to describe this problem I am having because I am not sure if I can reproduce the error in a small code example as this is part of a large code project.
I have a plain std::vector which contains pointers to objects.
When attempting to delete the objects, even if I know for a fact that the std::vector has at least one object in it as shown by the debugger, I get the "Vector Subscript Out of Range" error.
I'll even do a range for loop like so:
for (auto & e : CurrentObjects) {
delete e;
}
And yet I still get the "Vector Subscript Out of Range" error. The destructor is never called for the object represented by "e" so I am not sure why I get the error.
View 4 Replies
View Related
Apr 21, 2014
I have the following code. however, when I debug it gives an error saying" vector subscript out of range"
Vector based mufti-dimensional arrays
Vectors are a STL container that allow you to store pretty much anything in them. When used correctly they can be very powerful containers.
They provide an added benefit that they will automatically remove the memory they use when they go out of scope. This means that objects stored within a vector do not need to be de-allocated (but pointers to objects do). You can also do some interesting things with dynamic multidimensional arrays with vectors.
For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd dimension it's no longer a grid, but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors).
This functionality can be achieved using pointers, but is much harder to do.
#include <iostream>
#include <vector>
#include<conio.h>
using std::vector;
using namespace std;
[code]...
View 8 Replies
View Related
Nov 3, 2013
I've been debugging this program since yesterday and I continue to run into a string subscript error. I pasted the code in a pastebin (it's only 400 lines), to see why I'm getting this. The problem seems to come up during a debug assertion failure.
[URL] ....
View 5 Replies
View Related
Feb 26, 2014
whenever I try to use either <string> or any STL container. Everyone I saw so far, says that "using a .reserve(n)" before adding items to random positions is enough. However, each time I run the code, I still get the same error, unless I actually write the memory with some initial data, and only after access random positions.I am fully aware of the fact that the STL containers and <string> are dynamic data types and memory allocation is done dynamically. However, if I need to allocate all those memory slots before knowing how many I need, would lead me to the same memory complexity as using a char [] array (which is static -- size declaration at first).
how is it possible to keep the <string> dynamic, while being able to add elements on random positions (if possible). I know the strings have the ending char '', but there should still be something that would allow it to work. Okay, long story short, here is my example. I am trying to read from file rows of data. The first char on each row represents a normal char c. The rest of the row is a string which contains numbers (integers between 1 and 250) which represent the position at which the char c (read before) will have its location.
For example the input file:
#include <fstream>
#include <deque> // for later use
#include <string>
#include <sstream>
#include <algorithm> // for later use
[code].....
The program works perfectly, if instead of text.reserve(250); I use text.resize(250);. However, what is the difference between the two? I mean, why isn't reserve enough?
View 3 Replies
View Related
Feb 15, 2015
I've been getting this expression of "subscript being out of range" for my program but i'm not sure how exactly. I'm fiddling around with code, i'm trying to make a two dimensional array of random numbers, here is my code, it compiles just fine:
Code:
#include <iostream>
#include <array>
#include <random>
#include <cstdlib>
#include <iomanip>
[Code] .....
View 1 Replies
View Related
Jun 13, 2014
I am trying to create a simple palindrome program. I get the error string subscript out of range. I believe the error is either in the while or for loops.
#include <cctype>
#include <string>
#include <iostream>
using namespace std;
//cleans string, converts to lower
bool cleanTolower(const string& s);
[Code] ....
View 2 Replies
View Related
Nov 3, 2014
I have an assignment where i have to prompt the user to enter the name of a file then open that file and read names of students and what level they are at university
eg : John Wilkins, sophomore
Dan Robertson, junior
etc..
i did the code and it compiles perfectly, but when i input the name of the file it gives me error: string subscript out of range.
here's the code:
Code:
#include <iostream>
#include <cstring>
#include <string>
#include<ctime>
#include <fstream>
using namespace std;
int * read_file(string filename)
[code]...
View 2 Replies
View Related
Apr 4, 2014
Im having a problem with my sneaky hangman game I feel like im really close just getting an error I can't fix. When I run it, it says something like string subscript out of range when I goggled it. it says im searching past the bounds of what it should be or something.
Also a dictionary.txt file is needed to run the program
code: [URL]
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int guessesUsed = 0;
int wordlength;
[code]....
View 3 Replies
View Related
Nov 11, 2013
I need my program to read a list of numbers from and input file, random.txt, and calculate the following statistics on those numbers:
A. The number of numbers in the file.
B. The sum of all the numbers in the file.
C. The average of all the numbers in the file.
D. The largest number in the file.
E. The smallest number in the file.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
[Code]......
This is what I have so far but I'm not sure if it is right. I can't get the input file to open.
View 3 Replies
View Related
Jun 24, 2014
To get a value I would always use setter and getter. Would it be much better (performance) to use vector subscript operator overloading? And does vector subscription operator overloading require a size for the vector?
vector<int> v(10);
because otherwise, it doesn't work...
View 9 Replies
View Related
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
View Related
Mar 27, 2013
Write a program which reads a stream of numbers from a file, and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() will open both files. Another function named process() must then be called to read all the numbers from the input file and write the positive numbers to the output file. Note that you must pass the open stream variables for each file as arguments to the process() function, and that you need to (always) double check that the files opened successfully before using them.
This is what I have so far but its not working out!
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int process(ifstream &inf, ofstream &outf);
[Code] ....
View 1 Replies
View Related
Nov 7, 2014
void Equipa::alocaDirector (istream &isV){
string _name;
string _age;
string _country;
while (!isV.eof()){
getline(isV, _name);
[Code] .....
I want this function to "copy" the first 3 txt lines to one position in the vector. Example
Directors.txt:
John
45
England
Mark
53
Belgium
With a print function this would be something like:
Name: John
Age: 45
Country: England
and the same for the second guy.
But something really weird is happening. I have this alloc function and then I have in the main() something to print it:
Equipa me1;
ifstream myFile;
string output;
myFile.open("/home/luiscosta/git/AEDA/AEDAP1/DB/MemEqui/Diretores.txt");
//if (myFile.fail())
while (!myFile.eof()) {
getline(myFile, output);
[Code] ....
which calls a new print function:
string MembroEquipa::imprime() const {
stringstream ss;
//ss << nome << ", " << idade << ", " << pais;
[Code] ...
And then I run it, this happens:
Name: 45
Age: 0
Country: Angel Lopez
Name: 50
Age: 0
Country: Armindo Pinto
Name: 43
Age: 0
Country:
Somehow my print function is skipping one line and I can't print anything.
View 6 Replies
View Related
Jul 7, 2014
Input file:
Course Of Sales.csv
Time,Price ($),Volume,Value ($),Condition
10/10/2013 04:57:27 PM,5.81,5000,29050.00,LT XT
10/10/2013 04:48:05 PM,5.81,62728,364449.68,SX XT
10/10/2013 04:10:33 PM,.00,0,.00,
How to read the csv file with my own vector class. I already have date.h,date.cpp,time.h,time.cpp
How to do the maintest and how to create the vector class
I need to show Date, highest price and Start time(s) of the highest share price
View 3 Replies
View Related
Jul 31, 2013
I'm making a simple game and I'm having trouble creating boundaries for the players movements. The map is a 2D vector. When I compare the vector with the players current position sometimes I get an error during run. When the error isn't occurring the behavior of the boundaries is bizarre. The error tells me that the vector is out of range.
Here is the where the map is created. It's within its own class constructor.
vector< vector<char> > map_parts;
map_parts.resize(25);
for ( int i = 0; i < 25; i++ )
{
[Code].....
View 1 Replies
View Related
Jul 18, 2014
I'm trying to do some operator overloading, the function is supposed to add the values at index [i] of two vectors and place the result in the returning vector. The problem is I keep getting a vector out of range. This is the overloaded operator I'm working with (relatively new to these):
vector<float> operator+(const vector<float>& a, const vector<float>& b){
unsigned long size;
vector<float> temp;
if(a.size() >= b.size())
size = a.size();
[Code] .....
and then I would do something like this in the main:
vector<float> v, v1, v2;
v1.push_back(9.1);
...
v2.push_back(8);
...
v = v1 + v2;
but when I try to output the vector v I just get a vector out of range exception.
View 5 Replies
View Related
Dec 18, 2012
Suppose I have a stl vector of ints, and I want to pass a sub-range of that vector as an argument to a function. One way of doing that would be to copy the sub-range, and then pass that copy by reference, as follows:
Code:
#include <vector>
using namespace std;
int MyFunction(vector<int> &a_vector) {
// Do something
return 0;
[Code] ....
However, it can be time-consuming to copy the elements between the two vectors if my_vector is large. So I'm wondering if it is possible to directly pass a sub-range of my_vector by reference, without having to create a new vector and manually copy over all of the relevant elements?
View 4 Replies
View Related
Aug 12, 2012
I'm trying to get my program to read a series of comma delimited values from a file into a vector. However, I am unsure how to actually go about doing this. I've posted my best guess below but it's really just a stab in the dark and I always receive a compiler error.
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
vector <string> v_input;
int main() {
ofstream fout ("Insert File Path Here.txt");
fout << "4, 8, 15, 16, 23, 42";
[Code] ....
View 3 Replies
View Related
Mar 25, 2013
I have code that reads an input file and generates an output file .For reading the input file we have a xml file.If there is an error while reading or writing the output file the an errored file is generated. But in the errrored file the fields are not coming as in accordance with the reader xml . They are coming randomly . In the module for reading and writing the errored file list is being used . What should be done to write in the errored file as the reader xml fields.
View 1 Replies
View Related
Mar 25, 2013
I have code that reads an input file and generates an output file .For reading the input file we have a xml file.If there is an error while reading or writing the output file the an errored file is generated. But in the errrored file the fields are not coming as in accordance with the reader xml .they are coming randomly . In the module for reading and writing the errored file list is being used . What should be done to write in the errored file as the reader xml fields.
View 1 Replies
View Related
May 17, 2014
#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
using namespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");
[Code] .....
I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:
line 33: cout<<m[i][j]<<" ";
View 4 Replies
View Related
Nov 2, 2014
ok so I have three numbers in a file that I called in a function and I want to use all three numbers from that file in another function how can I??
View 9 Replies
View Related
May 23, 2014
I'm implementing an normal_distribution to select objects on a vector. The thing is I can't use values greater then 1 or less then -1. Here is what could be done:
gausx=gaus_distributionx(generator);
while((gausx>1) || (gausx<-1))
gausx=gaus_distributionx(generator);
The question is if the distribution would loose it's power to be a normal distribution, because some of the gerated numbers wouldn't be used. Any way to set ranges for the distribution?
View 6 Replies
View Related