...

array and loop question from an absolute beginner!!!

fish
03-29-2003, 01:23 PM
Hi there,

could someone help me please, Ive just stared with c and I want the function to accept upto 50 characters but finish when i hit return and then to change the case of them.

It continues to accept characters after i have hit return up until the array of 50 is finished.

My next problem is that it seems to loop outside the loop and repeat the whole process again from the initial printf statement.

any suggestions would be great.

MAX_LINE_LENGTH = 50


code:--------------------------------------------------------------------------------

int inverseCharacters()
{

char ch[MAX_LINE_LENGTH];
int i= 0;

printf("%s", "\nEnter a line of text combining upper and lower case: \n");
//scanf("%s", &ch);


while (i <= MAX_LINE_LENGTH)
{
ch[i]= getchar();

if(islower(ch[i]))
{
ch[i] = toupper(ch[i]);
putchar(ch[i]);

}
else
{
ch[i] = tolower(ch[i]);
putchar(ch[i]);
}
i++;
}

return 0;
}

willie
03-30-2003, 07:05 PM
Try whit 'getch' instead of 'getchar'. You will get better result.

I hops it can help you!:thumbsup:

Phantom
03-31-2003, 11:27 AM
getch? Do you mean getc? (which would actually be "getc(<var name>)" or "getc(stdin)") :)

willie
04-01-2003, 10:23 AM
Like this!

while (i <= MAX_LINE_LENGTH)
{
ch[i]= getch();

if(islower(ch[i]))
{
ch[i] = toupper(ch[i]);
putchar(ch[i]);

}

Be sure to include <conio.h>

By:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum