-
February 9th, 2010, 12:33 AM
#1
vector of characters
Dear all,
I am a newbie in C++ and having some difficulty storing value in a vector of characters. I am getting error "subscript out of range". Can a kind soul help me understand how to store the values 'id' in the vector 'AA' and access/output it.
I have declared
Code:
int Nlinks, i;
char id[31];
std::vector<int> AA; //Create an empty vector of type int **********
for (i = 1; i <= Nlinks; i++)
{
ENgetlinkid(i,id);
AA.push_back (id[31]);
}
for (i = 0; i < (char)AA.size(); i++)
{
cout << AA[i] << endl;
}
-
February 9th, 2010, 01:08 AM
#2
Re: vector of characters
Originally Posted by RePrasad
Dear all,
I am a newbie in C++ and having some difficulty storing value in a vector of characters. I am getting error "subscript out of range".
What is wrong with this program?
Code:
#include <iostream>
int main()
{
char id[31];
std::cout << id[31];
}
Hint: arrays are accessed starting at index 0 up to n-1, where n is the number of elements. The id array has 31 elements, but how is it being used in the cout statement?
That is the same error you have in your program.
Regards,
Paul McKenzie
-
February 9th, 2010, 03:59 AM
#3
Re: vector of characters
Hi Paul,
First of all I whould describe you what I understood from the function you have coded. I suppose you wanted to read out a link id belonging to a number 1-Nlinks.
It's not clear why have you declared a vector of integer and get back in the function type of character and pushed into the vector of integer.
It's also not clear how many ids you expect to receive from ENgetlinkid. Supposed you need a single integer only I would modify your code the following way.
void ENgetlinkid(int i,int& id); // declared elsewhere
int Nlinks=50, i;
int id;
std::vector<int> AA; //Create an empty vector of type int **********
for (i = 1; i <= Nlinks; i++)
{
ENgetlinkid(i,id);
AA.push_back(id);
}
for (vector<int>::iterator it = vector<int>::begin(); it != vector<int>::end() ;++it)
{
cout << *it << endl;
}
You got an exception because the upper bound of index in char id[31]; is 30.
Best regards.
Janos
-
February 9th, 2010, 04:23 AM
#4
Re: vector of characters
Hi Janos,
The standard format in the software I am using for the coding to get the linkid is
Code:
DLLEXPORT int _stdcall ENgetlinkid(int, char *);
Here, I cannot use This is why I have to use An id with character and numbers (example, Pipe110)
please guide me on how can I fix this one.
Regards,
RePrasad
-
February 9th, 2010, 04:36 AM
#5
Re: vector of characters
The one I mentioned is from the header file (epanet2.h) of the software EPANET
Code:
DLLEXPORT int _stdcall ENgetlinkid(int, char *);
Just an additional clarification.
Regards,
RePrasad
-
February 9th, 2010, 04:52 AM
#6
Re: vector of characters
Originally Posted by RePrasad
Here, I cannot use This is why I have to use
Did you understand my post?
Code:
char x[31]; // an array declaration
//...
// The valid indexes of x are x[0], x[1], x[2], ... x[30].
// There is no such element as x[31].
If you don't understand how to properly access arrays, I suggest you go back and study the basics on arrays. Again, if an array has n elements, arrays are accessed starting at 0 and going to n-1, not n.
Regards,
Paul McKenzie
-
February 9th, 2010, 08:57 AM
#7
Re: vector of characters
Thanks a lot Paul. Pl. have patience. Sometimes there are simple mistakes which wont get caught easily
-
February 9th, 2010, 03:45 PM
#8
Re: vector of characters
Thank you very much for the quick response sir. I think I am trying to do exactly as you thought. I tried changing the storage from
Code:
AA.push_back (id[31]);
to
Code:
AA.push_back(id[i]);
and the cout line of the code accordingly. However I have a condition to add
Code:
if
( fromnode == Nr || tonode == Nr)
AA.push_back(id[i])
and then cout
Also, I have changed the vector type to char i.e.
I am getting weird characters
as output on console on 11 lines. Could you please help me identify and help me understand my mistake.
Regards,
RePrasad
-
February 9th, 2010, 05:09 PM
#9
Re: vector of characters
So your code now looks like this...?
Code:
int Nlinks, i;
char id[31];
std::vector<int> AA; //Create an empty vector of type int **********
for (i = 1; i <= Nlinks; i++)
{
ENgetlinkid(i,id);
AA.push_back (id[i]);
}
Nlinks is just some totally arbitrary number. If it's anything higher than 30 you will (again) be accessing elements that are off the end of the array (i.e. off the end of id[]).
Apart from that, id[] has never been initialised anywhere so it will be a random chunk of memory filled with completely arbitrary data. That's why you're seeing weird characters. Don't assume that your computer knows what you know. If you want it to do something sensible, you need to give it every relevant instruction. You can't miss some of them out and assume it will know what you intended.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 9th, 2010, 05:45 PM
#10
Re: vector of characters
The vector is a vector of characters instead of vector of integers
Code:
int Nlinks, i;
char id[31];
std::vector<char> AA; //Create an empty vector of type char **********
for (i = 1; i <= Nlinks; i++)
{
ENgetlinkid(i,id);
AA.push_back (id[i]);
}
I am extremely sorry but could you please help me understand how to initialize the id[]
would get the id of the link from the network within the loop 1 thru Nlinks (Nlinks is number of links which is obtained earlier)
-
February 9th, 2010, 06:47 PM
#11
Re: vector of characters
Thank you very much,
I was able to initialize the vector. But now it prints blank (shows nothing) Please recommend a quick fix on this one.
-
February 10th, 2010, 01:34 AM
#12
Re: vector of characters
Originally Posted by RePrasad
I am extremely sorry but could you please help me understand how to initialize the id[]
would get the id of the link from the network within the loop 1 thru Nlinks (Nlinks is number of links which is obtained earlier)
Okay, in which case you've left out the most important code which is probably heppening in ENgetlinkid(). Also, do you check anywhere that Nlinks can never be more than 30?
Regarding the blank spaces, nobody can tell you why that's happening because you haven't shown enough code for anyone to be able to tell what's supposed to happen. It looks as if ENgetlinkid() gets an id of some sort - but only one character of that id ever gets pushed back to your vector. Is that what you intend to happen? I strongly suspect that you probably need a multi-dimensional array for id[], rather than the single dimension array that you specified. Or maybe you wanted a vector of strings, rather than a vector of single characters. Maybe you should be using AA.push_back (id); to some other kind of vector. It's impossible to guess because your intentions just aren't clear from the code you've provided.
The bottom line is that if another programmer can't work out what you intended, the computer's got no chance!
Last edited by John E; February 10th, 2010 at 01:54 AM.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 10th, 2010, 09:53 AM
#13
Re: vector of characters
Hello John,
Thank you very much for your step-by-step advise.
Here is the detailed code. What I intend to do is to read a network file and get its linkid "id" for a particular condition where either the starting or ending nodes are equal to root node.
However, after going thru the code it is printing either weird characters or blank. I completely understand that I should have provided the complete code to you.
MY INTEREST HERE IS TO STORE THE LINKS FOR THE CONDITION IN A VECTOR CALLED AA AND THEN ACCESS IT. SINCE I AM LEARNING THIS I AM TAKING MORE TIME THAN NORMAL.
Regards,
RePrasad
HERE IS THE CODE FOR YOUR QUICK REFERENCE
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#include "epanet2.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
fstream SaveFile("C:/tmp/Results.txt");
if ( !SaveFile )
{
cerr << "ERROR: could not open file '" << SaveFile << endl;
throw (-1);
}
char input1[] = "Net1.inp";
char input2[] = "NewNet1.inp";
char output2[] = "NewNet1.rpt";
int Nlinks;
long i;
long t;
long tstep;
int fromnode, tonode;
char id[31];
int Nr;
std::vector<char> AA;
ENopen(input1);
ENgetcount(EN_LINKCOUNT, &Nlinks);
printf("\nEnter the root node:Nr ");
cin >> Nr;
for (i = 1; i <= Nlinks; i++)
{
ENgetlinkid(i,id);
ENgetlinknodes(i,&fromnode,&tonode);
if
( fromnode == Nr || tonode == Nr)
SaveFile << id << endl;
AA.push_back (id[i]);
}
for (i = 0; i < (int)AA.size(); i++)
{
cout << AA[i] << endl;
}
ENcloseQ();
ENreport();
ENclose();
SaveFile.close();
return 0;
}
-
February 10th, 2010, 10:16 AM
#14
Re: vector of characters
The main problem is we do not know what ENgetlinkid(i,id) does.
Assuming the id is returned via a strcpy, Is a char [31] big enough ?
My guess is that John's suggest of using a vector<string> is what you want.
relevant lines that change:
Code:
std::vector<std::string> AA;
//
AA.push_back (id);
-
February 10th, 2010, 02:57 PM
#15
Re: vector of characters
Thank you very much for the analysis Phillip and Paul. Just to bring to your attention. The line corresponding the ENgetlinkid from the file is given below which I cannot make any changes to.
Code:
DLLEXPORT int _stdcall ENgetlinkid(int, char *);
Also given below is a standard format of retrieving id is given just for your reference from the toolkit help. It does not accept vector of string
Code:
ENgetlinkid
int ENgetlinkid( int index, char* id )
Description: Retrieves the ID label of a link with a specified index.
Arguments:
index: link index
id: ID label of link
Returns: Returns an error code.
Notes:
The ID label string should be sized to hold at least 15 characters.
Link indexes are consecutive integers starting from 1.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|