What was wrong with my function because we have to create function to the read that data in, it was a problem with the Initialize function it kept crashing

A line in this code is causing my program to crash and I implemented what you said

What line? We have no way of knowing if you don't tell us.

void readData()
{
    string location, dScript;
    ifstream fin;
    int shelf, row, bin;
    float price;
    int quant, reorder;
    Part item[9][7][12];
    
    fin.open("num.data");
    
    while(!fin.eof())
    {
     fin>>location>>dScript>>price>>quant>>reorder;
     ParseLocation(location, shelf, row, bin);
     item[shelf][row][bin].Initialize(dScript, price, quant, reorder);
    }
    fin.close();

Do you plan on using item[][][] outside of this function? It goes out of scope after this function is done.

my problem seems to be inside the while loop assigning the values of shelf row and bin

Well, you either have garbage-in (does the parsing function receive the proper values?), garbage-out (does it return the proper values?), or something else. You need debugging statements and good, easy-to-check input files to find out for sure. Garbage-in needs to be fixed before garbage-out is fixed. Garbage-out needs to be fixed before "something else" is fixed. Pinpoint the problem rather than speculating. You need to know what the variables SHOULD BE and what they actually are specific times in order to pinpoint what's going wrong.


Everything suggests a scoping issue. The following won't work:

void Read ()
{
    Part item[9][7][12];
    // fill in item[][][] array.
}

int main ()
{
     Read ();
     // do something with item[][][] array
     // the line above is illegal.  item[][][] no longer exists.
}

If you are doing what is directly above, that is a problem. Instead, declare the item[][][] array in main () and pass it to Read () to fill in.

Yes, I am figuring it is a scoping problem

This exact line is causing it to crash and parse function are working correctly as well I used a simple debugging output to check that

item[shelf][row][bin].Initialize(dScript, price, quant, reorder);

I did what you said and it is still crashing the application

Yes, I am figuring it is a scoping problem

This exact line is causing it to crash and parse function are working correctly as well I used a simple debugging output to check that

item[shelf][row][bin].Initialize(dScript, price, quant, reorder);

Well if you did what I posted as an example of the incorrect way to do it in the last post, and this line is in main and the declaration for item[][][] was in your readData() function and you get a compilation error with the word "scope" in it, it's a scoping error. As mentioned, declare item[][][] in main and pass it to the function.

If it's a run-time error like a segmentation fault, your shelf, row, or bin variable likely has a bad value.

Read the error messages carefully. They'll tell you what the problem is.

It is a runtime error, my compiler does not find any errors, I use Dev C++ I am sure If I use something like CC in nix the compiler can possible find something this windows compiler didnt. I declared the array in main and passed it to the function, and upon execution my application crashes, I have placed several output lines for shelf and row and bin before the Initialization and the values are what they should be based of the location number in my file

Which are 090312
030103
Then shelf values = 9, row =3 , and bin = 12 and likewise for the next location I have no idea what it could be I just need to get past this part so I can cruise on forward just to do ill paste my implementation, header, and testing of the class in here

Header File/Class

#ifndef _PARTH_
#define _PARTH_

#include <iostream>
#include <string>
using namespace std;

class Part
{
      public:
             Part() ;
             string GetDescription() const;
             void Initialize(string Script, float Price, int inQOH, int inMRQ);
             
      private:
              string pDscript;
              float uPrice;
              int QOH;
              int MRQ;
};
#endif

Class Implementation

#include <iostream>
#include <string>
#include "part.h"
using namespace std;

Part::Part()
{
  pDscript = "UNUSED";
  uPrice = 0;
  QOH = 0;
  MRQ = 0;
}

void Part::Initialize(string Script, float Price, int inQOH, int inMRQ)
{
     pDscript = Script;
     uPrice = Price;
     QOH = inQOH;
     MRQ = inMRQ;
}

string Part::GetDescription() const
{
  return pDscript;
}

Testing of class

#include <iostream>
#include <fstream>
#include <string>
#include "part.h"
using namespace std;

void readData(Part item[9][7][12]);
void ParseLocation(string location, int& shelf, int& row, int& bin);
int main()
{
   Part item[9][7][12];
   readData(item);
    
    system("Pause");
    return 0;
}

void ParseLocation(string location, int& shelf, int& row, int& bin)
{   
    string temp;
   
    temp = location.substr(0,1) + location.substr(1,1);
    shelf = atoi(temp.c_str()); 
   
    temp = location.substr(2,1) + location.substr(3,1);
    row = atoi(temp.c_str());
    
    temp = location.substr(4,1) + location.substr(5,1);
    bin = atoi(temp.c_str());

    
}

void readData(Part item[9][7][12])
{
    string location, initScript;
    ifstream fin;
    int shelf, row, bin;
    float initPrice;
    int initQOH;
    int initMRQ;
    
    fin.open("num.data");
    
    while(!fin.eof())
    { 
    
     fin>>location>>initScript>>initPrice>>initQOH>>initMRQ;
     ParseLocation(location, shelf, row, bin);     
     item[shelf][row][bin].Initialize(initScript, initPrice, initQOH, initMRQ);
     
     }
     
    fin.close();
    fin.clear();
}

The only thing I can think of it my method of parsing inst liked when I convert it from a c_str to an int? I suppose Ill devise a method using / and % and see whats happens, cant hurt can it

Another update, I implemented a new method in my parse function and added some simple debugging for the shelf, row, bin values when I call the Parse Function everything seems to be in check

void ParseLocation(int location, int& shelf, int& row, int& bin)
{   
     bin = location%100;
     shelf = location/10000;
     row = (location/100)%10;
    
}

My read data function with proper debugging statements and passing the parameter of the class array

void readData(Part item[9][7][12])
{
    ifstream fin;
    int location, initQOH, initMRQ;
    string initScript;
    int shelf, row, bin;
    float initPrice;
    
    fin.open("num.data");
    
    while(!fin.eof())
    { 
    
     fin>>location>>initScript>>initPrice>>initQOH>>initMRQ;
     cout<<location<<endl;
     cout<<endl;
     ParseLocation(location, shelf, row, bin);
     cout<<"shelf: "<<shelf<<endl;
     cout<<"row:"<<row<<endl;
     cout<<"bin:"<<bin<<endl;
     cout<<endl;
     }
     
    fin.close();
}

And once a fackin again when I call the member function Initialize it causes my application to close W..T...F

Which are 090312
030103
Then shelf values = 9, row =3 , and bin = 12 and likewise for the next location

Here's your declaration:

Part item[9][7][12];

That means valid shelf indexes are 0 to 8, valid row indexes are 0 to 6, valid bin indexes are 0 to 11.

So indexes [9][3][12] is a seg fault for shelf and bin since indexes start at 0, not 1. If you are getting file input like:

90312

then it's either a bad file, you need to declare your array to handle larger indexes, or the file assumes indexes starting at 1, not 0, so you have to subtract 1.

But assigning a value to indexes [9][3][12] is a seg fault the way you have it so it's going to be a run-time error. Double check your spec to and find out what assumptions you can make regarding input.

I had to decrement the shelf, row, and bin values by one when read in unless they were zero and used a different compiler and it wouldnt crash anymore, it only crashed when I used Dev C++ works perfect in VS C++

I had to decrement the shelf, row, and bin values by one when read in unless they were zero and used a different compiler and it wouldnt crash anymore, it only crashed when I used Dev C++ works perfect in VS C++

Well, check the specification and make sure you know exactly what is required and what data you are required to be able to handle and what you can assume. Design your program around that. There is no reason that a program written correctly won't work using both compilers. If it crashes in Dev C++, but not in Visual C++, that means there is an error that Visual C++ is letting you get away with, but Dev C++ is not. You need to get it so it works using both compilers.

True, that or that Dev C++ is an out of date no longer maintained compiler, I am going to have to test this assignment out with CC compiler on linux before I turn it in anyway

Tested in both compilers, VS and Dev, runs fine.
Would you like me to post my final product?

Do it. I'm in your CMPS 260 class and I want to compare. =p

In that case no, and you are you?

A google enthusiast. And whatever. I turned it in on Thursday. I've been trying to find ways to narrow it down since then. Hah. Really, I was just interested in seeing how you'd react if I told you that I noticed you were in my class.

Really ? can you supply me with a name?

Why would I do that? That would destroy the fun of being anonymous. And what would you do when you got a hold of my name? You wouldn't recognize my name, anyway.

Your not anon for long.
Your last name is obviously, greene or something of the sorts, all I have to do is sort through the roll list.

Got me. =p So, what're you going to do with my name? Hah. You won't be able to put a face on it.

Nothing I am just curious as to who you are

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.