array and loop question from an absolute beginner!!!

  • fish
  • Newbie
  • Newbie
  • fish
  • Posts: 5

Post 3+ Months Ago

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.
Code: [ Select ]
MAX_LINE_LENGTH = 50



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;
}
  1. MAX_LINE_LENGTH = 50
  2. int inverseCharacters()
  3. {
  4.     char ch[MAX_LINE_LENGTH];
  5.     int i= 0;
  6.     printf("%s", "\nEnter a line of text combining upper and lower case: \n");
  7.     //scanf("%s", &ch);
  8.     while (i <= MAX_LINE_LENGTH)
  9.     {
  10.         ch[i]= getchar();
  11.         if(islower(ch[i]))
  12.         {
  13.             ch[i] = toupper(ch[i]);
  14.             putchar(ch[i]);
  15.         }
  16.         else
  17.         {
  18.             ch[i] = tolower(ch[i]);
  19.             putchar(ch[i]);
  20.         }
  21.         i++;
  22.     }
  23.     return 0;
  24. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Well I would approach it a little bit differently if you wanted to allow the person to enter characters until they hit enter. I would do something like this:

Code: [ Select ]
MAX_LINE_LENGTH = 50

int main (int argc, char * argv[])
{
  char ch[MAX_LINE_LENGTH];

  //loop thrugh argv array which holds all characters on line
  for(int i=0; i < argc && i < MAX_LINE_LENGTH; i++) {
   ch[i] = argv[i];

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

  return 0;
}
  1. MAX_LINE_LENGTH = 50
  2. int main (int argc, char * argv[])
  3. {
  4.   char ch[MAX_LINE_LENGTH];
  5.   //loop thrugh argv array which holds all characters on line
  6.   for(int i=0; i < argc && i < MAX_LINE_LENGTH; i++) {
  7.    ch[i] = argv[i];
  8.    if(islower(ch[i])) {
  9.      ch[i] = toupper(ch[i]);
  10.      putchar(ch[i]);
  11.    }
  12.    else
  13.    {
  14.      ch[i] = tolower(ch[i]);
  15.      putchar(ch[i]);
  16.    }
  17.   }
  18.   return 0;
  19. }


Argv holds all your characters from your line and argc stores the number of characters that has been entered on the line. If you have any questions on something be sure to ask. I just kind of took your code reorganized it in this one.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

By the way, the way you were doing it was that you were looping through a loop 50 times that was calling

ch[i]= getchar();

and that is exactly why you would have continue to hit return until 50 characters had been entered.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Also I just noticed you are wanting to use this possibly as a function. So if that is true, then the code I wrote for you would have to be modified some, and it would kind of depend on how your program works. You might be able to simply pass the argc and argv to the function from the main block for which u can work with.

If you want to use the getchar() statement like you were doing, you would need to change the while loop some. What you would do is have it look for the return character and then jump out of the loop once it finds it. You could do that I think by going

Code: [ Select ]
while (i <= MAX_LINE_LENGTH && (ch[i]= getchar()) != '\13') {
....
}
  1. while (i <= MAX_LINE_LENGTH && (ch[i]= getchar()) != '\13') {
  2. ....
  3. }
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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


As far as that problem, at first glance I do not see anything that would cause that. Are you sure this infinite loop isn't being caused by the place where you are calling the function? Probably somewhere within main() ?
  • ED
  • Student
  • Student
  • ED
  • Posts: 66
  • Loc: Halifax

Post 3+ Months Ago

Hey fish !
do you post on x-nations ?
  • fish
  • Newbie
  • Newbie
  • fish
  • Posts: 5

Post 3+ Months Ago

I think i finally worked it out, i used fgets() then a
for(i = 0; i < MAX_LINE, ch[i] !='\0'; i++)

it has stopped my problem of accepting characters after <return> was entered if the 50 spaces in the array weren't filled.

thanks so much for your help.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Glad you solved your problem.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.