Thread: problem with reading characters

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Unhappy problem with reading characters

    can any body tell me why this code is not reading the characters properly

    Code:
    #include <stdio.h>
    void swap(void *,void *,int );
    
    int main()
    {
        int num1,num2;
        char c1,c2;
        float f1,f2;
        int opt;
        
        printf("Enter a option to swap integers or floats or characters\n 1 for integers\n 3 for characters:");
        scanf("%d",&opt);
        
        switch(opt)
        {
                   case 1: printf("Enter the two integers:");
                           scanf("%d %d",&num1,&num2);
                           printf("The values before swapping are N1 = %d and N2 = %d\n",num1,num2);
                           swap(&num1,&num2,1);
                           break;
                   case 3: printf("Enter the two characters:");
                           scanf("%c %c",&c1,&c2);
                           printf("The characters before swapping are C1 = %c and C2 = %c",c1,c2);
                           swap(&c1,&c2,3);
                           break;
                   default: printf("Wrong option..please run again");
                            break;
        }
        getch();
        return 0;
    }
    
    void swap(void *vptr1,void *vptr2,int k)
    {
         if(k==1)
         {
                 (*(int *)vptr1) = (*(int *)vptr1)^(*(int *)vptr2);
                 (*(int *)vptr2) = (*(int *)vptr1)^(*(int *)vptr2);
                 (*(int *)vptr1) = (*(int *)vptr1)^(*(int *)vptr2);
                 printf("The Values after swapping are N1 = %d and N2 = %d",(*(int *)vptr1),(*(int *)vptr2));
         }
         else if(k==3)
         {
              (*(char *)vptr1) = (*(char *)vptr1)^(*(char *)vptr2);
              (*(char *)vptr2) = (*(char *)vptr1)^(*(char *)vptr2);
              (*(char *)vptr1) = (*(char *)vptr1)^(*(char *)vptr2);
              printf("The characters after swapping are C1 = %c and C2 = %c",(*(char *)vptr1),(*(char *)vptr2));
         }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you define what you mean by "not reading characters properly"?


    Edit: And surely this:
    Code:
                 (*(int *)vptr1) = (*(int *)vptr1)^(*(int *)vptr2);
                 (*(int *)vptr2) = (*(int *)vptr1)^(*(int *)vptr2);
                 (*(int *)vptr1) = (*(int *)vptr1)^(*(int *)vptr2);
    is much easier to read as:
    Code:
    int temp = *(int *)vptr1;
    *(int* )vptr1 = *(int *)vptr2;
    *(int *) vptr2 = temp;
    Since temp is most likely going to be a register, it is also very likely to run faster than the XOR code.

    Of course, I would probably do this:
    Code:
         int temp;
         int *iptr1 = vptr1;
         int *iptr2 = vptr2;
         temp = *iptr1;
         *iptr1 = *iptr2;
         *iptr2 = temp;
    That makes the code even easier to read. No difference in the code generated, I'd expect.

    --
    Mats
    Last edited by matsp; 03-31-2009 at 05:23 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    printf("Enter the two characters:");
    scanf("%c %c",&c1,&c2);
    printf("The characters before swapping are C1 = %c and C2 = %c",c1,c2);
    swap(&c1,&c2,3);
    break;
    its not taking the inputs properly and i know using a temp is enough but its the requirement thats says not to use any typeof temporary variable

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csvraju View Post
    its not taking the inputs properly and i know using a temp is enough but its the requirement thats says not to use any typeof temporary variable
    That's typical "teacher teaching 'clever' things that then don't actually work in reality" teaching. NOT using a temporary is detrimental to performance, as has been proven a few times on this forum. It doesn't produce shorter code, or better code in any other way, and people won't understand the code. Where is the benefit in learning this? I find it really disturbing that there are teachers like this.

    I have worked in this industry [professional programmer, working on OS Kernels, Drivers and Real Time systems for the last 16 years] for over 20 years, and I have never encountered a situation where there is a NEED to NOT use a temporary. If I saw code that uses XOR to swap variables in a piece of production code here where I work right now, I would recommend that it was taken out and replaced with the corresponding "most obvious" code.

    Please print this and show to your teacher.

    Can you now describe, in more detail what "not taking inputs correctly" actually means. Is it perchance reading the newline from a previous scanf?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    "teacher" probably wanted the students to struggle and find the "XOR solution" on themselves...to exercise and enhance their arithmetic(or in this case boolean-algebra skills)

    i remember my teacher asking us to do the same...when in high-school in BASIC.

    i (and lots of others) had come up with
    Code:
    10 let a=5
    20 let b=10
    30 a = a+ b
    40 b = a - b
    50 a = a - b
    60 print "a=";a;"b=";b
    70 end
    some others had come up with a*b,a/b,a/b...
    ahh the sweet old days...sweet old gwbasic!

    @csvraju

    you should be getting error during compilation that getch() is an undefined function...
    Can you now describe, in more detail what "not taking inputs correctly" actually means. Is it perchance reading the newline from a previous scanf?
    yes.

    the problem lies in scanf("%d",&opt);

    add a space after %d
    scanf("%d ",&opt);
    Last edited by creeping death; 03-31-2009 at 08:03 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  2. Problem reading input
    By gp364481 in forum C Programming
    Replies: 3
    Last Post: 09-24-2008, 05:10 PM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. Problem in reading Character
    By Bargi in forum C Programming
    Replies: 5
    Last Post: 04-10-2008, 11:40 PM
  5. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM