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; I need to locate certain strings within a file that I've created and am not sure how to do it. ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    Searching File for String

    I need to locate certain strings within a file that I've created and am not sure how to do it. I need to find, for instance, all players that are from Arizona within this...

    Code:
     Derek , Anderson,0,Oregon State,Baltimore Ravens
     Kyle , Boller,3,California,Baltimore Ravens
     Calvin , Carlyle,1,Oregon State,Baltimore Ravens
     Devard , Darling,2,Washington State,Baltimore Ravens
     Dwan , Edwards,2,Oregon State,Baltimore Ravens
     Todd , Heap,5,Arizona State,Baltimore Ravens
     Pat , Johnson,7,Oregon,Baltimore Ravens
     Chris , McAlister,7,Arizona,Baltimore Ravens
     Edwin , Mulitalo,7,Arizona,Baltimore Ravens
     Jonathan , Ogden,10,UCLA,Baltimore Ravens
     Terrell , Suggs,3,Arizona State,Baltimore Ravens
     Tim , Euhus,2,Oregon State,Buffalo Bills
     Wendell , Hunter,0,California,Buffalo Bills
     Rian , Lindell,6,Washington State,Buffalo Bills
     Lawyer , Milloy,10,Washington,Buffalo Bills
     Ryan , Neufeld,5,UCLA,Buffalo Bills
     Coy , Wire,4,Stanford,Buffalo Bills
     Rashad , Bauman,4,Oregon,Cincinnati Bengals
     Duane , Clemons,10,California,Cincinnati Bengals
     Kori , Dickerson,1,Southern California,Cincinnati Bengals
     T.J. , Houshmandzadeh,5,Oregon State,Cincinnati Bengals
     Chad , Johnson,5,Oregon State,Cincinnati Bengals
     Levi , Jones,4,Arizona State,Cincinnati Bengals
     Deltha , O'Neal,6,California,Cincinnati Bengals
     Carson , Palmer,3,Southern California,Cincinnati Bengals
     Tab , Perry,0,UCLA,Cincinnati Bengals
     Terrell , Roberts,3,Oregon State,Cincinnati Bengals
     Steven , Vieira,0,UCLA,Cincinnati Bengals
     Brant , Boyer,10,Arizona,Cleveland Browns
     Kirk , Chambers,2,Stanford,Cleveland Browns
     Reuben , Droughns,5,Oregon,Cleveland Browns
     Jason , Fisk,11,Stanford,Cleveland Browns
     Amon , Gordon,2,Stanford,Cleveland Browns
     Sultan , McCullough,1,Southern California,Cleveland Browns
     Daylon , McCutcheon,7,Southern California,Cleveland Browns
     Omar , Nazel,1,Southern California,Cleveland Browns
     Dennis , Northcutt,6,Arizona,Cleveland Browns
     Terrelle , Smith,6,Arizona State,Cleveland Browns
     Mason , Unck,2,Arizona State,Cleveland Browns
     Roc , Alexander,2,Washington,Denver Broncos
     Marques , Anderson,4,UCLA,Denver Broncos
     John , Lynch,13,Stanford,Denver Broncos
     Karl , Paymah,0,Washington State,Denver Broncos
     Jake , Plummer,9,Arizona State,Denver Broncos
     Markus , Steele,4,Southern California,Denver Broncos
     Zach , Wilson,1,Southern California,Denver Broncos
     D.D. , Acholonu,1,Washington State,Houston Texans
     Jason , Bell,5,UCLA,Houston Texans
     Mark , Bruener,11,Washington,Houston Texans
     Jerry , DeLoach,5,California,Houston Texans
     Drew , Hodgdon,0,Arizona State,Houston Texans
     Junior , Ioane,5,Arizona State,Houston Texans
     Chris , Johnson,0,Arizona,Houston Texans
     Chris , McKenzie,0,Arizona State,Houston Texans
     Billy , Miller,6,Southern California,Houston Texans
     Zeke , Moreno,5,Southern California,Houston Texans
     Bryan , Pittman,3,Washington,Houston Texans
     Dashon , Polk,6,Arizona,Houston Texans
     Jason , Simmons,8,Arizona State,Houston Texans
     Carlos , Williams,0,Arizona,Houston Texans
     Kailee , Wong,8,Stanford,Houston Texans
     Jason , David,2,Washington State,Indianapolis Colts
     Bryan , Fletcher,1,UCLA,Indianapolis Colts
     Makoa , Freitas,3,Arizona,Indianapolis Colts
     Matt , Giordano,0,California,Indianapolis Colts
     Tarik , Glenn,9,California,Indianapolis Colts
     Larry , Tripplett,4,Washington,Indianapolis Colts
     Troy , Walters,6,Stanford,Indianapolis Colts
     Rich , Alexis,1,Washington,Jacksonville Jaguars
     Marcell , Allmond,1,Southern California,Jacksonville Jaguars
     Khalif , Barnes,0,Washington,Jacksonville Jaguars
     Rob , Meier,6,Washington State,Jacksonville Jaguars
     Lee , Webb,0,Southern California,Jacksonville Jaguars
     Reggie , Williams,2,Washington,Jacksonville Jaguars
     George , Wrighster,3,Oregon,Jacksonville Jaguars
     Scott , Fujita,4,California,Kansas City Chiefs
     Tony , Gonzalez,9,California,Kansas City Chiefs
     Damon , Huard,7,Washington,Kansas City Chiefs
     Sammy , Knight,9,Southern California,Kansas City Chiefs
     Freddie , Mitchell,5,UCLA,Kansas City Chiefs
     Nick , Murphy,2,Arizona State,Kansas City Chiefs
     Samie , Parker,2,Oregon,Kansas City Chiefs
     Junior , Siavii,2,Oregon,Kansas City Chiefs
     Jonathan , Smith,1,Washington State,Kansas City Chiefs
     Will , Svitek,0,Stanford,Kansas City Chiefs
     John , Welbourn,6,California,Kansas City Chiefs
     Brendon , Ayanbadejo,3,UCLA,Miami Dolphins
     Danny , Farmer,4,UCLA,Miami Dolphins
     A.J. , Feeley,3,Oregon,Miami Dolphins
     Alex , Holmes,0,Southern California,Miami Dolphins
     Frank , Middleton,9,Arizona,Miami Dolphins
     Will , Poole,2,Southern California,Miami Dolphins
     Kris , Richard,4,Southern California,Miami Dolphins
     Junior , Seau,16,Southern California,Miami Dolphins
     Chris , Solomona,0,Oregon,Miami Dolphins
     Donnie , Spragan,4,Stanford,Miami Dolphins
     Manuel , Wright,0,Southern California,Miami Dolphins
     Tully , Banta-Cain,3,California,New England Patriots
     Tedy , Bruschi,10,Arizona,New England Patriots
     Matt , Cassel,0,Southern California,New England Patriots
     Corey , Dillon,9,Washington,New England Patriots
     Victor , Leyva,3,Arizona State,New England Patriots
     Wesly , Mallard,4,Oregon,New England Patriots
     Willie , McGinest,12,Southern California,New England Patriots
     Josh , Miller,10,Arizona,New England Patriots
     Chad , Morton,6,Southern California,New England Patriots
     Tom , Sverchek,0,California,New England Patriots
     Andre , Torrey,0,Arizona,New England Patriots
     Jed , Weaver,7,Oregon,New England Patriots
     Erik , Coleman,2,Washington State,New York Jets
     Nnamdi , Asomugha,3,California,Oakland Raiders
     Brad , Badger,9,Stanford,Oakland Raiders
     Justin , Fargas,3,Southern California,Oakland Raiders
     DeLawrence , Grant,4,Oregon State,Oakland Raiders
     Teyo , Johnson,3,Stanford,Oakland Raiders
     Ryan , Riddle,0,California,Oakland Raiders
     Marquis , Smith,4,California,Oakland Raiders
     Marques , Tuiasosopo,4,Washington,Oakland Raiders
     Langston , Walker,4,California,Oakland Raiders
     Andrew , Walter,0,Arizona State,Oakland Raiders
     Chidi , Iwuoma,5,California,Pittsburgh Steelers
     Travis , Kirschke,8,UCLA,Pittsburgh Steelers
     Tommy , Maddox,9,UCLA,Pittsburgh Steelers
     Troy , Polamalu,3,Southern California,Pittsburgh Steelers
     Marvel , Smith,6,Arizona State,Pittsburgh Steelers
     Zach , Tuiasosopo,0,Washington,Pittsburgh Steelers

    i then need to print out the rest of the data contained within the line. This isn't going to be hard, but i'm not sure how to search for the "college" field... here's the rest of my code. I am working on "Option 3" at the bottom...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    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;
    
    	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)
    {
    	playersFile.open("players.txt");
    
    
    	i = atoi(info.yearsExp);
    */
    
    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);
    
    		(pac10[0] == infoPac10.college)
    		cout<<infoPac10.firstName<<infoPac10.lastName<<", "<<infoPac10.yearsExp<<", "<<infoPac10.college<<endl;				
    		 
    		(pac10[1] == infoPac10.college)
    		cout<<infoPac10.firstName<<infoPac10.lastName<<", "<<infoPac10.yearsExp<<", "<<infoPac10.college<<endl;
    	}
    	file_op.close();
    }
    
    
    if (option == 4)
    {
    	int q =0;
    
    	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;
    		q++;
    		}
    	}
    
    	cout<<"Total number is: "<<q<<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();
    }
    	
    cout<<"Number of players with 5 more years of exp. are:  "<<j<<endl;
    
    
    return 0;
    
    }

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Hmm, how about:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
      ifstream ifs("pac10.txt");
      string line;
    
      while (getline(ifs, line)) {
        if (line.find("Arizona") != string::npos)
          cout << line << '\n';
      }
    
      return 0;
    }
    There's no need to break the record into fields if all you're going to do is print the entire record. Unless you're doing some extra formatting and such.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    nah, that doesnt work, because i have to sort all that information by pac10 schools... they will have to go

    Arizona
    Arizona St.
    California
    Oregon
    Oregon St.
    Southern California
    Stanford
    UCLA
    Washington
    Washington St

    Plus, I have to switch the first and last name around so it looks right... so the cout<<line<</n won't work either...

    this is a toughy...

  4. #4
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    also... the "line.find" will find Arizona Cardinals also, and print them out by mistake...

    that's why i use the field infoPac10.college to hold all the info that i need to sort... this is making me very angry!!!

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Okay, so break the line up into fields like you do with the other options. You have a college field, so just test it against the one you're looking for and print all of the fields if you have a match. Since you're sorting the file, you should probably copy each record into an array, then sort with the college as your key. You have all of the pieces in place, just put them together and you're solid.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    I can do that, but how do I do it without an "if" statement... and if i use multiple "if" statements, then it doesnt sort itself according to the college...??? I need to know how to sort the file i think and then store the sorted file... that would make things ALOT simpler...

  7. #7
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    i still can't figure this out... this is getting rediculous....

  8. #8
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    but how do I do it without an "if" statement
    Why can't you use an if statement? That's a stupid restriction, but you can easily solve the problem with the standard library:
    Code:
    #include <algorithm>
    #include <cstring>
    
    bool cmp(pac10& a, pac10& b) {
      return std::strcmp(a.college, b.college);
    }
    
    pac10 list[SOME_SUITABLE_SIZE];
    
    // Fill list with records from the file. You already know how to do this.
    
    std::sort(list, list + SOME_SUITABLE_SIZE, cmp);
    // Done!
    Just because I don't care doesn't mean I don't understand.

  9. #9
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    I must be a total retard, because I can't get this to work... still... i'm so frustrated i could take out the support walls for my house... lol. I give up...

  10. #10
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    b.t.w.... i can use an 'if' statement, but if i do more than one if statement... it takes away the point of it... for instance...

    Code:
    if (pac10[0] == infoPac10.college)
      //write to file or print out entire formatted line
    if (pac10[1] == infoPac10.college)
      //write to file or print out entire formatted line
    it would work if i were only using one if statement, but with multiple if's, it defeats the purpose, because it will mix pac10[0] and pac10[1] together...

    is there a way for this to be avoided????

    so that when i find all Arizona... it is printed to a file... then when i find Arizona State, it is printed after all the Arizona... and so on... instead of it checking everything at one??

  11. #11
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    I give up...
    Good idea. Take a break, take a nap, come back to it in the morning with a fresh mind.
    is there a way for this to be avoided????
    Yes, but I'm beginning to think that you should put this program aside and make another one. A smaller program that just handles option 3, so you don't get confused with everything else that's going on.
    Just because I don't care doesn't mean I don't understand.

  12. #12
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    how about i break my monitors and my hard disk?? lol... what do you mean a smaller program?

  13. #13
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    what do you mean a smaller program?
    Instead of one program with all of the features you want, make several programs that have one feature each. That way you can focus on a single feature at a time. When you get them all working, it's a simple matter of putting them all together into one. Wouldn't you agree that that's better than trying to write the entire program all at once and being totally confused when you have trouble half way into it?
    Just because I don't care doesn't mean I don't understand.

  14. #14
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    Yes, i agree, however; i feel if i am too stupid to have this work in one program, then i am too stupid have it work with multiple programs.. lol... anyways... here's what i have so far if you wanna take another look at it...

    now i'm stuck on putting the data into an array and then sorting the array... i figure that's the best approach... i'm using an exchange sort to sort the data into "pac10 array[5000];"
    (trying to at least)

    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]<array[e];
    			{
    				temp = array[e];
    				array[e] = array [w];
    				array [w] = temp;
    			}
    		}
    	}
    
    
    
    	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;
    
    }

  15. #15
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    i feel if i am too stupid to have this work in one program, then i am too stupid have it work with multiple programs.. lol...
    It seems you're also too stupid to see how dividing a problem into smaller pieces makes it easier to solve. That's a joke, by the way. Please don't be offended because I don't really think you're stupid.
    here's what i have so far if you wanna take another look at it...
    You're on the right track. But my subtle suggestion that breaking the problem down into smaller parts was for my benefit as well as yours. Your code isn't formatted that well, and it's easier to track down specific problems as well as offer code based suggestions if your problem code is short. Neither of us want me to get fired because I was playing with your full program when I should have been working on my own code.
    Just because I don't care doesn't mean I don't understand.

Page 1 of 2 12 LastLast
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, 08:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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