Hi guys, newbie here, some questions!

This is a discussion on Hi guys, newbie here, some questions! within the C Programming forums, part of the General Programming Boards category; You should pay attention to the warnings of th ecompiler Code: #include <stdio.h> #include <string.h> #define ALPHABET_TOTAL 26 int main() ...

  1. #31
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You should pay attention to the warnings of th ecompiler
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define ALPHABET_TOTAL 26                    
    
    int main()
    {
    	
    	/*defining variables*/
    	
    	FILE *fp;                              
    	
    	int text[ALPHABET_TOTAL] = {0},          
    		text_input,                        
    		ASCII_count,                       
    		alpha;                             
    	
    	char filename[20];
    	
    	
    	/*initializing*/
    	
    	text_input = 0;   
    	ASCII_count = 0;
    	alpha = 0;
    	
    	/*user prompt*/ 
    	
    	printf("Type a file name, followed by the <enter> key : ");   
    	
    	if(fgets(filename,sizeof(filename),stdin))
    	{
    		char* p = strchr(filename, '\n');
    		if(p) *p = '\0';
    	}
    	else
    	{
    		puts("Failed to read a file name");
    		return 1;
    	}
    	
    	fp=fopen(filename,"r");
    	
    	
    	/*text analysis*/
    	
    	while( (text_input = fgetc(fp)) != EOF )   
    	{
    		
    		if(text_input >= 'a' && text_input <= 'z')
    			text_input -= 'a';
    		else if(text_input >= 'A' && text_input <= 'Z')
    			text_input -= 'A';
    		else
    			continue;
    
    		text[text_input]++;
    	}
        
    	/*display results*/
        
    	for (ASCII_count = 0; ASCII_count <= 25; ASCII_count++)
    		printf("\nTotal &#37;c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]);
    
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  2. #32
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well I tried compiling your program and I got implicit declaration of strchr, which means you didn't include some library, probably the strings library.

    also it said that :

    for (ASCII_count; ASCII_count <= 25; ASCII_count++)

    was a statement with no effect

  3. #33
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well you didn't initialize this:

    for (ASCII_count = 0; ASCII_count <= 25; ASCII_count++)

    Whatever the value is meant to be

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What compiler are you using?

    Does it ask you for a filename? If not, try addding "fflush(stdout);" after the printf() asking the name.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #35
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    I'm using the Borland C++

    Yeah it does prompt me for a file name, in fact the whole thing works pretty well

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Borland C++ is a pretty old, non-recommended compiler.
    Try upgrading to, say, Visual Studio. Great IDE.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Ok, another quick question, more to do with the logical side of things.

    Basically I've added a few lines in order to make a word count - using the number of spaces + 1, however my program seems to add to the count when it encounters punctuation too. I have no idea why!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define ALPHABET_TOTAL 26                    
    
    int main()
    
    {
    	
    	/*defining variables*/
    	
    	FILE *fp;                              
    	
    	int text[ALPHABET_TOTAL] = {0},          
    		text_input,                        
    		ASCII_count,                       
    		word_count;                             
    	
    	char filename[20];
    	
    	
    	/*initializing*/
    	
    	text_input = 0;   
    	ASCII_count = 0;
    	word_count = 1;
    	
    	/*user prompt*/ 
    	
    	printf("Type a file name, followed by the <enter> key : ");   
    	
    	if(fgets(filename,sizeof(filename),stdin))
    	{
    		char* p = strchr(filename, '\n');
    		if(p) *p = '\0';
    	}
    	else
    	{
    		puts("Failed to read a file name");
    		return 1;
    	}
    	
    	fp=fopen(filename,"r");
    	
    	
    	/*text analysis*/
    	
    	while( (text_input = fgetc(fp)) != EOF )   
    	{
    		
    		if(text_input >= 'a' && text_input <= 'z')
    			text_input -= 'a';
    		else if(text_input >= 'A' && text_input <= 'Z')
    			text_input -= 'A';
                    else if(text_input = ' ')
    			word_count++;
                  
    		else
    			continue;
    
    		text[text_input]++;
    	}
    
    	printf(" Total words: %d " , word_count++);
        
    	/*display results*/
        
    	for (ASCII_count = 0; ASCII_count <= 25; ASCII_count++)
    		printf("\n Total %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]);
    
    	return 0;
    }

  8. #38
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    So set it to count a word only when there is a space encountered or a new line. As long as you don't do this:

    hi , ok

    it will work fine

  9. #39
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    That's what I thought I'd done...

    Code:
    else if(text_input = ' ')
    			word_count++;

  10. #40
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    seems to add to the count when it encounters punctuation too. I have no idea why!
    Why do you think so?
    Show your input and the output
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Code:
    else if(text_input == ' ')
    Turn up them warnings. Some compilers warn about that.
    Single = is assign and double == is compare.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #42
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Quote Originally Posted by Elysia View Post
    Code:
    else if(text_input == ' ')
    Turn up them warnings. Some compilers warn about that.
    Single = is assign and double == is compare.

    aha!

    Thanks!

  13. #43
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Ok, another one

    Trying to print the 'average word length' which I'm trying to define as total characters/total words

    Do all of these variables need to be defined as doubles from the offset?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define ALPHABET_TOTAL 26                    
    
    int main()
    
    {
    	
    	/*defining variables*/
    	
    	FILE *fp;                              
    	
    	int text[ALPHABET_TOTAL] = {0},          
    		text_input,                        
    		ASCII_count;
            double  letter_count,                       
    		word_count,
                    word_average;                             
    	
    	char filename[20];
    	
    	
    	/*initializing*/
    	
    	text_input = 0;   
    	ASCII_count = 0;
            letter_count = 0;
    	word_count = 1;
            word_average = 0;
    	
    	/*user prompt*/ 
    	
    	printf("Type a file name, followed by the <enter> key : ");   
    	
    	if(fgets(filename,sizeof(filename),stdin))
    	{
    		char* p = strchr(filename, '\n');
    		if(p) *p = '\0';
    	}
    	else
    	{
    		puts("Failed to read a file name");
    		return 1;
    	}
    	
    	fp=fopen(filename,"r");
    	
    	
    	/*text analysis*/
    	
    	while( (text_input = fgetc(fp)) != EOF )   
    	{
    		
    		if(text_input >= 'a' && text_input <= 'z')
    			text_input -= 'a',
                            letter_count++;
    		else if(text_input >= 'A' && text_input <= 'Z')
    			text_input -= 'A',
                            letter_count++;
                    else if(text_input == ' ')
    			word_count++;
                  
    		else
    			continue;
    
    		text[text_input]++;
    
    	}
    
            word_average = letter_count/word_count;
    
    
    /*display results*/
    
    	printf(" Average word length: %d ", word_average);
        
    	
        
    	for (ASCII_count = 0; ASCII_count <= 25; ASCII_count++)
    		printf("\n Total %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]);
    
    	return 0;
    }

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    No, they don't need to be doubles because you're not using decimals.
    Code:
    		if(text_input >= 'a' && text_input <= 'z')
    			text_input -= 'a',
                            letter_count++;
    		else if(text_input >= 'A' && text_input <= 'Z')
    			text_input -= 'A',
                            letter_count++;
    Should be
    Code:
    		if(text_input >= 'a' && text_input <= 'z')
    		{
    			text_input -= 'a';
                            letter_count++;
    		}
    		else if(text_input >= 'A' && text_input <= 'Z')
    		{
    			text_input -= 'A';
                            letter_count++;
    		}
    { marks the beginning of a block and } marks the end, just like your function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    When I define the variables as ints, the &#37;d is rounded to a whole number in the printf call, why is it doing that?

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Pipes, inheritence, newbie questions
    By grasshopa in forum Windows Programming
    Replies: 0
    Last Post: 02-28-2006, 06:17 AM
  3. Newbie IO questions
    By gustavosserra in forum C Programming
    Replies: 5
    Last Post: 08-29-2003, 09:09 AM
  4. More newbie questions
    By sunzoner in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2002, 07:23 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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