Customer Data program Problem
Hey guys, I need help figuring out where the problem with this program is. I am using an IDE(dev-c++) and I get a really odd message that I can't figure out. The message says :
"This application had requested the Runtime to terminate in an unusual way. Please contact the application's support team for more information."
Here's the main:
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "CustomerData.h"
using namespace std;
int main(int argc, char *argv[])
{
fstream file;
file.open("test.txt", ios::in);
while(!file.eof()){
CustomerData cd;
try{
file>>cd;
cout<<cd;
}catch(string m){
cout<<m;
}catch(...){
if(file.eof())
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
here's the header file:
Code:
//Creating a customer data class
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#ifndef __CUSTOMERDATA
#define __CUSTOMERDATA
class CustomerData;
ostream &operator<<(ostream &ostr, CustomerData cd);
istream &operator>>(istream &istr, CustomerData cd);
class CustomerData{
private:
string cusID, compName, ownerName, contactPos, contactName, address,
city, state, zip, phone;
public:
string getCusID();
string getCompName();
string getOwnerName();
string getContactPos();
string getContactName();
string getAddress();
string getCity();
string getState();
string getZip();
string getPhone();
void setCusID(string)throw (string);
void setCompName(string)throw (string);
void setOwnerName(string, string)throw (string);
void setContactPos(string)throw (string);
void setContactName(string, string)throw (string);
void setAddress(string)throw (string);
void setCity(string)throw (string);
void setState(string)throw (string);
void setZip(string)throw (string);
void setPhone(string)throw (string);
CustomerData();
virtual ~CustomerData();
vector<string> vectorID;
};
#endif
and the cpp file:
Code:
//Mike Webster
//4.27.09
//CS 302
//Creating a customer data class
#include "CustomerData.h"
#include <iostream>
#include <string>
using namespace std;
ostream &operator<<(ostream &ostr, CustomerData cd){
ostr<<cd.getCusID()<<endl;
ostr<<cd.getCompName()<<endl;
ostr<<cd.getOwnerName()<<endl;
ostr<<cd.getContactPos()<<endl;
ostr<<cd.getContactName()<<endl;
ostr<<cd.getAddress()<<endl;
ostr<<cd.getCity()<<endl;
ostr<<cd.getState()<<endl;
ostr<<cd.getZip()<<endl;
ostr<<cd.getPhone()<<endl;
return ostr;
};
istream &operator>>(istream &istr, CustomerData cd){
string temp = "";
string temp2 = "";
char temp3[256];
istr>>temp;
cd.setCusID(temp);
temp = "";
istr>>temp;
cd.setCompName(temp);
temp = "";
istr>>temp;
istr>>temp2;
cd.setOwnerName(temp, temp2);
temp = "";
istr.getline(temp3, 256);
temp = temp3;
cd.setContactPos(temp);
temp = "";
istr>>temp;
istr>>temp2;
cd.setContactName(temp, temp2);
temp = "";
istr>>temp;
cd.setAddress(temp);
temp = "";
istr>>temp;
cd.setCity(temp);
temp = "";
istr>>temp;
cd.setState(temp);
temp = "";
istr>>temp;
cd.setZip(temp);
temp = "";
istr>>temp;
cd.setPhone(temp);
temp = "";
return istr;
};
CustomerData::CustomerData(){
setCusID("DEFAULT");
setCompName("DEFAULT");
setOwnerName("DEFAULT", "DEFAULT");
setContactPos("DEFAULT");
setContactName("DEFAULT","DEFAULT");
setAddress("DEFAULT");
setCity("DEFAULT");
setState("DF");
setZip("00000");
setPhone("(555)555-5555");
vectorID.clear();
};
void CustomerData::setCusID(string temp)throw (string){
bool flag = false;
string temp2;
for(int i = 0; i< vectorID.size(); i++){
if(vectorID[i]==temp)
flag = true;
}
if(flag)
throw("Non-Unique ID");
else if(temp.length()>12)
throw("ID too long");
else if(temp.length()<2)
throw("ID too short");
else
cusID = temp;
};
CustomerData::~CustomerData(){};
void CustomerData::setCompName(string temp)throw (string){
if(temp.length()>59)
throw("CompName too long");
else if(temp.length()<1)
throw("CompName too short");
else
compName = temp;
};
void CustomerData::setOwnerName(string temp, string temp2)throw (string){
if(temp.length() < 1 || temp2.length() < 1)
throw("Incorrectly formatted owner Name");
else
ownerName = temp;
};
void CustomerData::setContactPos(string temp)throw (string){
if(temp.length()<1)
throw("Nonexistent contact pos");
else
contactPos = temp;
};
void CustomerData::setContactName(string temp, string temp2)throw (string){
if(temp.length() < 1 || temp2.length() < 1)
throw("Incorrectly Formatted ContactName");
else
contactName = temp;
};
void CustomerData::setAddress(string temp)throw (string){
if(temp.length() < 1)
throw("Nonexistent Address");
else
address = temp;
};
void CustomerData::setCity(string temp)throw (string){
if(temp.length() < 1)
throw("Nonexistent City");
else
city = temp;
};
void CustomerData::setState(string temp)throw (string){
if(temp.length() != 2)
throw("Incorrectly Formatted State");
else
state = temp;
};
void CustomerData::setZip(string temp)throw (string){
if(temp.length() != 5)
throw("Incorrectly Formatted Zipcode");
else
zip = temp;
};
void CustomerData::setPhone(string temp)throw (string){
if(temp.length() != 13)
throw("Incorrectly Formatted Phone Number");
else
phone = temp;
};
string CustomerData::getCusID(){
return cusID;
};
string CustomerData::getCompName(){
return compName;
};
string CustomerData::getOwnerName(){
return ownerName;
};
string CustomerData::getContactPos(){
return contactPos;
};
string CustomerData::getContactName(){
return contactName;
};
string CustomerData::getAddress(){
return address;
};
string CustomerData::getCity(){
return city;
};
string CustomerData::getState(){
return state;
};
string CustomerData::getZip(){
return zip;
};
string CustomerData::getPhone(){
return phone;
};
Please help, I am really stuck on this one and my deadline is monday!
Any help is greatly appreciated!
More Customer Data Help!!
Ok, I asked for help before but now I have one last problem.
My program reads the files correctly (finally) but it returns the values back to the default values somewhere between the istream and the ostream and I can't figure out where or why.
Here's my code:
main:
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "CustomerData.h"
using namespace std;
int main(int argc, char *argv[])
{
fstream file;
fstream ofile;
file.open("testFile.txt", ios::in);
ofile.open("newfile.txt", ios::out);
CustomerData cd;
cout<<cd;
while(!file.eof()){
try{
file>>cd;
cout<<cd;
ofile<<cd;
cout<<cd;
}catch(string m){
cout<<m;
break;
}catch(...){
if(file.eof())
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
CustomerData.h :
Code:
//Creating a customer data class
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#ifndef __CUSTOMERDATA
#define __CUSTOMERDATA
class CustomerData;
ostream &operator<<(ostream &ostr, CustomerData cd);
istream &operator>>(istream &istr, CustomerData cd);
class CustomerData{
private:
string cusID, compName, ownerName, contactPos, contactName, address,
city, state, zip, phone;
public:
string getCusID();
string getCompName();
string getOwnerName();
string getContactPos();
string getContactName();
string getAddress();
string getCity();
string getState();
string getZip();
string getPhone();
void setCusID(string)throw (string);
void setCompName(string)throw (string);
void setOwnerName(string)throw (string);
void setContactPos(string)throw (string);
void setContactName(string)throw (string);
void setAddress(string)throw (string);
void setCity(string)throw (string);
void setState(string)throw (string);
void setZip(string)throw (string);
void setPhone(string)throw (string);
bool checkForCusID(string temp);
CustomerData();
virtual ~CustomerData();
vector<string> vectorID;
};
#endif
CustomerData.cpp :
Code:
//Mike Webster
//4.27.09
//CS 302
//Creating a customer data class
#include "CustomerData.h"
#include <iostream>
#include <string>
using namespace std;
ostream &operator<<(ostream &ostr, CustomerData cd){
ostr<<cd.getCusID()<<endl;
ostr<<cd.getCompName()<<endl;
ostr<<cd.getOwnerName()<<endl;
ostr<<cd.getContactPos()<<endl;
ostr<<cd.getContactName()<<endl;
ostr<<cd.getAddress()<<endl;
ostr<<cd.getCity()<<endl;
ostr<<cd.getState()<<endl;
ostr<<cd.getZip()<<endl;
ostr<<cd.getPhone()<<endl;
return ostr;
};
istream &operator>>(istream &istr, CustomerData cd){
string temp = "";
string temp2 = "";
char temp3[256];
istr>>temp;
cd.setCusID(temp);
temp = "";
istr>>ws;
istr.getline(temp3, 256);
temp = temp3;
cd.setCompName(temp);
temp = "";
istr.getline(temp3, 256);
temp = temp3;
cd.setOwnerName(temp);
temp = "";
istr >> ws;
istr.getline(temp3, 256);
temp = temp3;
cd.setContactPos(temp);
istr >> ws;
temp = "";
istr.getline(temp3, 256);
temp = temp3;
cd.setContactName(temp);
temp = "";
istr >> ws;
istr.getline(temp3, 256);
temp = temp3;
cd.setAddress(temp);
temp = "";
istr>>temp;
cd.setCity(temp);
temp = "";
istr>>temp;
cd.setState(temp);
temp = "";
istr>>temp;
cd.setZip(temp);
temp = "";
istr>>temp;
cd.setPhone(temp);
temp = "";
return istr;
};
CustomerData::CustomerData(){
setCusID("DEFAULT");
setCompName("DEFAULT");
setOwnerName("DEFAULT, DEFAULT");
setContactPos("DEFAULT");
setContactName("DEFAULT, DEFAULT");
setAddress("DEFAULT");
setCity("DEFAULT");
setState("DF");
setZip("00000");
setPhone("(555)555-5555");
vectorID.clear();
};
void CustomerData::setCusID(string temp)throw (string){
cout<<temp.length()<<endl;
bool flag = false;
for(int i = 0; i< vectorID.size(); i++){
if(vectorID[i]==temp)
flag = true;
}
if(flag)
throw string("Non-Unique ID\n");
else if(temp.length()>12)
throw string("ID too long\n");
else if(temp.length()<2)
throw string("ID too short\n");
else{
cusID = temp;
vectorID.push_back(temp);
}
};
bool CustomerData::checkForCusID(string temp){
bool flag = false;
for(int i = 0; i< vectorID.size(); i++){
if(vectorID[i]==temp)
return true;
}
return false;
};
CustomerData::~CustomerData(){};
void CustomerData::setCompName(string temp)throw (string){
if(temp.length()>59)
throw string("CompName too long\n");
else if(temp.length()<1)
throw string("CompName too short\n");
else
compName = temp;
};
void CustomerData::setOwnerName(string temp)throw (string){
if(temp.length() < 1)
throw string("Incorrectly formatted owner Name\n");
else
ownerName = temp;
};
void CustomerData::setContactPos(string temp)throw (string){
if(temp.length()<1)
throw string("Nonexistent contact pos\n");
else
contactPos = temp;
};
void CustomerData::setContactName(string temp)throw (string){
if(temp.length() < 1)
throw string("Incorrectly Formatted ContactName\n");
else
contactName = temp;
};
void CustomerData::setAddress(string temp)throw (string){
if(temp.length() < 1)
throw string("Nonexistent Address\n");
else
address = temp;
};
void CustomerData::setCity(string temp)throw (string){
if(temp.length() < 1)
throw string("Nonexistent City\n");
else
city = temp;
};
void CustomerData::setState(string temp)throw (string){
if(temp.length() != 2)
throw string("Incorrectly Formatted State\n");
else
state = temp;
};
void CustomerData::setZip(string temp)throw (string){
if(temp.length() != 5)
throw string("Incorrectly Formatted Zipcode\n");
else
zip = temp;
};
void CustomerData::setPhone(string temp)throw (string){
if(temp.length() != 13)
throw string("Incorrectly Formatted Phone Number\n");
else
phone = temp;
};
string CustomerData::getCusID(){
return cusID;
};
string CustomerData::getCompName(){
return compName;
};
string CustomerData::getOwnerName(){
return ownerName;
};
string CustomerData::getContactPos(){
return contactPos;
};
string CustomerData::getContactName(){
return contactName;
};
string CustomerData::getAddress(){
return address;
};
string CustomerData::getCity(){
return city;
};
string CustomerData::getState(){
return state;
};
string CustomerData::getZip(){
return zip;
};
string CustomerData::getPhone(){
return phone;
};
Any help appreciated.