Customer Data program Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Customer Data program Problem

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    6

    Exclamation 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!

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    That message means the program called "terminate()" or threw an unhandled exception.

    To find this, run the program in your debugger and it should break for you when the problem happens. I see that your program has some "throw" commands, so very likely the issue is from one of them. Every throw needs a catch, unless you LIKE having the program exit upon error (generally this is not a great plan, but if your system could cause real world damage upon error, sometimes exiting is safer than the unknown. Its still a poor idea, but you see this sometimes, thats what a windows blue or red screen of death really means, windows felt it might damage hardware or the file system and stopped before it could get any worse).
    Last edited by jonnin; 05-08-2009 at 09:31 AM.

  3. #3
    Join Date
    May 2009
    Posts
    6

    Question Throw...Catch question

    Ok, I understand what you're saying, but I am wondering if I just handled the throwing and catching incorrectly. Can I throw an exception from a class and catch it in main? If so, shouldn't catch(...) catch anything thrown? I thought that was what my professor said.....


    Thanks for the response.

  4. #4
    Join Date
    May 2007
    Posts
    843
    Member function allow to throw and catch using exception class.

  5. #5
    Join Date
    Dec 2007
    Posts
    401
    > I am wondering if I just handled the throwing and catching incorrectly.

    No, you have not handled the throw correctly. in all your functions which throw, for example this one, you are violating the exception specification of the function:

    Code:
    // the function promises to throw only std::string
    void CustomerData::setOwnerName(string temp, string temp2)throw (string){
    
        if(temp.length() < 1 || temp2.length() < 1)
            throw("Incorrectly formatted owner Name"); // throws a const char*
            // this violates the exception specification of the function
            // which says nothing other than a std::string would be thrown
            // the implementation calls std::unexpected()
            // the default implementation of std::unexpected() calls std::terminate()
            // and the default implementation of std::terminate() calls std::abort()
        else
            ownerName = temp;
    
    };
    modify throw("Incorrectly formatted owner Name");
    to throw std::string("Incorrectly formatted owner Name");
    unfortunately, this is an error that can not be caught by a typical debugger using normal debugging techniques - you need to debug at the assembly level.

    another error you will encounter, once you have fixed this one is in the function istream &operator>>(istream &istr, CustomerData cd). in these lines:
    Code:
        // ...
        temp = "";
    
        istr>>temp;
        istr>>temp2;
        cd.setOwnerName(temp, temp2);
        temp = "";
        
        istr.getline(temp3, 256);
        // ...
    after istr >> temp2 ; the stream buffer will contain the white-space character which ends the input for temp2. istr.getline(temp3, 256); will now encounter this white-space character in the buffer and treat it as part of the input to be read. modify to:

    Code:
        // ...
        // temp = ""; // not really required
    
        istr>>temp;
        istr>>temp2;
        cd.setOwnerName(temp, temp2);
        temp = "";
        
        istr >> std::ws ; // remove characters from istr till a non-white-space character is encountered
        istr.getline(temp3, 256);
        // ...
    Last edited by vijayan; 05-09-2009 at 10:24 AM.

  6. #6
    Join Date
    May 2009
    Posts
    6

    Another question

    Ok guys thanks for all of your replies, I fixed those errors, but now I have a different problem with my istream. I don't think that it is reading any information in from the filestream that I have in main. It throws the "too short" string every time. If I change the initialization of temp from "" to "Not short" it goes through that just fine, but then the next field with throw it's error, confirming my thought that it's not inputing properly. Any ideas?

  7. #7
    Join Date
    Dec 2007
    Posts
    401
    > ... my thought that it's not inputing properly. Any ideas?

    perhaps, you have not been able to open the file at all. (there is no file "test.txt" in the current directory). and you are testing only for eof() and not bad() etc. make sure that you are able to open the file and read it by writing a small test program:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        const char* const path = "test.txt" ;
        std::ifstream file( path ) ;
        std::string line ;
    
        if( !file ) std::cerr << "error opening file: " << path << '\n' ;
    
        else while( std::getline( file, line ) ) std::cout << line << '\n' ;
    }

  8. #8
    Join Date
    May 2009
    Posts
    6

    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.

Similar Threads

  1. Pseudocode Problem
    By zobi316 in forum VB Classic
    Replies: 3
    Last Post: 03-10-2008, 08:05 AM
  2. Copy Constucted Data Member Problem
    By Paul Skinner in forum C++
    Replies: 1
    Last Post: 12-13-2007, 10:03 PM
  3. Airplane Seating Program Problem
    By Phalax in forum C++
    Replies: 1
    Last Post: 11-22-2007, 09:49 AM
  4. Problem in data report?
    By vkssms in forum VB Classic
    Replies: 1
    Last Post: 10-01-2007, 09:55 AM
  5. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 10:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center