Solved

Help me diagnose this file i/o error!

Posted on 2004-08-02
9
257 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 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
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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

691 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