a bunch of little things...

This is a discussion on a bunch of little things... within the C Programming forums, part of the General Programming Boards category; I had a couple of things while I was coding that I'm not sure about... 1 . Which looks better, ...

  1. #1
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597

    a bunch of little things...

    I had a couple of things while I was coding that I'm not sure about...

    1. Which looks better, assuming that quit is an integer, either 0 or 1. Also, is there a different way to do it that would allow me to use something besides an int? (I know, I can have a char that is either 'y' or 'Y'. So is this better:
    Code:
    while (!quit)
    {
      /* stuff here, then ask quit/continue */
    }
    or this:
    Code:
    while (quit == 0)
    {
      /* stuff here, then ask quit/continue */
    }
    2. Another thing: I was trying to get a choice (integer value) between 1 and 4, otherwise scan again, using the code:
    Code:
    while ( !(choice==1 || choice ==2 || choice ==3 || choice ==4))
      {
        printf("Enter your choice: ");
        scanf("%d", &choice);
      }
    ... would this be the same as:
    Code:
    while (choice < 1 && choice > 4)
      {
        printf("Enter your choice: ");
        scanf("%d", &choice);
      }
    Last edited by dbaryl; 05-14-2002 at 11:20 PM.
    This is my signature. Remind me to change it.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I like
    while(!quit)
    beacuse no =='s required.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Code:
    while (choice < 1 && choice > 4)
    I think you want this to be:
    Code:
    while (choice < 1 || choice > 4)
    Choice will never be less than one ANDgreater than 4
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A "safe'ish" example of scanf() use to get an int:
    Code:
    #include <stdio.h>
    #define MAX 10
    
    int main(void)
    {
    	int num1;
    
    	for (;;)
    	{
    		printf("Please enter an int between 0 and %d, or -1 to exit >", MAX);
    		if (scanf("%d", &num1) == 1)
    		{
    			if (num1 == -1) break;
    			if (num1 >= 0 && num1 <= MAX)
    				printf("You entered %d\n", num1);
    			else
    				printf("%d is out of range\n", num1);
    		}
    		else
    		{
    			printf("That was not a number\n");
    			while (getchar() != '\n');
    
    			/* Eat duff character from buffer */
    		}
    	}
    
    	return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Originally posted by fyodor
    Code:
    while (choice < 1 && choice > 4)
    I think you want this to be:
    Code:
    while (choice < 1 || choice > 4)
    Choice will never be less than one ANDgreater than 4
    Sorry, my mistake... I actually had the other code in the program, so wrote this one really quick, messed up.

    Hammer: when does the for ever end? Also, can you please explain the reason for the break here:
    Code:
    if (num1 == -1) break;
    Thanks.
    This is my signature. Remind me to change it.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by dbaryl
    Hammer: when does the for ever end? Also, can you please explain the reason for the break here:
    Code:
    if (num1 == -1) break;
    Thanks.
    Its stops right in front of ya eyes

    The prompt says:
    >printf("Please enter an int between 0 and %d, or -1 to exit >", MAX);

    The code says
    >if (num1 == -1) break;
    It is this break that gets you out of the infinite loop. Under normal circumstances you wouldn't ask the user to enter -1 to exit (not very friendly), this was just a snippet to show how it might work, and to try and highlight the need to check the return code from scanf(), which is something so many people seem to overlook.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Thanks a lot, Hammer, that clears it up. Guess I should start paying a bit more attention...

    Don't want to start anothe thread on this, but is declaring and initilizing at the same time wrong in C? Like so:
    Code:
    /#include <stdio.h>
    
    int main (void)
    {
      int x = 5;
    
      return(0);
    }
    I heard that here, from ygfperson, if I understood correctly:
    http://www.cprogramming.com/cboard/s...640#post110640

    [edit]Jeez, I misspelled "Hammer" as "Hummer", LOL [/edit]
    Last edited by dbaryl; 05-15-2002 at 02:03 PM.
    This is my signature. Remind me to change it.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by dbaryl
    ... but is declaring and initilizing at the same time wrong in C? Like so:
    Code:
    /#include <stdio.h>
    
    int main (void)
    {
      int x = 5;
    
      return(0);
    }
    Not sure on that one. I know you have to initialise at the top of the function, and you certainly can't do it within a for statement:
    >for (int i = 0; ..... <-- Wrong.
    I *thought* it was OK to declare and initialise at the same time at the top of the function though. I haven't had a compiler moan about it yet... maybe someone will put us straight.

    Oh yeah, Hummer
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Which looks better, assuming that quit is an integer, either 0 or 1.
    I prefer
    Code:
    while (quit == 0)
    {
      /* stuff here, then ask quit/continue */
    }
    Simply because I'm so pedantic that I don't allow Lint warnings in my code. while ( !quit ) will flag a warning saying that the operand of ! is not a boolean.

    >is declaring and initilizing at the same time wrong in C?
    If it's wrong, I don't want to be right. I can't see how such a useful feature would be considered bad style since initializing in the declaration and initializing after the declaration both take the same time and resources to execute. I imagine which you use is a matter of personal preference, but initializing variables when you create them is a good practice for debugging. If you initialize to a wacko value that would take a miracle for it to be naturally assigned to the variable in your code then you can much more easily find where a value gets corrupted and/or if you forget to give it a more useful value before using it.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  3. Getting a percentage from a bunch of random numbers?
    By kidkash in forum C Programming
    Replies: 6
    Last Post: 02-11-2003, 07:42 AM
  4. things to think about... [from www.nuclearwastesite.com]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-10-2001, 11:47 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21