?
Solved

Help me diagnose this file i/o error!

Posted on 2004-08-02
9
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 500 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 500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

801 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