-
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);
}
-
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,
-=-=-=-=-=-=-=-
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);
}
-
Great!
Thanks for the help.
Ross
-
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.