473,394 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

<vector> code, perfect on vc++6, broken on g++3.2.2

Hi all,

I have written a simple c++ app, as I'm still learning c++. The
thing works flawlessly on VC++6, but just doesn't work on g++. The
transliterate function which causes the problem is supposed to search
for some characters (2 or 1) and replace them with their equivalent
Arabic characters from an array vector<string>. I have been debugging
it on Linux for like 6 hours, and it's really boring that it works on
vc++.

Anyway, tons of thank yous to anyone that can help me with this one:
PS: I noticed:
arabic.size() returns 0??
if I replace it with a number like 6, it just crashes?!
declarations
------------
std::vector<std::string> arabic; //Array for arabic letters
std::vector<std::string> english; //Array for english letters
std::vector<std::string> comments; //Array for comments (ignored)
code
--------
std::string arabize::transliterate(std::string user_ip){
std::cout << "In arabize Transliterator" << std::endl;
std::string CharOrTwo;
std::string text_out; //Holds the Arabic processed text
bool MatchFound=false;

if (user_ip.length() < 1) { std::cout << "Write some text first!" <<
std::endl; return "";}
for(int i=0; i < user_ip.length() ; ++i){
//Loops over all input characters

if (i < (user_ip.length()-1)){ //If not at last char
CharOrTwo = user_ip.at(i);
CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
MatchFound=true;
text_out += arabic[ctr]; //Put their Arabic equivalent
}
if (MatchFound) {MatchFound=false; i++;continue;}//i++ bec we
worked on 2 chars
}
//Next 2 chars didn't match, or we're at last char.
//So, let's match this char we are at
CharOrTwo = user_ip.at(i);
for(int ctr = 0; ctr < arabic.size(); ctr++) //Again is it in
dictionary
if (CharOrTwo == english[ctr] ){
MatchFound=true;
text_out += arabic[ctr]; //Put their Arabic equivalent
}
if (MatchFound) {MatchFound=false;continue;}
}//Done iterating over all input chars
#ifdef _DEBUG
std::cout << "Input:" << user_ip << std::endl <<
"Output:" << text_out << std::endl;
#endif
return text_out;
}
Jul 22 '05 #1
5 1490
Ahmad wrote:
I have written a simple c++ app, as I'm still learning c++. The
thing works flawlessly on VC++6, but just doesn't work on g++. The
transliterate function which causes the problem is supposed to search
for some characters (2 or 1) and replace them with their equivalent
Arabic characters from an array vector<string>. I have been debugging
it on Linux for like 6 hours, and it's really boring that it works on
vc++.


Please follow the guidelines from the faq:

[5.8] How do I post a question about code that doesn't work correctly?

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Jul 22 '05 #2
OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)

CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?

After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help
Aggro <sp**********@yahoo.com> wrote in message news:<s8***************@read3.inet.fi>...
Ahmad wrote:
I have written a simple c++ app, as I'm still learning c++. The
thing works flawlessly on VC++6, but just doesn't work on g++. The
transliterate function which causes the problem is supposed to search
for some characters (2 or 1) and replace them with their equivalent
Arabic characters from an array vector<string>. I have been debugging
it on Linux for like 6 hours, and it's really boring that it works on
vc++.


Please follow the guidelines from the faq:

[5.8] How do I post a question about code that doesn't work correctly?

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Jul 22 '05 #3
On 6 Mar 2004 07:58:06 -0800, en****@link.net (Ahmad) wrote:
OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)

CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?

After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help


You haven't posted all your code, so I can't tell for sure, but I suspect
you may be getting bitten by the "scope of a variable defined in a for
loop" issue.

In VC6, saying something like:

for (int i = 0; i < whatever; ++i)
{
// whatever
}

is the same as saying:

int i;
for (i = 0; i < whatever; ++i)
{ // whatever
}

IOW, i remains in scope after the end of the loop. gcc probably does it
/right/, which is to say, the first version above is "as if" you wrote:

{
int i;
for (i = 0; i < whatever; ++i)
{ // whatever
}
}

So perhaps you've got a version of your loop variable, "ctr", visible at
the scope outside of the for loop? A global perchance? Just grasping at
straws here...
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
"Ahmad" <en****@link.net> wrote in message
news:30*************************@posting.google.co m...
OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)

CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?

After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help

I guess you went ahead and ignored the posting guidelines. Please post a
*complete*, *compilable*, example that demonstrates the problem.
have been debugging
it on Linux for like 6 hours, and it's really boring that it
works on vc++.


This really doesn't mean anything, that it "works on vc++". There is a
whole bunch of invalid code that I can come up that "works on vc++", but
doesn't take away from the fact that the code could exhibit undefined
behavior when compiled with another compiler.

I see nowhere in the code where you call push_back(), resize(), or construct
the vector objects that would appropriately size them.

#include <vector>
int main()
{
std::vector<int> IV;
IV[0] = 10; // Illegal access

std::vector<int> IV2;
IV2.push_back(10); //OK
}

IV has no element 0, since no room for element 0 was created, so you have an
illegal memory access.

You are just lucky that it worked in Visual C++ *if* the code you posted is
all you are doing with the vector. Also, how do you know it is a problem
with vector, and not something else you are doing in the program that may
have corrupted memory?

This is why you should post *minimal*, *compilable* code that duplicates the
problem.

Paul
Jul 22 '05 #5

"Ahmad" <en****@link.net> wrote in message
news:30*************************@posting.google.co m...
OK, sorry if I was not very clear. Anyway, after playing with gdb, I
think the problem is clearer (though still strange)

CharOrTwo += user_ip.at(i+1); //Take next 2 chars
for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?

After the for loop, ctr has strange values of 100,000+ though it
should be from 0 to 5??
any help


No still not very clear, please follow the guidelines in the FAQ

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

You will get fast accurate help if you follow these guidelines, if you don't
you will get guesses at best.

john
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
6
by: Some Clown | last post by:
Greetings, I'm trying to figure out how to loop through a vector of strings, searching each item as I go for either a boolean condition or a "contains" test. So if my vector is called 'v' I...
2
by: Russ Ford | last post by:
I'm trying to overload the << operator to output vectors, as follows: template <class T> ostream& operator << (ostream& o, const vector<T>& v) { o << "< "; copy (v.begin(), v.end(),...
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
3
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
0
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
1
by: Birthe Gebhardt | last post by:
Dear all, I could not find the way to handle 'not normal' list objects, for example using remove_if, find etc. Example : class Todo { public : .. int getNumber(){ return num_;}
5
by: Stefan.Wagenbrenner | last post by:
Good morning, I'm trying to write a dll using Dev Cpp 4. I'm able to compile the following code, but the linker throws an error and no dll is produced: #include <stdio.h> #include <windows.h>...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.