Jump to content

Weird problem with code, functions when not suppose to

- - - - -

  • Please log in to reply
No replies to this topic

#1
Guest_Murph-s_*

Guest_Murph-s_*
  • Guests
I am working on a C++ program for my programming 1 class and the instructor said for the swap function that int "a" and "b" should be structs but however when ran through bloodshed the program completes the intended results of the program with no problem or errors.

The program is suppose to accept a chosen txt file, asks the user if they would like to sort the information by age from the struct, asks them their desirable output file and then writes it to that file in order by age from least to greatest along with the people's general info.

My teacher and I are confused about how this can possibly work and if someone gives us a detailed explantaion pertaining to how this can be possible so that we may pass it on to the other programmers in our class it will be greatly apreceated. Thanks for all your efforts!

The people program will be placed at the bottom, which should be saved as a txt file in your own notepad so that you may see that this works without the structs in the swap function.



/* Program - 19
Asks user which txt file they whish to choose,
open and reads that file and stores,
Asks the user if they would like to order the information by age,
if yes, orders by age, asks the user which txt file they whish to write to,
transfers data to the desired file.
Shane Murphy, 3/19/07 */

#include <vector>

#include <cctype>

#include <iostream>

#include <cstdlib>

#include <iomanip>

#include <string>

#include <fstream>

using namespace std;


struct peopleDocument

{

    string firstName;

	string lastName;

	string address;

	string city;

	string state;

	int zipCode;

    string socialSecurity;

	int age;

};//the lines from the txt file is declared in a struct


void openFile(ifstream &infile);

void readfile(istream &infile, vector<peopleDocument> &people);

int openFile(ofstream &outfile);

int sequentialSearch(ofstream &outfile, vector<peopleDocument> &people, string

ssnumber);

int printall(ostream &outfile, vector<peopleDocument> &people);

int printone(ostream &outfile, vector<peopleDocument> &people, int y);

int closeFile(ifstream &infile);

int closeFile(ofstream &outfile);

void swap(int & a, int & b);

int sequentialSearch (const vector<peopleDocument> & people, int low, int high);

void selectionSort(vector<peopleDocument> & names);



int main()//beginning of main

{


    

	vector<peopleDocument>people(1);

	char answer = ' ';

	char goAgain = ' ';


   

    string ssnumber = "";

	ifstream infile;

	ofstream outfile;

	openFile(infile);

	readfile(infile, people);

    

    

    cout<<"Do you want to sort them by age? (y/n)";

    cin>>answer;

    

    if (answer == 'y')

    {

       do

      {     

            

       selectionSort(people);

       

       openFile(outfile);

       printall(outfile, people);

      

       cout<<"\n...completed!"<<endl;

       cout<<endl;

      

       closeFile(infile);

       closeFile(outfile);

      

   	   cout << "Would you like to go again? ";

       cin >> goAgain;

       system("cls"); //clears screen//

        

      } while (goAgain!='n');//Ends do while loop//

      

    }

    

         

         cout<<"\nGood-bye!";

         cout<<endl;

         

         closeFile(infile);

         closeFile(outfile); 

         

         system("pause");

   

   	//sequentialSearch(cout, people, ssnumber)



	return 0;

}//end of main


//Pre:takes in ifstream infile

//Post:

//Descrip:Opens the users desired file from the path

void openFile(ifstream &infile)

{


     string filename="";

     vector<peopleDocument>people(1);



	 cout<<"Please enter the name of an input file: ";

	 cin>>filename;



	 string path="C:\\Documents and Settings\\MurpJ2\\My

Documents\\Programming1\\People programs\\";

	 path += filename;


     infile.open(path.c_str());


}//end of openfile


//Pre:takes in ifstream infile and the vector

//Post:

//Descrip:Reads the data from the file and stores in a struct

void readfile(istream &infile, vector<peopleDocument> &people)

{

  int y=0;

  while(getline(infile,people[y].firstName))

  {


	   getline(infile,people[y].lastName);

	   getline(infile,people[y].address);

	   getline(infile,people[y].city);

       getline(infile,people[y].state);


       infile>>(people[y]).zipCode;

       infile.get();


       getline(infile,people[y].socialSecurity);


       infile>>(people[y]).age;

       infile.get();


       y++;


       people.resize(y+1);


   }


   people.resize(y);



}//end of readfile


//Pre:takes in ofstream outfile

//Post:

//Descrip:Opens the user's desired output file

