View Poll Results: How many times have you written a program flawlessly on the first try

Voters
19. You may not vote on this poll
  • All the time

    3 15.79%
  • Sometimes

    10 52.63%
  • It's too hard

    6 31.58%

Thread: I/O streams

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Question I/O streams

    The following below is a function in my program. I am supposed to find out the frequency of the letters of the alphabet in the input file. The problem is that i can't get any frequency value after the letter a. I was wondering how to read the text each time a new letter is asked for and can i set my counter back to 0 every time.
    Thanks

    here is the function definition:

    Code:
    void read_data(ifstream& input)
    {
    	char alpha[26] ;
    	alpha[0]= 'a';
    	alpha[1]= 'b';
    	alpha[2]= 'c';
    	alpha[3]= 'd';
    	alpha[4]= 'e';
    	alpha[5]= 'f';
    	alpha[6]= 'g';
    	alpha[7]= 'h';
    	alpha[8]= 'i';
    	alpha[9]= 'j';
    	alpha[10]= 'k';
    	alpha[11]= 'l';
    	alpha[12]= 'm';
    	alpha[13]= 'n';
    	alpha[14]= 'o';
    	alpha[15]= 'p';
    	alpha[16]= 'q';
    	alpha[17]= 'r';
    	alpha[18]= 's';
    	alpha[19]= 't';
    	alpha[20]= 'u';
    	alpha[21]= 'v';
    	alpha[22]= 'w';
    	alpha[23]= 'x';
    	alpha[24]= 'y';
    	alpha[25]= 'z';
    	cout<<"The Letter Frequencies are:"<<endl;
    	char ch;
    	double sum =0;
    	double frequency;
    	int counter = 0; 
    	for (int i =0; i<26;i++)
    	{
    			int counter = 0;
    			while (input.get(ch))
    			{
    				if (!islower(ch))
    				{
    					tolower(ch);
    				}
    				if (ch == alpha[i])
    				{
    					counter ++;
    				}
    				if (i<1)
    				{
    					if(isalpha(ch))
    					{
    					sum ++;
    					}
    				}
    				
    			}
    			frequency = (counter/sum)*100;
    	cout<<alpha[i]<<" = "<<frequency<<" %"<<"\n";
    		
    			
    	}		
    }
    Last edited by scottmanc; 02-06-2003 at 05:45 PM.
    C++ can hurt.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    What's with the poll? Anyway, If you write a program flawless on
    the first try, Then you've picked a program that's very easy and
    can be created just by learning statements and doesn't require
    you to have expirience in creating it.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    RE: Poll

    I seem to remember reading somewhere that, on average, every line of code gets re-written twice! (If you write a 100 line program, you're going to end up writing 300 lines.) We all like to think that we are better than that, but by the time you're done fixing your syntax errors, fixing your logical errors, improving your program, and "experimenting", this could easily be the case. Most of the time we don't know exactly what the problem is... so we have to change more than one line to narrow-down the problem.

    I suppose it's a bit of a paradox that you should be somewhat of a perfectionist to be a programmer, yet you have to accept the fact that you're going to make mistakes.

    So, compile often, and test your code as soon as you have a testable bit of code!

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    so can anyboady help me with my problem?
    (trying to get the text to be read for each letter of the alphabet)
    C++ can hurt.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    I personally would use a loop to initialize this 26 element array instead of writing each line out!!!
    Mr. C: Author and Instructor

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i would too but i have time and i don't want the user to enter the values they are predetermined (unless of course there is a way for the compiler to use the alphabet)
    C++ can hurt.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    The reason it won´t is because you got your algorithm screwed...

    Your reading the file through to check only for the letter 'a'.

    Now I would do like (written in pseudo):

    Code:
    char c;
    char array_of_characters[26]; //initialise them to zero!
    
    file=open("your filename here something");
       while(file.readch(&c)) // I think it needs the adress of a char
             {
                  if(isalpha(c)) 
                       {
                        tolower(c);
                        toindex(c);  // some function to convert your character to array index..
                        array_of_characters[c]++;
                        }
              }
    I guess this should do it...

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    thanks you gave me a good idea. Now i'll have to see if it will work after i type it up.
    C++ can hurt.

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's very hard to write a program without any mistakes from the first time( ot at least for me ), it's very notmal to have some errors while writting the program, I don't think you should be very concerned with it.
    none...

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    They say one should spend 90% working on the code in paper and 10% hands on; of course, I spend 90% on the computer and the other 10% scratching my head trying to figure out what's wrong.

    You can write punct, chars, digits etc using a simple for loop so that you can test your algo, or put it in a function and write the stuff many times

    Code:
    for(int i = 0; i < 127; i++)
    {
       if(isalpha(i) || isdigit(i) || ispunct(i))
          outFile << (char)i;
    }
    As for your function, I'm not positive as to what you need. The number of times each of the 26 alpha chars shows up (yikes) versus how many lower case letters there are among the total count of characters. The latter shouldn't be too difficult. I'm not saying that this is the best method but it *should* work in counting the type of each character.
    Code:
    void readFile(ifstream& input)
    {
      char dataIn;
      float digits, total, lcase, ucase;
    	
      lcase = ucase = digits = total = 0;
    	
      input >> dataIn;
    	
      while(input)
      {
         if(islower(dataIn))
            lcase++;
    		
        else if(isupper(dataIn))
           ucase++;
    		
        else if(isdigit(dataIn))
          digits++;
    		
        total++;
        input >> dataIn;
      }
    
      cout << "The freq of lowr case is " lcase/total; // etc
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    the problem that i am trying to solve is to find out the total number of each letter of the alphabet that appears in a text, find the amount of times each one appears in teh whole text and then get the frequency by dividing the amount of times one leeter appears by the total amount of letters present and multipying it by 100.
    C++ can hurt.

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    thank you for those who helped me. The program is up and running. BTW, the code for program is intended to help decifering encrypted messages.
    C++ can hurt.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    oh one more thing, does anyone know how to increase the amount of lines that the output can show on the screen because i can only see around 20 lines or so. I need the whole output.
    Last edited by scottmanc; 02-07-2003 at 05:58 PM.
    C++ can hurt.

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    re-direct the output to a file, if your program is named hist then type
    hist > file.txt
    at the dos/shell prompt. file.txt contains your output.

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Re: I/O streams

    Originally posted by scottmanc

    Code:
    void read_data(ifstream& input)
    {
    	char alpha[26] ;
    	alpha[0]= 'a';
    	alpha[1]= 'b';
    	alpha[2]= 'c';
    	alpha[3]= 'd';
    	alpha[4]= 'e';
    	alpha[5]= 'f';
    	alpha[6]= 'g';
    	alpha[7]= 'h';
    	alpha[8]= 'i';
    	alpha[9]= 'j';
    	alpha[10]= 'k';
    	alpha[11]= 'l';
    	alpha[12]= 'm';
    	alpha[13]= 'n';
    	alpha[14]= 'o';
    	alpha[15]= 'p';
    	alpha[16]= 'q';
    	alpha[17]= 'r';
    	alpha[18]= 's';
    	alpha[19]= 't';
    	alpha[20]= 'u';
    	alpha[21]= 'v';
    	alpha[22]= 'w';
    	alpha[23]= 'x';
    	alpha[24]= 'y';
    	alpha[25]= 'z';
    	//
    }
    You could replace all that code with :
    Code:
    	char alpha[26];
    	for(int index=0;index < 26; index++)
    		alpha[index] = (char)(65+index);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  2. Mulitple I/O streams
    By thamisfit in forum C Programming
    Replies: 3
    Last Post: 03-22-2003, 04:15 PM
  3. Cant comprehend C++ I/O and streams.
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2003, 07:21 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. Dealing with i/o streams in a class
    By ender in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2002, 05:26 PM