Thread: two general questions

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    Question two general questions

    First question:
    I have a while loop that doesn't work, and I'm almost ceratin it's because of my test statement. I'm just wondering if this is ever going to be a legal statement.

    //code//

    do {
    key=getch();
    } while (key!= '1' || '2' || '3');

    //endcode//

    Question two:
    I have a function that puts a character on a certain part of the screen using gotoxy(xval,yval). Would it be possible to return both xval and yval at the end of the function and use them both in main as normal variables with their return values??

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm just wondering if this is ever going to be a legal statement.
    It never has been and never will be. Try this instead:
    Code:
    do {
      key=getch();
    } while ( key != '1' && key != '2' && key != '3' );
    Or better yet, write a function to validate key and use the return value in your loop condition for readability and maintenance purposes:
    Code:
    do {
      key=getch();
    } while ( !keyCheck ( key ) );
    >Would it be possible to return both xval and yval at the end of the function
    Yes, but it would take a bit of fiddling. A better way would be to store both xval and yval in a struct and then return that struct so that both values can be easily returned AND they remain bound to each other as it should be:
    Code:
    struct SCREEN_COORD
    {
      int xval,
          yval;
    } coord;
    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > > I'm just wondering if this is ever going to be a legal statement.
    > It never has been and never will be. Try this instead:

    Um. Wrong. It IS a legal statement. If it weren't, it'd give you compiler errors and or warnings, and possibly not compile. However, what you meant to say was:

    "I'm wondering if this will ever perform the check I expect it to?"

    To which, Prelude's response would be correct. However, it IS a legal C statement. It is valid. It will compile and run. It will not give you the result you expect however.[/picky]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It IS a legal statement
    True, but can you think of a situation where that particular construct would be useful in any way? It is legal by the syntax of the language, but wrong by just about any other measure you care to use.

    >If it weren't, it'd give you compiler errors and or warnings, and possibly not compile
    Any decent compiler will definitely warn about a conditional expression being a constant.

    >[/picky]
    I'll grant you that. But I'm curious as to exactly what you consider legal. I consider legal to be both syntactically and logically correct since we're not just talking about the language in and of itself, but how it is used. void main is legal according to the syntax of the language, but logically it's stupid. I know this is similar to asking you how you define "is', but I will anyway.

    -Prelude
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Any decent compiler will definitely warn about a conditional
    > expression being a constant.

    So in other words, a "decent compiler" should warn on:

    while( 1 )

    Right?

    As far as what was "right", I was referring to it being leagal as far as C is concerned. Actually, I'd disagree as to "void main" being right at all.

    The reason here is that, if I recall correctly, the standard states that main should return an integer. That's why I'd say that void main is incorrect.

    I don't think that you'll find anywhere in the standard or what not that says that that comparison is illegal.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So in other words, a "decent compiler" should warn on:
    >while( 1 )
    >Right?
    All of the compiler's I've used do so, and all syntactic/semantic checkers warn about it. This is more often a logic error than it is a design feature, so it makes sense to warn about it in much the same way assignment in a conditional is warned about.

    >I was referring to it being leagal as far as C is concerned.
    And I was referring to it being both legal *and* correct, it isn't both so I responded with no. Though I will admit I could have worded it much better to avoid confusion.

    >Actually, I'd disagree as to "void main" being right at all.
    I agree that void main is not right, but it is legal. According to the ANSI/ISO standard, main returns an int. But void main is not a constraint violation or syntax error, so it's perfectly legal by the use of the language even though it isn't standard and may result in nasal demons.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Thanks for all your help. I changed the loop structure to what you first posted, Prelude, and now the loop doesn't exit. It looks to me like I typed it all correct, but when I type in one of the numbers it doesn't recognize them. If you'll tell me the specific code tag I'm supposed to use, it would help me too.

    //code//
    while (key!= '1' && key!= '2' && key!= '3') {
    key=atoi(getchar());
    }
    clrscr();
    switch (key) {
    case 1:
    printf("Enter your name: ");
    break;
    case 2:
    printf("Which file? ");
    break;
    case 3:
    exit;
    default:
    printf("This shouldn't have happened");
    }
    //endcode//

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I hate to poke my head in, but please use code tags.
    Loops and the like get badly distorted without them.
    The world is waiting. I must leave you now.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    while (key!= '1' && key!= '2' && key!= '3') { 
      key=atoi(getchar()); 
    }
    This is nasty and probably won't compile, much less run. The atoi function takes a const char * as it's argument, which is wildly different from the int that getchar returns. My question is why are you even trying to convert key to an int if you are testing it as a char? You could just say
    Code:
    key = getchar();
    while ( key != '1' && key != '2' && key != '3' ) {
      key = getchar();
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password program / general programming questions
    By plr112387 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2007, 10:10 PM
  2. A Few General C Questions
    By Jedijacob in forum C Programming
    Replies: 13
    Last Post: 02-17-2005, 02:47 AM
  3. a few opengl(or general programming) questions
    By linuxdude in forum Game Programming
    Replies: 20
    Last Post: 06-14-2004, 07:47 AM
  4. FAQ: Examples of good questions (General)
    By Prelude in forum FAQ Board
    Replies: 1
    Last Post: 10-11-2002, 08:57 PM
  5. General Dev Questions
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2002, 08:06 PM