int openFile(ofstream &outfile)

{

     string filename="";

	 cout<<"Please enter the name of an output file: ";

	 cin>>filename;


	 string path="C:\\Documents and Settings\\MurpJ2\\My

Documents\\Programming1\\People programs\\";

	 path += filename;


     outfile.open(path.c_str());



}//end of openoutputfile


//Pre:takes in ofstream outfile and the vector

//Post:prints all of the information from the function printone to the output

file

//Descrip:Prints all of the data stored form the struct into the desired output

file

int printall(ostream &outfile, vector<peopleDocument> &people)

{

    for(int x=0; x<people.size(); x++)

    {

         printone(outfile, people, x);


    }//end of for loop


}//end of printall function


//Pre:takes in ofstream outfile and the vector

//Post:finds the social security number the user is looking for

//Descrip:Asks the user which social security number they would like to find

finds the social security number the user is looking for an dprints the

information to the screen

int sequentialSearch(ostream &outfile, vector<peopleDocument> &people, string

ssnumber)

{


    bool found = false;


    cout<<"Please enter the social security # you wish to search for: ";

	cin>>ssnumber;

    cout<<endl;




      for(int x=0; x<people.size(); x++)

    {



       if(people[x].socialSecurity == ssnumber)

       {


         printone(outfile, people, x);

         found = true;

         break;



       }




    }//end of for loop


    if(!found)

    {

            cout<<"\nThis is not a valid entry";

            cout<<endl;

            sequentialSearch(cout, people, ssnumber);


    }





}


//Pre:takes in ofstream outfile, the vector and the counter

//Post:prints one set of the data from the struct

//Descrip:Prints one set of the data stored form the struct that will be used by

print all

int printone(ostream &outfile, vector<peopleDocument> &people, int y)

{


       outfile<<people[y].firstName;

	      outfile<<"\n";

       outfile<<people[y].lastName;

       	  outfile<<"\n";

	   outfile<<people[y].address;

	   	  outfile<<"\n";

	   outfile<<people[y].city;

       	  outfile<<"\n";

       outfile<<people[y].state;

       	  outfile<<"\n";

       outfile<<people[y].zipCode;

       	  outfile<<"\n";

       outfile<<people[y].socialSecurity;

     	  outfile<<"\n";

       outfile<<people[y].age;

       	  outfile<<"\n";

}//end of printone function


int closeFile(ifstream &infile)

{


    infile.close();//closes the input file


}//end of closeFile


int closeFile(ofstream &outfile)

{


    outfile.close();//closes the output file


}//end of closeFile



//Pre:takes in the vector

//Post:the ordered vector by the age connected with the rest of their info

//Descrip:Orders vector by the age connected with the rest of their info

void selectionSort(vector<peopleDocument> & people)

{

	int  index = 0;


	for (int i = 0; i < people.size()-1; i++)

	{

        

		index = sequentialSearch(people, i, people.size()-1);

		swap(people[i], people[index]);

        

	}

}


//Pre:takes in int a and b

//Post:the two decided ages are switched 

//Descrip:Changes defined ages

void swap([U]int[/U] & a, [U]int[/U] & b)

{

	int temp;

	temp = a;

	a = b;

	b = temp;

}


//Pre:takes in the vector and int low and high

//Post:the age next and the lowest one it found after that in line in the vector

//Descrip:The next age in line in the vector is selected then it looks through

the rest of the ages and finds and selects the lowest number 

int sequentialSearch (const vector<peopleDocument> & people, int low, int high)

{

	int ind = low;

	

	for (int i = low+1; i <=high; i++)

	{

		if (people[i].age < people[ind].age)

			ind = i;

	}

	return ind;

}



Here is the people.txt file as requested in the program.


Miguel
Hildalgo
8904 Merrill Ln 102
Laurel
MD
20878
123-43-7564
18
Antwon
LeVay
8538 Southlawn Court
Alexandria
VA
22309
235-67-4543
46
William
Wallace
7205 Rollingwood Dr
Chevy Case
MD
20815
235-23-4135
67
Charles
Absher
4119 Military Rd NW
Washington
DC
20015
123-56-3457
34
Deborah
Aceto
5211 Trailway Dr.
Rockville
MD
20853
112-35-813
19
William
Aceto
5211 Trailway Dr
Rockville
MD
20853
123-98-3453
21
Niccolo
Machiavelli
6389 Pageland Lane
Gainesville
VA
20155




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users