?
Solved

proper order in a program using classes

Posted on 2004-10-29
5
Medium Priority
?
185 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
Comment
Question by:police45s
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12450304
Exactly what is your question?
0
 

Author Comment

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

Expert Comment

by:Axter
ID: 12450365
That's not a question.

Your post is vaque and ambiguous.

Please post a specific question.

0
 

Author Comment

by:police45s
ID: 12450390
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:
HendrikTYR earned 1000 total points
ID: 12450683
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

594 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