Searching File for String

This is a discussion on Searching File for String within the C++ Programming forums, part of the General Programming Boards category; In option 3 you appear to read the file with information regarding players in the pac10 conference into an array ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In option 3 you appear to read the file with information regarding players in the pac10 conference into an array of type pac10 called array. Then you appear to be implementing a bubble sort on the array with the critical comparison occuring with the following line:

    if (array[w]<array[e];

    There are three problems with this line. First, you need to remove the semicolon at the end of the lne. Second, you need to put a ) where the semicolon is now. Third, you indicate you want to sort the array based on the college the player attends, but you are comparing an entire pac10 object, not just the appropriate data member of the object. Since you don't overload the < operator for comparing pac10 objects this is bound to fail, even if you didin't have the syntax errors. To access a given data member from a given object in an array of object the syntax is equivalent to this:

    arrayName[desiredIndexNumber].desiredDataMemberName


    or in this case:

    array[x].college

    will get you the college data member of a pac10 object


    The current set up has the college data member of a pac10 object as type char[], so you will need to use strcmp(), or similar, function from cstring (or string.h if you must) header file. It would probably be easier to change all the data members in all of the type declarations from char[] to STL::strings because the < operator is implemented for STL::strings. You can convert char[] strings to STL::strings and you can use STL::strings as a char[] strings if you want, or need, to, but trying to avoid mixing them in the same program, if you can, is probably a good idea.
    Last edited by elad; 09-08-2005 at 09:23 AM.
    You're only born perfect.

  2. #17
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    it's not liking the instance of "pac10 temp;" in "option 3".... this is getting really confusing... i took the last suggestions of changing the 'sort' in option3, but it still doesnt work and i'm not sure why... can someone please tell me what the heck i'm doing wrong...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    
    
    struct initInfo
    {
    	char number [5];
    	char lastName [20];
    	char firstName [20];
    	char position [5];
    	char yearsExp [5];
    	char college [20];
    	char nflTeam [30];
    
    };
    
    struct pac10
    {
    	char firstName [20];
    	char lastName [20];
    	char yearsExp [5];
    	char college [30];
    	char nflTeam [50];
    };
    
    struct big10
    {
    	char firstName [20];
    	char lastName [20];
    	char yearsExp [5];
    	char college [30];
    	char nflTeam [50];
    };
    
    
    
    
    int main()
    {
    
    
    	fstream file_op("pac10.txt", ios::out);
    	fstream file_op1("big10.txt", ios::out);
    
    
    	ifstream playersFile;
    
    	
    	initInfo info;
    	pac10 infoPac10;
    	big10 infoBig10;
    
    	int option;
    	int i, p;
    	int j = 0;
    
    	pac10 array[5000];
    
    	string nfl[32];
    	nfl [0] = "Baltimore Ravens";
    	nfl [1] = "Buffalo Bills";
    	nfl [2] = "Cincinnati Bengals";
    	nfl [3] = "Cleveland Browns";
    	nfl [4] = "Denver Broncos";
    	nfl [5] = "Houston Texans";
    	nfl [6] = "Indianapolis Colts";
    	nfl [7] = "Jacksonville Jaguars";
    	nfl [8] = "Kansas City Chiefs";
    	nfl [9] = "Miami Dolphins";
    	nfl [10] = "New England Patriots";
    	nfl [11] = "New York Jets";
    	nfl [12] = "Oakland Raiders";
    	nfl [13] = "Pittsburgh Steelers";
    	nfl [14] = "San Diego Chargers";
    	nfl [15] = "Tennessee Titans";
    	nfl [16] = "Arizona Cardinals";
    	nfl [17] = "Atlanta Falcons";
    	nfl [18] = "Carolina Panthers";
    	nfl [19] = "Chicago Bears";
    	nfl [20] = "Dallas Cowboys";
    	nfl [21] = "Detroit Lions";
    	nfl [22] = "Green Bay Packers";
    	nfl [23] = "Minnesota Vikings";
    	nfl [24] = "New Orleans Saints";
    	nfl [25] = "New York Giants";
    	nfl [26] = "Philadelphia Eagles";
    	nfl [27] = "San Francisco 49ers";
    	nfl [28] = "Seattle Seahawks";
    	nfl [29] = "St. Louis Rams";
    	nfl [30] = "Tampa Bay Buccaneers";
    	nfl [31] = "Washington Redskins";
    	
    	string pac10[10];
    	pac10 [0] = "Arizona";
    	pac10 [1] = "Arizona State";
    	pac10 [2] = "California";
    	pac10 [3] = "Oregon";
    	pac10 [4] = "Oregon State";
    	pac10 [5] = "Southern California";
    	pac10 [6] = "Stanford";
    	pac10 [7] = "UCLA";
    	pac10 [8] = "Washington";
    	pac10 [9] = "Washington State"; 
    
    	string big10[11];
    	big10 [0] = "Illinois";
    	big10 [1] = "Indiana";
    	big10 [2] = "Iowa";
    	big10 [3] = "Michigan";
    	big10 [4] = "Michigan State";
    	big10 [5] = "Minnesota";	
    	big10 [6] = "Northwestern";	
    	big10 [7] = "Ohio State";	
    	big10 [8] = "Penn State";
    	big10 [9] = "Purdue";
    	big10 [10] = "Wisconsin";
    
    
    	playersFile.open("players.txt");
    
    	if (playersFile.fail())
    	{
    		cout<<"An error occured while trying to open the file."<<endl;
    		system ("PAUSE");
    		exit (1);
    	}
    
    	while (!playersFile.eof())
    	{
    
    		playersFile.getline(info.number, 11, ',');
    		playersFile.getline(info.lastName, 20,  ',');
    		playersFile.getline(info.firstName, 20, ',');
    		playersFile.getline(info.position, 20, ',');
    		playersFile.getline(info.yearsExp, 5, ',');
    		playersFile.getline(info.college, 50, ',');
    		playersFile.getline(info.nflTeam, 40);
    
    		for (int n=0; n<10; n++)
    		{
    			if (pac10[n]==info.college)
    				file_op<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    			
    		for (int z=0; z<10; z++)
    		{
    			if (big10[z]==info.college)
    				file_op1<<info.firstName<<","<<info.lastName<<","<<info.yearsExp<<","<<info.college<<","<<info.nflTeam<<endl;
    		}
    
    	
    	}
    	
    	playersFile.close();
    	file_op.close();
    	file_op1.close();
    	
    cout<<"Enter an option number between 1 and 7: ";
    cin>>option;
    
    while (option > 7)
    {
    	cout<<"Option must be lower than 7..."<<endl;
    	cout<<"Try it again... Enter an option between 1 and 7: ";
    	cin>>option;
    }
    
    while (option < 1)
    {
    	cout<<"There is no option lower than 1..."<<endl;
    	cout<<"Try it again... Enter an option between 1 and 7: "<<endl;
    	cin>>option;
    }
    
    
    
    if (option == 1)
    {
    	
    	cout<<"Baltimore Ravens percentage is: "<<(23.0/84)*100<<"%"<<endl;
    	cout<<"Buffalo Bills percentage is: "<<(19.0/72)*100<<"%"<<endl;
    	cout<<"Cincinnati Bengals percentage is: "<<(22.0/80)*100<<"%"<<endl;
    	cout<<"Cleveland Browns percentage is: "<<(25.0/87)*100<<"%"<<endl;
    	cout<<"Denver Broncos percentage is: "<<(34.0/87)*100<<"%"<<endl;
    	cout<<"Houston Texans percentage is: "<<(30.0/90)*100<<"%"<<endl;
    	cout<<"Indianapolis Colts percentage is: "<<(16.0/87)*100<<"%"<<endl;
    	cout<<"Jacksonville Jaguars percentage is: "<<(31.0/86)*100<<"%"<<endl;
    	cout<<"Kansas City Chiefs percentage is: "<<(34.0/85)*100<<"%"<<endl;
    	cout<<"Miami Dolphins percentage is: "<<(33.0/89)*100<<"%"<<endl;
    	cout<<"New England Patriots percentage is: "<<(29.0/83)*100<<"%"<<endl;
    	cout<<"New York Jets percentage is: "<<(22.0/80)*100<<"%"<<endl;
    	cout<<"Oakland Raiders percentage is: "<<(26.0/85)*100<<"%"<<endl;
    	cout<<"Pittsburgh Steelers percentage is: "<<(30.0/82)*100<<"%"<<endl;
    	cout<<"San Diego Chargers percentage is: "<<(16.0/87)*100<<"%"<<endl;
    	cout<<"Tennessee Titans percentage is: "<<(11.0/82)*100<<"%"<<endl;
    	cout<<"Arizona Cardinals percentage is: "<<(26.0/76)*100<<"%"<<endl;
    	cout<<"Atlanta Falcons percentage is: "<<(30.0/86)*100<<"%"<<endl;
    	cout<<"Carolina Panthers percentage is: "<<(27.0/86)*100<<"%"<<endl;
    	cout<<"Chicago Bears percentage is: "<<(23.0/86)*100<<"%"<<endl;
    	cout<<"Dallas Cowboys percentage is: "<<(21.0/74)*100<<"%"<<endl;
    	cout<<"Detroit Lions percentage is: "<<(28.0/85)*100<<"%"<<endl;
    	cout<<"Green Bay Packers percentage is: "<<(21.0/81)*100<<"%"<<endl;
    	cout<<"Minnesota Vikings percentage is: "<<(25.0/83)*100<<"%"<<endl;
    	cout<<"New Orleans Saints percentage is: "<<(36.0/86)*100<<"%"<<endl;
    	cout<<"New York Giants percentage is: "<<(24.0/85)*100<<"%"<<endl;
    	cout<<"Philadelphia Eagles percentage is: "<<(23.0/86)*100<<"%"<<endl;
    	cout<<"San Francisco 49ers percentage is: "<<(20.0/83)*100<<"%"<<endl;
    	cout<<"Seattle Seahawks percentage is: "<<(25.0/87)*100<<"%"<<endl;
    	cout<<"St. Louis Rams percentage is: "<<(27.0/80)*100<<"%"<<endl;
    	cout<<"Tampa Bay Buccaneers percentage is: "<<(27.0/89)*100<<"%"<<endl;
    	cout<<"Washington Redskins percentage is: "<<(29.0/84)*100<<"%"<<endl;
    
    }
    
    
    
    if (option == 2)
    {
    	file_op.open("pac10.txt");
    
    	while (!file_op.eof())
    	{
    
    	file_op.getline(infoPac10.firstName, 50, ',');
    	file_op.getline(infoPac10.lastName, 50, ',');
    	file_op.getline(infoPac10.yearsExp, 10, ',');
    	file_op.getline(infoPac10.college, 50, ',');
    	file_op.getline(infoPac10.nflTeam, 50);
    
    	cout<<infoPac10.firstName<<infoPac10.lastName<<", "<<infoPac10.yearsExp<<", "<<infoPac10.college<<endl;
    
    	}
    	file_op.close();
    }
    
    
    if (option == 3)
    {
    	file_op.open("pac10.txt");
    
    	while (!file_op.eof())
    	{
    		
    		file_op.getline(infoPac10.firstName, 50, ',');
    		file_op.getline(infoPac10.lastName, 50, ',');
    		file_op.getline(infoPac10.yearsExp, 10, ',');
    		file_op.getline(infoPac10.college, 50, ',');
    		file_op.getline(infoPac10.nflTeam, 50);
    	
    		for (int t=0; t<3500; t++)
    		{
    			array[t] = infoPac10;
    		}
    
    	}
    	
    	int w, e;
    	pac10 temp;
    	
    	for (w=1; w<3499; w++)
    	{
    		for(e = (w+1); e<3500; e++)
    		{
    			if (array[w].college<array[e].college)
    			{
    				temp.college = array[e].college;
    				array[e].college = array [w].college;
    				array [w].college = temp.college;
    			}
    		}
    	}
    
    
    
    	file_op.close();
    		
    }
    
    
    if (option == 4)
    {
    
    	file_op.open("pac10.txt");
    
    	while (!file_op.eof())
    	{
    
    		file_op.getline(infoPac10.firstName, 50, ',');
    		file_op.getline(infoPac10.lastName, 50, ',');
    		file_op.getline(infoPac10.yearsExp, 10, ',');
    		file_op.getline(infoPac10.college, 50, ',');
    		file_op.getline(infoPac10.nflTeam, 50);
    
    		p = atoi(infoPac10.yearsExp); 
    
    		if (p >= 5)
    		{
    			cout<<infoPac10.firstName<<infoPac10.lastName<<", "<<infoPac10.yearsExp<<", "<<infoPac10.college<<endl;
    			
    		}
    	}
    
    	
    	file_op.close();
    }
    
    
    if (option == 5)
    {
    	file_op1.open("big10.txt");
    	
    	while (!file_op1.eof())
    	{
    		file_op1.getline(infoBig10.firstName, 50, ',');
    		file_op1.getline(infoBig10.lastName, 50, ',');
    		file_op1.getline(infoBig10.yearsExp, 10, ',');
    		file_op1.getline(infoBig10.college, 50, ',');
    		file_op1.getline(infoBig10.nflTeam, 50);
    
    		cout<<infoBig10.firstName<<infoBig10.lastName<<", "<<infoBig10.yearsExp<<", "<<infoBig10.college<<endl;
    	}
    	file_op1.close();
    }
    
    
    if (option == 7)
    {
    	file_op1.open("big10.txt");
    
    	while (!file_op1.eof())
    	{
    
    		file_op1.getline(infoBig10.firstName, 50, ',');
    		file_op1.getline(infoBig10.lastName, 50, ',');
    		file_op1.getline(infoBig10.yearsExp, 10, ',');
    		file_op1.getline(infoBig10.college, 50, ',');
    		file_op1.getline(infoBig10.nflTeam, 50);
    
    		p = atoi(infoBig10.yearsExp); 
    
    		if (p > 5)
    		{
    		cout<<infoBig10.firstName<<infoBig10.lastName<<", "<<infoBig10.yearsExp<<", "<<infoBig10.college<<endl;	
    		}
    	}
    
    	file_op1.close();
    }
    	
    
    
    return 0;
    
    }

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    array[x].college is of type char[] and therefore you cannot compare one array[x].college with another array[x].college using the < operator as you try to do. You will need to replace the comparison with strcmp(), as I said in my last post, or, preferable, IMO, replace all char[] data members in all the struct declarations with STL string data members so array[x].college becomes of type STL string you can compare one array[x].college with another using the < operator.

    EDIT: In addition, you want to compare using the college data member but you want to switch the entire pac10 object, not just the college data member value.
    Last edited by elad; 09-08-2005 at 02:21 PM.
    You're only born perfect.

  4. #19
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    ok... i have this now... and it's still not liking it....

    Code:
    		for (int t=0; t<3500; t++)
    		{
    			array[t] = infoPac10;
    		}
    
    	}
    	
    	
    	for (w=1; w<3499; w++)
    	{
    		for(e = (w+1); e<3500; e++)
    		{
    			if (strcmp(array[w].college,array[e].college)>0)
    			{
    				temp = array[e];
    				array[e] = array[w];
    				array [w] = temp;
    			}
    		}
    	}

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    What's the error message/problem?


    Also, probably not the problem, but should w run from 0 to 3499 rather than 1 to 3499 in the following line:

    for (w=1; w<3499; w++)
    You're only born perfect.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21