Urgent help please

This is a discussion on Urgent help please within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> #include <string> #include <fstream> #include <cstdlib> using namespace std; void ReadPlayerInfo(ifstream& myIn, string NameArray[], int ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    Urgent help please

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    void ReadPlayerInfo(ifstream& myIn, string NameArray[], int idArray[], int& size);
    void ReadProcessBatting (ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size);
    void ProcessRecord(string batRec, float& batAvgRunning, int& walk);
    int SearchPosition(int idArray[], int id[], int size);
    void PrintTable(string NameArray[],int idArray[],float batAvg[],int walkArray[],int pos);
    int FindHigh(float batAvg[], int size);
    int FindLow(float batAvg[], int size);
    
    //define global variables
    float batAvgRunning = 0;
    int size = 20;
    int walk = 0;
    string NameArray[20];
    string batRec;
    int idArray[20];
    int id[20];
    int walkArray[20];
    float batAvg[20];
    
    int main()
    {
    	
    	int i;
    	
    	//Begin opening files
    	string PlayerRecordsFile;
    	string PlayerBattingFile;
    	
    	ifstream myIn;
    	ifstream myIn2;
    	
    	cout << "Enter file name that contains player records: ";
    	cin >> PlayerRecordsFile;
    	myIn.open(PlayerRecordsFile.c_str());
    	
    	while (!myIn)
    	{
    		cout << "Enter file name that contains player records: ";
    		cin >> PlayerRecordsFile;
    		myIn.open(PlayerRecordsFile.c_str());
    	}
    	
    	ReadPlayerInfo(myIn, NameArray, idArray, size);   
    
    	cout << "Enter file name that contains batting data: ";
    	cin >> PlayerBattingFile;
    	myIn2.open(PlayerBattingFile.c_str());
    	
    	while (!myIn2)
    	{
    		cout << "Enter file name that contains batting data: ";
    		cin >> PlayerBattingFile;
    		myIn2.open(PlayerBattingFile.c_str());
    	}
    
    	cout << "Player Name     " << "ID      " << "Batting Average      " <<  "Number of Walks" << endl;
    	cout << "--------------------------------------------------------------------------" << endl;
    	ReadProcessBatting(myIn2, id, batAvg, walkArray, size);
    	
        
    
    	
    
    	myIn.close();
    	myIn.clear();
    	myIn2.close();
    	myIn2.clear();
    	return 0;
    		
    }
    
    void ReadPlayerInfo(ifstream& myIn, string NameArray[], int idArray[], int& size)
    {
    	string playerName;
    	int playerNumber;
    	int i;
    	for (i=0;i<size;i++)
    	{
    		myIn >> playerName;
    		NameArray[i] = playerName;
    		myIn >> playerNumber;
    		idArray[i] = playerNumber;
    	}
    }
    
    void ProcessRecord(string batRec, float& batAvgRunning, int& walk)
    {
    	int x = 0;
    	walk = 0;
    	char choice;
    	float hit = 0;
    	float out = 0;
    	while (x < batRec.length())
    	{
    		choice = batRec[x];
    		x++;
    	
    		switch(choice)
    		{
    			case 'O':
    				out++;
    				break;
    			case 'H':
    				hit++;
    				break;
    			case 'W':
    				walk++;
    				break;
    			default:
    				break;
    		}
    	}
    
    	batAvgRunning = (hit/(out+hit));
    }
    
    void ReadProcessBatting(ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size)
    {
    	int x;
    	string batRec;
    	for (x=0;x<size;x++)
    	{
    		
    		myIn2 >> id[x];
    		getline(myIn2,batRec);
    		ProcessRecord(batRec, batAvgRunning, walk);
    		batAvg[x] = batAvgRunning;
    		walkArray[x] = walk;
    		cout << x;
    	}
    	
    	SearchPosition(idArray, id, size);
    }
    
    int SearchPosition(int idArray[], int id[], int size)
    {
    	int m;
    	int i = 0;
    	int pos;
    	
    	for (m=0;m<size;m++)
    	{
    		i = 0;
    		while (idArray[m] != id[i])
    		{
    			i++;
    		}
    		pos = i;
    		PrintTable(NameArray, idArray, batAvg, walkArray, pos);
    		
    	}
    }
    
    void PrintTable(string NameArray[],int idArray[],float batAvg[],int walkArray[],int pos)
    {
    	int x;
    	
    	cout << NameArray[x] << "     " << idArray[x] << "      " << batAvg[pos] << "     " << walkArray[pos];
    	cout << endl;
    	
    	x++;
    }
    Ok all I get is a segmentation fault.

    I have no idea, this is kicking my butt. The way we are required to do it is stupid in my opinion. But I have to solve this problem please help

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    when do you get the fault though? what operation does it fail on? then that code can be looked at alone, i compiled and ran this ok but then i cant do anything because i dont know what format your files are layed out in so i cant even create a couple of test files,
    with all the arrays you are using it is more than likely going to be that you are writing something out of bounds, can't you step through the file with your debugger?
    perhaps check out the functions containing the while loops, like in searchposition(), these could potentially go over limit as they depend on conditions being met to stop them incrementing, if your conditions are not matched as you expect they could run too long,
    Last edited by rogster001; 12-02-2009 at 03:24 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This looks suspicious:
    Code:
                    i = 0;
    		while (idArray[m] != id[i])
    		{
    			i++;
    		}
    There's a boundary on m in the loop, but not on i.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    From the forum guidelines:
    Tips for Efficient and Successful Posting

    1. Don't use all caps.

    2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    hmmm alright it gets the segmentation error right away.

    I have this function which is called right away when opening second file
    Code:
    void ReadProcessBatting(ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size)
    {
    	int x;
    	string batRec;
    	for (x=0;x<size;x++)
    	{
    		
    		myIn2 >> id[x];
    		getline(myIn2,batRec);
    		ProcessRecord(batRec, batAvgRunning, walk);
    		batAvg[x] = batAvgRunning;
    		walkArray[x] = walk;
    		cout << x;
    	}
    	
    	SearchPosition(idArray, id, size);
    }
    I have the cout << x statement as a test but I do not get any x's output

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Here is what the two data files look like

    playerRecord.dat
    Code:
    Armstrong 3
    Bartlett 2
    Carr 4
    Combs 5
    Davis 47
    Fletcher 14
    Fort 9
    Hale 16
    Hammer 10
    Jones 33
    Kriz 19
    Morris 8
    Riddle 11
    Smith 18
    Snyder 28
    Toler 6
    Trussell 12
    Turner 7
    Walker 15
    Wilson 1
    batting.dat
    Code:
    1 HWOOOHOOWOWOOOHOHOHOHOOOHHOHOHWOHHOOOOOOHOOOHHOOHOHOOOOOWOO
    2 OHOOHOHOWOHHOWOOOOHWHOHHOOHOOOOOOOOWOOHOHOHOOHOOOHOOOHOO
    3 WOOHHOOWOOOOHHOWOOHOOOOHOOOOOWOOOOOOOHOWOOOHOOOOHWOHOOO
    4 HHOOHHOOOWOHWOOHWOHOOHOOOOHOOWHOOHOOHOOHHOOOOOWHOOHHOOOOOWHOOHHOO
    5 HOOHOHOHOWOOOOHWOOOOOOOOOHOOOOHOOOHOOOHOWHOHOOOOOOOOO
    6 OOHOWOOOOOOHWOHOWOOHHOOOHOHOHOOHOHOOHOOOOWOWOOHOOO
    7 OHOOHHOOOOOOOOOOOOHOOOHWOOWHHOOHOWHHWOOOWOWHOOHOOHOOOWO
    8 HOOHOWHOHOOWOOOHOOHOWWHOOHOHOOOWOOOOOOOOOOOHOO
    9 OHOOOOWOHOOWHOOHOOWOHOOHOHOOHOOOOOOWOOHOWOO
    10 OWOOOWOWOOOOOOWOHOOOOWOWOOHOOOHOHHOOOHOOOOH
    11 HOOOOOOOHHWOHHOHOOOOOOWOOOOOOOOOOOOOOHOHOHO
    12 HOOHOWOOOHOOOOHOOOOHOHOHOOHHOOOHOWOOWOOWHHO
    14 WOOHOHOOHOOOHOOHOWHHOOOHOOOHOOOHOHOOHO
    15 WHHOOWOOOOOOOOHOOOOHHOHOOOOWOOOOOWOO
    16 OHOHOHHOOOHOHHOHOHOOOWWOOOOOOOWOO
    18 OHOOOOOOWWOOOHOHOHHOHHOOOHOOOOO
    19 WOOHOHHOOOOOOOOWOOOHOWHOWOOOOH
    28 OOOHOOHOOWOWOOOOOHOO
    33 OOHOOOOHOOOWHOOHOHOOOOOHWOHOOOHOOOOHOOHO
    47 OOOOWOOOOWOOHOOOHHOOOHOHOOHOOOWH

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    If I running it on a C++ compiler and not in UNIX I get the

    cout << x;

    to output like it is supposed to then it stops working.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Ok new update...

    It compiles and runs fine in a windows enviroment. But UNIX is still giving me that segmantation error

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    the debugger shows it crashes the first time it gets into the SearchPosition() function which was spotted as a potential problem earlier, it crashes when >

    i = 1
    pos = 2

    this was the output at the time >

    Enter file name that contains player records: playerRecord.dat
    Enter file name that contains batting data: batting.dat
    Player Name ID Batting Average Number of Walks
    ----------------------------------------------------------------
    012345678910111213141516171819
    also compiling this in codeblocks produced a raft of warnings, you should tidy up a bit
    Last edited by rogster001; 12-02-2009 at 09:35 AM.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Ok awesome thank you. Any ideas on how to fix this? it is very urgent and my brain is fried haha.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    your turn now mate...come on dude, i made a new project, created the two files, got it to compile and stepped through the debugger a couple of times, and gave you info on what was going on when it crashed...you wrote it, you should well be able to sort it out now

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Alright I got it ... almost

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    void ReadPlayerInfo(ifstream& myIn, string nameArray[], int idArray[], int& size);
    void ReadProcessBatting (ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size);
    void ProcessRecord(string batRec, float& batAvgRunning, int& walk);
    int SearchPosition(int idArray[], int id[],int firstID, int& pos);
    void PrintTable(string nameArray[],int idArray[],float batAvg[],int walkArray[], int counter, int pos);
    int FindHigh(float batAvg[], int size);
    int FindLow(float batAvg[], int size);
    
    //define global variables
    
    
    int main()
    {
    	int size = 20;
    	int pos = 0;
    	string nameArray[20];
    	string batRec;
    	int idArray[20];
    	int id[20];
    	int firstID = 0;
    	int walkArray[20];
    	float batAvg[20];
    	int i;
    	
    	//Begin opening files
    	string PlayerRecordsFile;
    	string PlayerBattingFile;
    	
    	ifstream myIn;
    	ifstream myIn2;
    	
    	cout << "Enter file name that contains player records: ";
    	cin >> PlayerRecordsFile;
    	myIn.open(PlayerRecordsFile.c_str());
    	
    	while (!myIn)
    	{
    		cout << "Enter file name that contains player records: ";
    		cin >> PlayerRecordsFile;
    		myIn.open(PlayerRecordsFile.c_str());
    	}
    	
    	ReadPlayerInfo(myIn, nameArray, idArray, size);   
    
    	cout << "Enter file name that contains batting data: ";
    	cin >> PlayerBattingFile;
    	myIn2.open(PlayerBattingFile.c_str());
    	
    	while (!myIn2)
    	{
    		cout << "Enter file name that contains batting data: ";
    		cin >> PlayerBattingFile;
    		myIn2.open(PlayerBattingFile.c_str());
    	}
    
    	cout << "Player Name     " << "ID      " << "Batting Average      " <<  "Number of Walks" << endl;
    	cout << "--------------------------------------------------------------------------" << endl;
    	
    	ReadProcessBatting(myIn2, id, batAvg, walkArray, size);
    	for(i=0;i<size - 1;i++)
    	{
    		SearchPosition(idArray, id,firstID, pos);
    		firstID++;
    		PrintTable(nameArray, idArray, batAvg, walkArray, firstID, pos);
    	}
        
    
    	
    
    	myIn.close();
    	myIn.clear();
    	myIn2.close();
    	myIn2.clear();
    	return 0;
    		
    }
    
    void ReadPlayerInfo(ifstream& myIn, string nameArray[], int idArray[], int& size)
    {
    	string playerName;
    	int playerNumber;
    	int i;
    	for (i=0;i<size;i++)
    	{
    		myIn >> playerName;
    		nameArray[i] = playerName;
    		myIn >> playerNumber;
    		idArray[i] = playerNumber;
    	}
    }
    
    void ProcessRecord(string batRec, float& batAvgRunning, int& walk)
    {
    	int x = 0;
    	walk = 0;
    	char choice;
    	float hit = 0;
    	float out = 0;
    	while (x < batRec.length())
    	{
    		choice = batRec[x];
    		x++;
    	
    		switch(choice)
    		{
    			case 'O':
    				out++;
    				break;
    			case 'H':
    				hit++;
    				break;
    			case 'W':
    				walk++;
    				break;
    			default:
    				break;
    		}
    	}
    
    	batAvgRunning = (hit/(out+hit));
    }
    
    void ReadProcessBatting(ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size)
    {
    	int x;
    	string batRec;
    	float batAvgRunning;
    	int walk;
    	for (x=0;x<size;x++)
    	{
    		
    		myIn2 >> id[x];
    		getline(myIn2,batRec);
    		ProcessRecord(batRec, batAvgRunning, walk);
    		batAvg[x] = batAvgRunning;
    		walkArray[x] = walk;
    	}
    	
    
    }
    
    int SearchPosition(int idArray[], int id[], int firstID, int& pos)
    {
    	int b = 0;
    	
    		while (idArray[firstID] != id[b] && b < 19)
    		{
    			b++;
    		}
    		pos = b;
    		return pos;
    }
    
    void PrintTable(string nameArray[],int idArray[],float batAvg[],int walkArray[],int counter, int pos)
    {
    	cout << setw(5) << nameArray[counter] << "       ";
    	cout << setw(5) <<  idArray[counter] << "          ";
    	cout << setprecision(3) << setw(5) << batAvg[pos] << "                 ";
    	cout << setw(5) << walkArray[pos];
    	cout << endl;
    }
    If you run this I get 19 of the 20 players to show up... The ID 3 is not showing idk why...

    and how can I format to display everything nicely like aligned

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    you are almost there then if you are talking about improving the looks
    if you want to sort that out in console based application you can work wonders with the formatting available in printf() and a little 'gotoxy()' or some kind of SetConsoleCursor() function, there are some examples in the Cprogramming.com FAQ

    but if you wanted to do something really fancy then try a text lilbrary like pdcurses >> http://pdcurses.sourceforge.net/index.html
    Last edited by rogster001; 12-02-2009 at 10:50 AM.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Why does it only output 19 of the 20 im really not seeing that... If i change it and print another line

    it brings out a line with batting.dat followed by random numberss

  15. #15
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    are your counts out of step? in your mind and in the code i mean...you know an array index starts at 0? so 20 elements would be numbered 0 to 19...? maybe thats what you have not implemented correctly somewhere.

    it could be this because the random numbers you are talking about are probably a spare array element that you have failed to fill with the data you want, thus when you access it you just read whatever is living in that memory area at the time, i.e any old nonsense
    Last edited by rogster001; 12-02-2009 at 10:55 AM.

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

Similar Threads

  1. BitWise Newbie - Needs Urgent Help
    By frodonet in forum C Programming
    Replies: 15
    Last Post: 09-26-2007, 12:58 PM
  2. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM
  3. help... urgent,... thanks!
    By weihann in forum C Programming
    Replies: 6
    Last Post: 02-28-2002, 09:17 PM
  4. Help.... Urgent... Thanks!
    By weihann in forum C Programming
    Replies: 0
    Last Post: 02-27-2002, 09:15 PM

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