Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array and loop question from an absolute beginner!!!

    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;
    }

  • #2
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try whit 'getch' instead of 'getchar'. You will get better result.

    I hops it can help you!

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    getch? Do you mean getc? (which would actually be "getc(<var name>)" or "getc(stdin)")

  • #4
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •