Solved

proper order in a program using classes

Posted on 2004-10-29
173 Views
Last Modified: 2012-05-05
I have a problem with arrangement on a program. I can't seem to put the pieces in the correct order.  I want to keep it short so here is a code snippet.  the program reads in an input file, does some sanity checks on the variables and outputs to the outfile. I have only included one of the variables(title).  I'm new to this game, please be considerate.  
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
const int max = 10;
int count = -1;
int i,j;
string data [max];
string colon = ":";
ifstream infile;
ofstream outfile;

class Address{
public:
string title,newstring;
      
public:
Address () {
title = "Mr. or Mrs.";   //default constructor
      }
Address (string myTitle) {
               setTitle(myTitle) ;
                          }

void setTitle (string myTitle) {
if(myTitle == "Mr." || myTitle =="Dr." ||
 myTitle == "Miss" || myTitle =="Mrs." )
title = myTitle;
else
cout<<"you have entered an incorrect title \n";
cout << title;
      }

string gettitle () {
return title;
      }
      };
cout << endl;

int main () {
      infile.open ("strings.txt");
      if (!infile){
      cout <<"Cannot access the input file.\n";
      exit(-1);
      }
      while ( count < max && infile >> data [++ count ]);
      infile.close();

      cout<<"the addresses from the input data file are: " << "\n\n";
      for (i=0;i<count;i++) {
            cout << data[i] << endl;
            j= data[i].find(colon);
            title = data[i].substr(0,j);
            cout << title << endl;
            newstring = data[i].erase(0,j+1);

            j= data[i].find(colon);
            name = data[i].substr(0,j);
            cout<<name<<endl;
            newstring = data[i].erase(0,j+1);

            j= data[i].find(colon);
            street = data[i].substr(0,j);
            cout<<street<<endl;
            newstring = data[i].erase(0,j+1);

            j= data[i].find(colon);
            city = data[i].substr(0,j);
            cout<<city<<endl;
            newstring = data[i].erase(0,j+1);

            j= data[i].find(colon);
            state = data[i].substr(0,j);
            cout<<state<<endl;
            newstring = data[i].erase(0,j+1);

            zip = (0,j);
            cout<<zip<<endl;
            
      }
      outfile.open("outfile.txt");
      if(!outfile) {
      cout<<"cannot open the output file.\n";
      exit(-2);

      for (i=0;i<count;i++)
      outfile << data[i]  << endl;
      outfile.close();

      return 0;
}
0
Question by:police45s
    5 Comments
     
    LVL 30

    Expert Comment

    by:Axter
    Exactly what is your question?
    0
     

    Author Comment

    by:police45s
    putting this puzzle in the proper order so it can run
    0
     
    LVL 30

    Expert Comment

    by:Axter
    That's not a question.

    Your post is vaque and ambiguous.

    Please post a specific question.

    0
     

    Author Comment

    by:police45s
    my question would be the following:  how can the above program be restructured so to allow it to be executed properly?  The confusion presents itself when class address is added.    
    0
     
    LVL 3

    Accepted Solution

    by:
    It at least compiles if you go this route:

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;
    const int MAX = 10;
    int count = -1;
    int i,j;
    string data [MAX];
    string colon = ":";
    ifstream infile;
    ofstream outfile;

    class Address{
    public:
    string title,newstring;
         
    public:
    Address () {
    title = "Mr. or Mrs.";   //default constructor
         }
    Address (string myTitle) {
                   setTitle(myTitle) ;
                             }

    void setTitle (string myTitle) {
    if(myTitle == "Mr." || myTitle =="Dr." ||
     myTitle == "Miss" || myTitle =="Mrs." )
    title = myTitle;
    else
    cout<<"you have entered an incorrect title \n";
    cout << title;
         }

    string gettitle () {
    return title;
         }
         };

    int main () {
         infile.open ("strings.txt");
         if (!infile){
         cout <<"Cannot access the input file.\n";
         exit(-1);
         }
         while ( count < MAX && infile >> data [++ count ]);
         infile.close();

         cout<<"the addresses from the input data file are: " << "\n\n";
         for (i=0;i<count;i++) {
             Address addr;
              cout << data[i] << endl;
              j= data[i].find(colon);
              addr.setTitle(data[i].substr(0,j));
              cout << addr.gettitle() << endl;

             
         }
         outfile.open("outfile.txt");
         if(!outfile) {
         cout<<"cannot open the output file.\n";
         exit(-2);
         }

         for (i=0;i<count;i++)
         outfile << data[i]  << endl;
         outfile.close();

         char ch;
         cin.get(ch);

         return 0;
    }


    Regards
    Hendrik
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    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.
    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.

    875 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

    7 Experts available now in Live!

    Get 1:1 Help Now