Question:
Whats wrong with this C program?
anthony99992004
2009-03-07 12:34:38 UTC
I need help figuring out what is wrong with this program, if someone could please explain it to me i would be very thankful.

/***************************

The purpose of the following program is to count the whitespace, the digits, the uppercase alphabetics and lowercase alphabetics in the input.

*****************************/

#include

int main(void)
{
int iochar,
numdigits=0,
numlower=0,
numupper=0,
numwhites=0;

printf("Please enter a phrase:\n\n");

while((iochar=getchar())!=EOF)
{
if (iochar=' ')
{
numwhites++;
putchar(iochar);
}
else
if((iochar>='0')&&(iochar<='9'))
{
numdigits++;
putchar(iochar);
}
else
if(('a'<=iochar)&&(iochar<='z'))
{
numlower++;
putchar(iochar-32);
}
else
if(('A'<=iochar)&&(iochar<='Z'))
{
numupper++;
putchar(iochar);
}
else
putchar(iochar);
}

printf("%d white characters, %d digits, ",numwhites,numdigits);
printf("%d lowercase have been converted to ",numlower);
printf("uppercase and %d uppercase.\n",numupper);

printf("\n\n");


return 0;
}
Six answers:
Math.
2009-03-07 13:16:41 UTC
Alright, I have checked your code and I think I have just the solution for you.



First of all, in your loop, you should check for line break, that is char '\n'. Next thing to fix is that when you check for space, you have to add another "="-sign, as you have it now it will just set iochar.



I do not know about you, but I had to flush the output stream to show the text before entering the loop.



Ok, to sum it up, this is your program edited:





#include



int main()

{

int iochar, numdigits = 0, numlower = 0, numupper = 0, numwhites = 0;



printf("Please enter a phrase:\n");

fflush(stdout); // To show the user what to do

while ((iochar = getchar()) != '\n')

{

if (iochar == ' ')

{

numwhites++;

putchar(iochar);

}

else if ((iochar >= '0') && (iochar <= '9'))

{

numdigits++;

putchar(iochar);

}

else if (('a' <= iochar) && (iochar <= 'z'))

{

numlower++;

putchar(iochar - 32);

}

else if (('A' <= iochar) && (iochar <= 'Z'))

{

numupper++;

putchar(iochar);

}

else

putchar(iochar);

}



printf("%d white characters, %d digits, ", numwhites, numdigits);

printf("%d lowercase have been converted to ", numlower);

printf("uppercase and %d uppercase.\n", numupper);

printf("\n\n");



return 0;

}





Hope that is what you needed :)
?
2016-05-26 04:02:03 UTC
It's hard to tell what's wrong without knowing what's happening. Does it fail to compile? If so, what are the error messages? Doesn't it link? Again, what are the error messages? Does it abort while running? With what symptoms? Does it produce wrong output? What does it look like? What input was given, also? Lots of stuff we have to guess at without knowing for sure. Sorry. PS: A suggestion: Instead of a struct CIRCLE and two one-method classes, why not make a class CIRCLE which not only contains the parameters, but the two methods? This is one of the features of object-oriented programming: the encapsulation of data and methods that operate on the data.

2009-03-07 12:42:43 UTC
Well, just by looking over it once I see that you're checking for EOF in your while loop, but you're not addressing a file source but an input string from the console. You should check for end of line instead:



while( ( iochar=getchar() ) != "\n" )

{



// --- do your while loop stuff here --- //



}



There might be other issues as well, but at least I help you with one bug.
abey
2009-03-07 12:51:29 UTC
hai,

friend i go through ur program.your logic is good but u didnt write what was the error.

first of all u have to open iostream.h

#include

iochar is int data type. change that to char.

so putchar() cannot use a int value.

k.

bye..

....

abey
gjmb1960
2009-03-07 12:47:22 UTC
i didnt bther to compile or run this , one thing that doesnt seem ok to me is that white space can also be \t \n etc.

ity looks ok to me, do you get compile errors ? can you give an . output that deviates from the expected ?
2009-03-07 12:43:18 UTC
I'm not sure but i think that in place of numwhites++;, numdigits++ and others like that, you have to write ++numwhites.

Hope that helps a bit..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...