Solved

Help me diagnose this file i/o error!

Posted on 2004-08-02
9
254 Views
Last Modified: 2010-04-01
My program compiles correctly, but crashes upon execution.. here is the code that is run in the mainline.. The error appears to be in the additem function, but I don't know what's causing it!


//main function
   accountLinklist accounts;
   string uid, bal, type, accountToEdit;
   ifstream infile2("accounts.txt");
      while (!infile2.eof()){
         std::getline(infile2, sin, ',');
         std::getline(infile2, uid, ',');
         std::getline(infile2, bal, ',');
         std::getline(infile2, type, '\n');
         char *acctType = strdup(type.c_str());
         float balance = atof(bal.c_str());
         accounts.additem(sin, uid, balance, *acctType);  //This line generates the error
      }


Here is the listing for the accounts class and the additem function:


struct accountLink{
  string SIN;
  string uID;
  float balance;
  char type;
  accountLink *next;
  virtual void addFunds() = 0;
  virtual void withdrawFunds() = 0;
  virtual void calculateInterest() = 0;
};
class accountLinklist{
private:
  accountLink *first;
  accountLink *realfirst;
public:
  accountLinklist(){
     first = NULL;
     realfirst = first;
  }
  void additem(string s, string id, float b, char t);
  void display();
  void find(string s);
};
void accountLinklist::additem(string s, string id, float b, char t){
  accountLink * newlink = NULL;
  if (t = 's')
     accountLink * newlink = new Savings;
  if (t = 'i')
     accountLink * newlink = new Investment;
  if (t = 'c')
     accountLink * newlink = new Chequing;
  newlink->SIN = s;        //And then stops working here. HELP!!
  newlink->uID = id;
  newlink->balance = b;
  newlink->type = t;
  newlink->next = NULL;
  accountLink *prevptr = NULL;
  accountLink *tempptr = first;
  while (tempptr != NULL){
     if (tempptr->uID > id)
        break;
     prevptr = tempptr;
     tempptr = tempptr->next;
  }
  if (tempptr != NULL)
     newlink->next = tempptr;
  if (prevptr != NULL)
     prevptr->next = newlink;
  else
     first = newlink;
}
0
Comment
Question by:Tabris42
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 125 total points
ID: 11700683
if (t = 's')
if (t = 'i')
if (t = 'c')

Possibly you mean ==

0
 
LVL 4

Expert Comment

by:void_main
ID: 11700979
I bet he does!
@Tabris42: What compiler do you use? My Borland compiler says: "Possibly incorrect assignment" (or something) when I use "="  inside of   "if"


regards
void_main
0
 
LVL 2

Assisted Solution

by:sin_
sin_ earned 125 total points
ID: 11701080
Tabris,

There are a couple of problems...

if ( t = 's' ) indeed is a problem..but there is one more issue that you need to take care of.

see this code:

int main()
{

      char* p = "hello";
      if(*p='c') cout << "hello" << endl;
      return 0;
}

It's doing the same thing that you are doing in your code.

when you say char* p = "hello", it's put into the read only segment of the memory. So it becomes a pointer to const char.

so *p = 'a' will crash the program because this memory is read only.

so do a comparison like that:

*p == 'c'
 
It will work out.


Hope this helps
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 16

Expert Comment

by:nonubik
ID: 11701368
if (t == 's')
     accountLink * newlink = new Savings;
else  if (t == 'i')
     accountLink * newlink = new Investment;
else  if (t == 'c')
     accountLink * newlink = new Chequing;
else return;
0
 

Author Comment

by:Tabris42
ID: 11702596
While that does seem to be the problem.. it doesn't seem to solve the problem. It still crashes either way. Neither the pointer notation or nonubik's syntax prevent the crash!
0
 
LVL 16

Accepted Solution

by:
nonubik earned 250 total points
ID: 11702649
In fact i was wrong:

accountLink * newlink = NULL;

if (t == 's')
     newlink = new Savings;
else  if (t == 'i')
      newlink = new Investment;
else  if (t == 'c')
     newlink = new Chequing;
else return;
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11702672
'cause every 'newlink' inside the if-statement are local variables. So when you pass the 'if's you'll have the newlink NULL pointer again.
hope that's it
0
 

Author Comment

by:Tabris42
ID: 11702718
Ahh.. thanks! You've really pulled me out of a jam here!
0
 
LVL 2

Expert Comment

by:sin_
ID: 11703008
See if this code helps..

#include "stdafx.h"
#pragma warning(disable:4786)
#include <iostream>
#include <string>
using namespace std;

struct accountLink{
  string SIN;
  string uID;
  float balance;
  char type;
  accountLink *next;
  virtual void addFunds() = 0;
  virtual void withdrawFunds() = 0;
  virtual void calculateInterest() = 0;
  virtual ostream& operator<< (ostream& o) {
            o << SIN << uID <<balance << type << endl;
            return o;
      }
            
};

class Savings: public accountLink {

public:
      void addFunds() {};
      void withdrawFunds() {} ;
      void calculateInterest() {};
      ostream& operator<<(ostream& o) {
            o << Savings::SIN << Savings::uID <<Savings::balance << Savings::type << endl;
            return o;
      }

};

class Investment:public accountLink {

public:
      void addFunds() {};
      void withdrawFunds(){}      ;
      void calculateInterest() {} ;
};

class Chequing :public accountLink {
public:
      void addFunds() {};
      void withdrawFunds() {} ;
      void calculateInterest() {} ;
};

class accountLinklist{

private:
  accountLink *first;
  accountLink *realfirst;

public:
  accountLinklist(){
     first = NULL;
     realfirst = first;
  }
  void additem(string s, string id, float b, char t);
  void display();
  void find(string s);
};

void accountLinklist::additem(string s, string id, float b, char t){
 
  accountLink * newlink = NULL;
  if (t == 's')
     newlink = new Savings;
  else if (t == 'i')
     newlink = new Investment;
  else if (t == 'c')
     newlink = new Chequing;
  else
  {
        newlink = NULL;
        //Return..it's a null ptr...it will crash otherwise.
        return ;
  }

 
  newlink->SIN = s;        //And then stops working here. HELP!!
  newlink->uID = id;
  newlink->balance = b;
  newlink->type = t;
  newlink->next = NULL;
  accountLink *prevptr = NULL;
  accountLink *tempptr = first;
 
  while (tempptr != NULL){
     if (atoi((tempptr->uID).c_str()) > atoi(id.c_str())) // > operator isn't overloaded in string..so convert it into an int and then do a comparison.
        break;
     prevptr = tempptr;
     tempptr = tempptr->next;
  }
  if (tempptr != NULL)
     newlink->next = tempptr;
  if (prevptr != NULL)
     prevptr->next = newlink;
  else
     first = newlink;

  //print the class content which got populated.
  cout << newlink;
}

int main()
{

   accountLinklist accounts;
   string sin,uid, bal, type, accountToEdit;
   ifstream infile2("accounts.txt");
      while (!infile2.eof()){
         getline(infile2, sin, ',');
         getline(infile2, uid, ',');
         getline(infile2, bal, ',');
         getline(infile2, type, '\n');
         char *acctType = strdup(type.c_str());
         float balance = atof(bal.c_str());
         accounts.additem(sin, uid, balance, *acctType);  //This line generates the error
      }
      return 0;
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question