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

x
?
Solved

proper order in a program using classes

Posted on 2004-10-29
5
Medium Priority
?
181 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
[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
  • 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

610 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