Thread: Problems with program

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    20

    Problems with program

    I have a question about selection 1 on my menu. Its supposed to count the occurrence of each letter in the file its processing. Then its supposed to create a percentage of each letter occuring in the text file. I've looked in the book and at my profs examples but still can't find anything. I'm clueless as how to do this, any help or pointers in the right direction would be greatly appreciated.
    Thank you,
    K

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
    	string infilename, outfilename;
    	string WordToFind, WordToReplace, ReplaceWith;
    	int selection = 0;
    
    	cout << "Please type in the name of the file to be opened." << endl;
    	cin >> infilename;
    
    	do
    	{
    	
    	cout << "               Select one of the following functions ";
    	cout << "\n				1)Tabulate the frequency of occurrence of letters (A...Z) in a file ";
    	cout << "\n				2)Count the occurrence of a specified word in a file. ";
    	cout << "\n				3)Replace a specified word by another word in a file. ";
    	cin >> selection;
    	
    	}
    
    	while (selection < 1 || selection > 3);
    		return selection;
    
    	if (selection == 1)
    		cout << infilename.length() << endl;
    	
    	else (selection == 2)
    		cout << "Please enter the word you would like to look for";
    		cin >> WordToFind;
    		
    		infilename.find(WordToFind);
    		cout << infilename << WordToFind << infilename.find(WordToFind) << endl;
    	
    	else if (selection == 3)
    		cout << "Please enter the word you would like to be replaced";
    		cin >> WordToReplace;
    
    		cout << "What word would you like to replace << WordToReplace << with?" << endl;
    		cin >> ReplaceWith;
    
    		WordToReplace.swap(ReplaceWith);
    
    		cout << "Enter the file name to write into:";
    		cin >> outfilename;
    
    		ofstream outfile (outfilename.c_str());
    
    		outfile.close();
    
    		return 0;
    }

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    char chCount[26];

    Go through the file, one character at a time, if it's a-z or A-Z increment the corresponding array element. Once it's done, add all the elements together and you'll get the total number of A-Z characters in the file. From that, you can display the percentages and such.

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbs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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Call me stupid. But I'm still confused. How am I going to store each letter of the alphabet and print it out? Would I make another array? And could I use switch instead of if else statements?
    Thanks,
    K

  4. #4
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    You don't need to store each letter, only count each letter.

    char chCount[26];

    This array will hold 26 elements, A-Z. Read through the file and each time you get a letter increment the corresponding element.

    _something_ like:

    PHP Code:
    char THECHAR;
    char chCount[26];

    // Read in from the file

    switch (THECHAR) {
      case 
    'a': case 'A':
      
    chCount[0]++;
      
      case 
    'b': case 'B':
      
    chCount[1]++;

      case 
    'c': case 'C':
      
    chCount[2]++;

      
    // etc...


  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Thanks guys. I understand now & am no longer confused.
    K

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    I used the one like Dizolve did and now I get an error that says:
    switch expression of type char[26] is illegal. It could be because I have a switch inside of a switch. See below.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
    	string infilename, outfilename;
    	string WordToFind, WordToReplace, ReplaceWith;
    	int selection = 0;
    	char chCount[26];
    	char TheChar[26];
    
    	cout << "Please type in the name of the file to be opened." << endl;
    	cin >> infilename;
    
    	do
    	{
    	
    	cout << "               Select one of the following functions ";
    	cout << "\n				1)Tabulate the frequency of occurrence of letters (A...Z) in a file ";
    	cout << "\n				2)Count the occurrence of a specified word in a file. ";
    	cout << "\n				3)Replace a specified word by another word in a file. ";
    	cin >> selection;
    	
    	}
    
    	while (selection < 1 || selection > 3);
    		return selection;
    
    	switch(selection)
    	{
    	case '1':
    		
    	switch(TheChar)
    	{
    		case 'a': case 'A':
    		chCount[0]++;
      
    		case 'b': case 'B':
    		chCount[1]++;
    
    		case 'c': case 'C':
    		chCount[2]++;
    
    		case 'd': case 'D':
    		chCount[3]++;
      
    		case 'e': case 'E':
    		chCount[4]++;
    
    		case 'f': case 'F':
    		chCount[5]++;
    
    		case 'g': case 'G':
    		chCount[6]++;
      
    		case 'h': case 'H':
    		chCount[7]++;
    
    		case 'i': case 'I':
    		chCount[8]++;
    		
    		case 'j': case 'J':
    		chCount[9]++;
    	
    		case 'k': case 'K':
    		chCount[10]++;
      
    		case 'l': case 'L':
    		chCount[11]++;
    
    		case 'm': case 'M':
    		chCount[12]++;
    
    		case 'n': case 'N':
    		chCount[13]++;
      
    		case 'o': case 'O':
    		chCount[14]++;
    
    		case 'p': case 'P':
    		chCount[15]++;
    
    		case 'q': case 'Q':
    		chCount[16]++;
      
    		case 'r': case 'R':
    		chCount[17]++;
    
    		case 's': case 'S':
    		chCount[18]++;
    
    		case 't': case 'T':
    		chCount[19]++;
      
    		case 'u': case 'U':
    		chCount[20]++;
    
    		case 'v': case 'V':
    		chCount[21]++;
    
    		case 'w': case 'W':
    		chCount[22]++;
      
    		case 'x': case 'X':
    		chCount[23]++;
    
    		case 'y': case 'Y':
    		chCount[24]++;
    
    		case 'z': case 'Z':
    		chCount[25]++;
    	break;
    	}
    	case '2':
    		cout << "Please enter the word you would like to look for";
    		cin >> WordToFind;
    		
    		infilename.find(WordToFind);
    		cout << infilename << WordToFind << infilename.find(WordToFind) << endl;
    		break;
    
    	case '3':
    		cout << "Please enter the word you would like to be replaced";
    		cin >> WordToReplace;
    
    		cout << "What word would you like to replace << WordToReplace << with?" << endl;
    		cin >> ReplaceWith;
    
    		WordToReplace.replace(m, n, ReplaceWith);
    
    		cout << "Enter the file name to write into:";
    		cin >> outfilename;
    
    		ofstream outfile (outfilename.c_str());
    
    		outfile.close();
    	}
    		return 0;
    }
    Thanks!
    :-)
    K

  7. #7
    PorkChop
    Guest
    You can't base a switch statement on an array. TheChar would have to be a single character, not an array of 26.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Thanks Chop!
    K

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  3. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 04:26 PM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM