Thread: I need help with simple program if can please

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    I need help with simple program if can please

    i can move the code around and make the 'q' a number 10 or i could use the switch case itd be much easier but i wanna struggle with if statements for now. Why wouldnt the appointed line work? why why?
    Code:
    // TheMenuConSuitchCais.cpp : Defines the entry point for the console application.
    
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    void desicionTime (void);
    
    int main()
    {
    	for(;;)
    	{
    	char input;
    	int choice;
    	char choicee;
    
    	printf("\n\n	0. PreLab\n");
    	printf("	1. The Menu\n");
    	printf("	2. Switch Case\n");
    	printf("	3. Random\n");
    	printf("	4. Conversion\n");
    	printf("	5. Game of Chance\n");
    	printf("	6. Function Gnenerator\n");
    	printf("	7. Attributes Menu\n");
    	printf("	8. Resistor Color Code\n");
    	printf("	9. Regression Line Crap\n");
    	printf("	q. Press q to quit: \n\n");
    
    	printf("	Choose an option and press enter please:");
    	scanf(" &#37;d", &choice) || scanf(" %c", %choicee); /*<-----WHY WOULDNT THIS LINE WORK?*/
    		if (choice==10)
    		{
    		break;
    		}
    		if (choicee==q)
    		{
    		system("cls");
    		desicionTime();
    		}
    	}
    	return (0);
    }
    Last edited by tru.cutru; 06-26-2008 at 10:03 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because it's wrong.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    why is it wrong? i put the 'q' i just took them out by mistake when i put it on the thread

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It's wrong because it's not right. From a purely syntactical approach, I can point out that || only works for boolean expressions of sorts. It's rather meaningless to put it together otherwise. Also, the &#37; in front of your variable name makes no sense. It should be an & instead.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    oh okay thanks. So is only use for boolean? is not use for anything other than boolean expressions. Thank you

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by tru.cutru View Post
    oh okay thanks. So is only use for boolean? is not use for anything other than boolean expressions. Thank you
    '||' is called logical operator..if it would have allowed other expression do you think it is necessary to call it as boolean operator...

    Also always output of expression is true and false

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Technically, I think the above code would work as far as the pointed line is concerned [subject to the typo of using % instead of & of course] - it will try to read a numeric input [but I think the space before %d is perhaps a bit unneeded and may lead to problems], and if that fails, it will perform the scanf with %c. This is because the compiler guarantees that || and && are "short circuited", meaning that it will only perform operations UNTIL such a point that it's satisfied it knows the outcome. If the first scanf returns non-zero (that is, it was successful), then it will not do the second scanf(). If the first scanf was unsuccessful (no valid digits), then it would continue and do the second scanf.

    I strongly believe that the OP would have written this code in understanding the meaning of it and understanding WHY it would have worked, had there not been a typo there.

    However, it is NOT good programming style [in fact, I would send such code back to the apprentice that wrote it for a rewrite, if I had an apprentice working for me - no senior programmer SHOULD write code like that]. Particularly as there's absolutely no need for it. You can just as well read all the input as %c, and deal with '0'..'9', 'q', 'Q' as inputs, or renumber your menu to have more than 1 digit and no "letters", and read it as an integer. That way, you need only one scanf-statement.

    --
    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.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Quote Originally Posted by matsp View Post
    Technically, I think the above code would work as far as the pointed line is concerned [subject to the typo of using % instead of & of course] - it will try to read a numeric input [but I think the space before %d is perhaps a bit unneeded and may lead to problems], and if that fails, it will perform the scanf with %c. This is because the compiler guarantees that || and && are "short circuited", meaning that it will only perform operations UNTIL such a point that it's satisfied it knows the outcome. If the first scanf returns non-zero (that is, it was successful), then it will not do the second scanf(). If the first scanf was unsuccessful (no valid digits), then it would continue and do the second scanf.

    I strongly believe that the OP would have written this code in understanding the meaning of it and understanding WHY it would have worked, had there not been a typo there.

    However, it is NOT good programming style [in fact, I would send such code back to the apprentice that wrote it for a rewrite, if I had an apprentice working for me - no senior programmer SHOULD write code like that]. Particularly as there's absolutely no need for it. You can just as well read all the input as %c, and deal with '0'..'9', 'q', 'Q' as inputs, or renumber your menu to have more than 1 digit and no "letters", and read it as an integer. That way, you need only one scanf-statement.

    --
    Mats
    the % instead of the & was indeed a typo (I had moved on and changed my code, and I clicked undo several times and modified the code, only so I could post my question). Actually I've encountered errors when I dont include the space after the quotation in scanf and also when I dont leave out space after the comma in scanf (this is suing Visual Studio 2008)

    Actually the above code ran but it crashed after the right expression of the OR logical operator. (whenever I would choose 'q')

    Can you extend more on why the logical || and && operator are considered "short circuited" I dont quite follow you.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    char* p = NULL;
    if (p && p->something())

    The p->something() will never execute because p == NULL and the left side of the && fails.
    The same goes for ||.
    The compiler will only parse and execute the if until it finds a condition to be false and then aborts (ie, doesn't execute the rest).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A slight clarification for Elysia's explanation for || is that a || b will not have b evaluated if a is true. This is opposite from the case for &&.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM