Results 1 to 4 of 4

Thread: Weird...

  1. #1
    AnT
    Guest

    Weird...

    PHP Code:
    //---------------------------------------------------------------------------
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #pragma hdrstop

    //---------------------------------------------------------------------------

    #pragma argsused
    int main(int argccharargv[])
    {
       
    char cChar[1];
       
    start:
       
    clrscr();
       
    printf("How ya doin?\nI'm doin fine.\n");
       
    printf("Press Q or q to exit: ");
       
    gets(cChar);
       goto 
    end;
       
    end:
       if(*
    cChar == 'Q' || *cChar == 'q')
          {
          return 
    0;
          }
       else
          {
          
    MessageBox(NULL"You Pressed " + *cChar"Test"MB_OK);
          goto 
    start;
          }
    }
    //--------------------------------------------------------------------------- 
    I'm using this, but when I run it, and press a key other than Q or q, it gives me something like $qrii instead of like "You Pressed A", or something. Can someone tell me what I'm doing wrong? As you can see, I'm a complete newb, so any help is greatly appreciated.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    read up strings in the tutorial/faq at the top of the page. You can't use normal operators to manipulate pure strings.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Zaei
    Guest
    Dont use labels and gotos. Your code should look like this:
    Code:
    int main(...)
    {
       char cChar[2];
       char buff[64];
       while(true) // infinite loop
       {
           clrscr();
           printf("How ya doin?\nI'm doin fine.\n");
           printf("Press Q or q to exit: ");
           gets(cChar);
           if(*cChar == 'Q' || *cChar == 'q')
           {
              return 0; // break the loop, and quit
           }
           else
           {
              sprintf(buff, "You Pressed %s", cChar);
              MessageBox(NULL, buff, "Test", MB_OK);
            }
        }
    }
    You might have to swap the literal "You Pressed %s" with buff in the call to sprintf, because I dont remember which comes first.

    Z.

    [eidt]
    fixed the code sample

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    buff should be first
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width