Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help me diagnose this file i/o error!

Posted on 2004-08-02
9
Medium Priority
?
263 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

Technology Partners: 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

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…
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. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

604 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