c++ searching files helps

Discussion in 'Software' started by someguy85, Nov 20, 2009.

  1. someguy85

    someguy85 Private E-2

    Hi everyone. I'm having a tough time getting my program to do what I want. I'm reading data from a file, but I need to convert all the data to uppercase (except for the digits of course). For some reason the toupper function won't work saying "cannot convert char* to int". I thought the toupper function simply ignored exempt characters? I also need to convert user entered data to uppercase to properly search through the data.

    Also, in the search function, I'm looking to write an 'else if' testing that the target input was not found in the file and also that it is the end of the file to display 'target not found' instead of printing this with each loop iteration. I tried using the eof function, but have not had any luck. Sorry for asking for so much but I just can't seem to find out how to fix this and I haven't found anything in my book to help me. Thanks for any help. The code is below and the data file that is being read from is attached.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    char** allocateMemory(int);
    void getList(char**, int);
    void search(char**, int);
    
    void main()
    {
    	char response;
    	const int numNames = 11;
    	char** list = allocateMemory(numNames);
    	getList(list, numNames);
    	do
    	{
    		search(list, numNames);
    		cout << "To search for another name enter Y or to quit enter N. ";
    		cin >> response;
    		response = toupper(response);
    	}while(response == 'Y');
    
    	
    
    }
    
    char** allocateMemory(int names)
    {
    	char** list = new char*[names]; // allocates rows (in this case 11 because that's the value of names)
    	for(int count = 0; count < names; count++)
    
    	{
    		list[count] = new char[30];
    	}
    
    	return list;
    }
    
    void getList(char** list, int names)
    {
    	fstream inFile;
    	inFile.open("phoneList.txt");
    	if(!inFile)
    	{
    		cout << "Error opening file.\n";
    		exit(0);
    	}
    	for(int count = 0; count < names; count++)
    	{
    		inFile.getline(list[count], 30);
    	}
    	inFile.close();
    }
    
    void search(char** list, int name)
    {
    	char target[30];
    	cout << "Enter a full name, partial name, or phone number to start search. ";
    	cin.ignore();
    	cin.getline(target, 30);
    
    	for(int count = 0; count < name; count++)
    	{
    		if(strstr(list[count], target) != NULL)
    		{
    			cout << "\nTarget was found.\n\tTarget:\t" << list[count] << endl;
    		}
    		else
    		{
    			cout << "\nTarget not found.\n";
    		}
    	}
    }
    
     

    Attached Files:

  2. Mainwaring

    Mainwaring Private E-2

    The toupper function works per character, not per string; to use it in your program you would need to convert every character in the string seperately.

    For example:
    Code:
    void getList(char** list, int names)
    {
    	fstream inFile;
    	inFile.open("phoneList.txt");
    	if(!inFile)
    	{
    		cout << "Error opening file.\n";
    		exit(0);
    	}
    	for(int count = 0; count < names; count++)
    	{
    		inFile.getline(list[count], 30);
    		for(int i = 0; i < 30; i++)
    		{
    			list[count][i] = toupper(list[count][i]);
    		}
    	}
    	inFile.close();
    }
    

    I'm not sure if I understand what you want, since the search function does not actually deal with the file itself but only with the list in memory. I think what you want to do is to go through the list and show the result if you find the target and else show that you checked all list entries, but didn't find the target.
    You could do that for example like this;

    Code:
    void search(char** list, int name)
    {
    	char target[30];
    	int foundtarget;
    	cout << "Enter a full name, partial name, or phone number to start search. ";
    	cin.ignore();
    	cin.getline(target, 30);
    
    	foundtarget = 0;
    	for(int count = 0; count < name; count++)
    	{
    		if(strstr(list[count], target) != NULL)
    		{
    			foundtarget= 1;
    			cout << "\nTarget was found.\n\tTarget:\t" << list[count] << endl;
    		}
    	}
    
    	if(!foundtarget)
    	{
    		cout << "\nTarget not found.\n";
    	}
    }
     
  3. someguy85

    someguy85 Private E-2

    Thanks for your help. I did figure out how to get the "target not found" message to display the way I wanted. I haven't yet incorporated the use of converting to uppercase. I'll attempt that after I complete my next couple assignments (which will lead to the creation of other threads), because it turns out I didn't even need to convert to uppercase for the assignment (though I'd still like to know how). I'll go ahead and post the code I already have working in case anybody else has similar problems. Thanks again for your help.


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    // Function Prototypes
    char** allocateMemory(int);
    void getList(char**, int);
    void search(char**, int);
    
    void main()
    {
    	char response;
    	const int numNames = 11;
    	char** list = allocateMemory(numNames);
    	getList(list, numNames);
    	do
    	{
    		search(list, numNames);
    		cout << "To search for another name enter Y or to quit enter N. ";
    		cin >> response;
    		response = toupper(response);
    	}while(response == 'Y');	
    
    }
    //**************************************************************************************************************************
    
    char** allocateMemory(int names)
    {
    	char** list = new char*[names]; // allocates rows (in this case 11 because that's the value of names)
    	for(int count = 0; count < names; count++)
    
    	{
    		list[count] = new char[30];
    	}
    
    	return list;
    }
    //**************************************************************************************************************************
    
    void getList(char** list, int names)
    {
    	fstream inFile;
    	inFile.open("phoneList.txt");
    	if(!inFile)
    	{
    		cout << "Error opening file.\n";
    		exit(0);
    	}
    	for(int count = 0; count < names; count++)
    	{
    		inFile.getline(list[count], 30);
    	}
    	inFile.close();
    }
    //**************************************************************************************************************************
    
    void search(char** list, int name)
    {
    	char target[30];
    	cout << "Enter a full name, partial name, or phone number to start search. ";
    	cin.ignore();
    	cin.getline(target, 30);
    	bool found = false;
    
    	for(int count = 0; count < name; count++)
    	{
    		if(strstr(list[count], target) != NULL)
    		{
    			cout << "\nTarget was found.\n\tTarget:\t" << list[count] << endl;
    			found = true;
    		}
    	
    	}
    	if(found == false)
    		{
    			cout << "\nTarget not found.\n";
    		}
    }
    
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds