Error in my program

This is a discussion on Error in my program within the C Programming forums, part of the General Programming Boards category; Hello all, I've written a program but can't figure out why it is only outputting the same integer for all ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    7

    Error in my program

    Hello all,

    I've written a program but can't figure out why it is only outputting the same integer for all the tests. Maybe someone can take a look. I think the code is pretty self explainitory but at the end (which is where the problem is I think) the program needs to tally up the number of letters, numerals, punctuation, spaces, and other in an inputed string.

    When I run the program it will count the number of characters correctly but will not tally. I get the number -370086 for all of the answers. Thanks for any help I receive and if something else needs to be explained, I'll be around. Thanks again.
    -Ross

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define SIZE 80
    
    int main(void)
    {
    	char S[SIZE];
    	int SL;
    	int P;
    	int QL;
    	int QD;
    	int QP;
    	int QS;
    	int QO;
    
    	system("cls");
    
    	printf("CHARACTER COUNTER\n\n");
    
    	printf("Written by Ross Buffington == 12/03/2003\n\n");
    
    	printf("This program will request and store a string of characters\n");
    	printf("and then report how many of the selected characters are:\n");
    	printf("         Letters\n");
    	printf("         Numerals\n");
    	printf("         Punctuation\n");
    	printf("         Spaces\n");
    	printf("         or Other\n\n");
    
    	fflush(stdin);           /* Flush Keyboard Buffer */
    	printf("On the line below, enter the string that should be analyzed:\n");
    	printf(">");
    	fgets(S, SIZE, stdin);
    	if(S[strlen(S) - 1] == '\n')
    		S[strlen(S) - 1] = '\0';
    
    	SL = strlen(S);
    
    	for(P=0; S[P] < S[SIZE - 1]; P++)
    
    		while(S[P] != '\0')
    			{
    				if(isalpha(S[P]))
    					QL = QL + 1;
    				else if(isdigit(S[P]))
    					QD = QD + 1;
    				else if(ispunct(S[P]))
    					QP = QP + 1;
    				else if(isspace(S[P]))
    					QS = QS + 1;
    				else
    					QO = QO + 1;
    			}
    
    	printf("\nANALYSIS RESULTS:\n\n");
    
    	printf("String Length: %d\n", SL);
    	printf("Letters:      %d\n", QL);
    	printf("Numerals:     %d\n", QD);
    	printf("Punctuation:  %d\n", QP);
    	printf("Spaces:       %d\n", QS);
    	printf("Other:        %d\n", QO);
    
    
    
    
    	fflush(stdin); getchar();    /*Pauses the program before exit*/
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    There are 3 logical errors in the code,

    1) Variables QL, QD, QP, QS, QO are to be initialized to 0, since you are using it in the later part of the program.

    2) I don't understand the condition in the for-loop

    Code:
    for(P=0; S[P] < S[SIZE - 1]; P++)
    If you want to traverse through the entire string then the for-loop would be,

    Code:
    for(P=0; P < SL - 1 ; P++)
    3) Instead of,
    Code:
    while(S[P] != '\0')
    it has to be,
    Code:
    if(S[P] != '\0')

    -=-=-=-=-=-=-=-

    so the rewritten code will be

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define SIZE 80
    
    int main(void)
    {
    	char S[SIZE];
    	int SL;
    	int P;
    	int QL=0;
    	int QD=0;
    	int QP=0;
    	int QS=0;
    	int QO=0;
    
    	system("cls");
    
    	printf("CHARACTER COUNTER\n\n");
    
    	printf("Written by Ross Buffington == 12/03/2003\n\n");
    
    	printf("This program will request and store a string of characters\n");
    	printf("and then report how many of the selected characters are:\n");
    	printf("         Letters\n");
    	printf("         Numerals\n");
    	printf("         Punctuation\n");
    	printf("         Spaces\n");
    	printf("         or Other\n\n");
    
    	fflush(stdin);           /* Flush Keyboard Buffer */
    	printf("On the line below, enter the string that should be analyzed:\n");
    	printf(">");
    	fgets(S, SIZE, stdin);
    	if(S[strlen(S) - 1] == '\n')
    		S[strlen(S) - 1] = '\0';
    
    	SL = strlen(S);
    
    	for(P=0; P < SL-1; P++)
    		if(S[P] != '\0')
    			{
    				if(isalpha(S[P]))
    					QL = QL + 1;
    				else if(isdigit(S[P]))
    					QD = QD + 1;
    				else if(ispunct(S[P]))
    					QP = QP + 1;
    				else if(isspace(S[P]))
    					QS = QS + 1;
    				else
    					QO = QO + 1;
    			}
    
    	printf("\nANALYSIS RESULTS:\n\n");
    
    	printf("String Length: %d\n", SL);
    	printf("Letters:      %d\n", QL);
    	printf("Numerals:     %d\n", QD);
    	printf("Punctuation:  %d\n", QP);
    	printf("Spaces:       %d\n", QS);
    	printf("Other:        %d\n", QO);
    
    
    
    
    	fflush(stdin); getchar();    /*Pauses the program before exit*/
    	return(0);
    }

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    7
    Great!

    Thanks for the help.

    Ross

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Also, fflush(stdin) is unnecessary in your program as well as not good practice in C/C++ as the standard states it is undefined so anything can happen -- including messing up your program.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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