Solved

Help me diagnose this file i/o error!

Posted on 2004-08-02
9
251 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
Comment Utility
if (t = 's')
if (t = 'i')
if (t = 'c')

Possibly you mean ==

0
 
LVL 4

Expert Comment

by:void_main
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Tabris42
Comment Utility
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
Comment Utility
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
Comment Utility
'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
Comment Utility
Ahh.. thanks! You've really pulled me out of a jam here!
0
 
LVL 2

Expert Comment

by:sin_
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now