Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:54:01+00:00 2026-06-01T12:54:01+00:00

I have an issue that I’m basically baffled by. To start off, I have

  • 0

I have an issue that I’m basically baffled by. To start off, I have two global arrays – trustArray[] and fashionArray[]. Here is the function that fills the trustArray:

void getTrust()
{
    string line;
    int reachedTrust=0;
    int numberOfTrustsRecorded=0;
    ifstream myfile ("BuyingJeans.Hw5 (1).csv");
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line,',');
            //int found=line.find("Like-Purchase");
            if (line=="Trust-Like"){
                reachedTrust=1;
                getline (myfile,line,',');
            }
            if(reachedTrust==1){
                if(numberOfTrustsRecorded <6){
                    double testValue = atof(line.c_str());
                    trustArray[numberOfTrustsRecorded] = testValue;
                    numberOfTrustsRecorded++;
                }
            }
        }
        myfile.close();
    }
    else
        cout << "Unable to open file"; 
}

For some reason, the atof() in this function is changing two of the values in fashionArray[]. If I change the atof() to say, an atoi(), the problem no longer occurs. Here is the method that fills the array that is being changed (fashionArray[]):

void getFashion(){
string line;
int reachedFashion=0;
int numberOfFashionsRecorded=0;
ifstream myfile ("BuyingJeans.Hw5 (1).csv");
if (myfile.is_open())
{
    while ( myfile.good() )
    {
        getline (myfile,line,',');
        if (line=="Like-Fash -->"){
            reachedFashion=1;
            getline (myfile,line,',');
            //cout<<line<<endl;
            //getchar();
        }
        if(reachedFashion==1){
            if(numberOfFashionsRecorded <6){
                fashionArray[numberOfFashionsRecorded] = atoi(line.c_str());
                numberOfFashionsRecorded++;
            }
        }

    }
    myfile.close();
}

else cout << "Unable to open file"; 

}

Here is the main method that calls these two methods:

int main () {

getFashion();
getTrust();

for(int x=0; x<6;x++)
    cout<<fashionArray[x]<<endl;
getchar();
return 0;
}

The first two values of fashionArray end up being changed to some ridiculously large negative and positive integers. One interesting thing is that if I reverse the order in which the two methods are called in the main() method, the issue no longer occurs. Anyone have any idea what could be causing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:54:02+00:00Added an answer on June 1, 2026 at 12:54 pm

    I think you are writing beyond trustArray and into fashionArray. You have not provided initialisation code (please do), but I suppose it looks something like this :

    float trustArray[N];
    float fashionArray[N];
    

    With N equal to some positive integer. My guess is that in your case, N=5.

    In your loop, you test that numberOfTrustsRecorded < 6. Values of numberOfTrustsRecorded that will pass that test are 0, 1, 2, 3, 4, 5. That’s six (6) floats to fit in a array of 5. Writing numberOfTrustRecorded[5] will overwrite memory. Change your test or increase your buffer size to 6.

    Why aren’t you seeing valid values in fashionArray[0] ? I don’t know. Maybe your compiler aligned the fashionArray memory buffer so the overwrite starts in unused memory, leaving you with half the bits required to make a IEEE float. Whatever bits are in memory make up a random float. A memory dump would show the problem.

    Why is running the method in reverse order works ? Probably that the bug is still there, but running getFashion() second cleans the mess left by getTrust(). The memory you overwrite is yours, so as long as you don’t try to make sense of it, nobody complains. Initialize fashionArray[0] = 0.0, run getTrust() and look at fashionArray[0] before running getFashion(). You will probably see the random floats.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have done drag and drop using with jquery. here i have issue that
I have this issue that I can't seem to get my head around... Basically
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue that is driving me a bit nuts: Using a UserProfileManager
I have an issue that seems like very flaky behavour, is this a problem
I have an issue that looks like a race condition with a webview callback
I have an issue that when I leave the android device idle for a
Heyy can somebody help me ? i have this issue that still do know
I have a database issue that i currently cannot wrap my head around with
I have a weird issue that only seems to be affecting IE 7. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.