Quick IF statement question (beginner)

This is a discussion on Quick IF statement question (beginner) within the C Programming forums, part of the General Programming Boards category; Not so. sizeof(char) != sizeof(int). What you mean is that character literals like 'a' are actually int values, not char ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Not so. sizeof(char) != sizeof(int). What you mean is that character literals like 'a' are actually int values, not char ones. And because char can be signed or unsigned by default, most character handling functions like tolower() take ints, not chars.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Not literally, of course. They have different sizes, but they're just integers both of them, and they're treated as such:

    Code:
    	int a = 'a';
    	char a2 = 'a';
    	int b = 70;
    	char b2 = 70;

  3. #18
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7
    I have tried rewriting it several times, and still the same problem occurs. Here is my latest attempt:

    Code:
    exitfunction()
    {
    char exitquestion;
    printf("\n\nDo you wanna quit?");
    exitquestion = getchar();
    
    	
    	if(exitquestion == 'N' || exitquestion == 'n')
    	
    	{
    		main();
    	}
    	
    		else
    	
    			if(exitquestion == 'Y' || exitquestion == 'y')
    			
    			{
    			
    				exit (0);			
    								
    			}
    			
    			else
    				
    				if(exitquestion != 'Y' && exitquestion != 'y' && exitquestion != 'N' && exitquestion != 'n')
    				
    				{
    				
    					printf("\n\nOnly [Y]es Or [N]o are acceptable inputs!");
    					exitfunction();
    					
    				}		
    }
    But it still exhibits the same behaviour. I am truly stumped. Anyone have any ideas or can suggest a better way to achieve the desired interface using a different method?

    Cheers.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is this "recursion instead of loop" sickness that has hit us right now.

    You DO NOT call main() again to repeat something within main. You do not call your own function to repeat it. In both cases, some form of loop construction should be used, such as while, for, do-while - even goto is better than calling recursively [but it's the least good solution, in my opinion].

    --
    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. #20
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7
    I tried writing it as a while loop but I couldn't work out how to make it work like the above if statements should.

    ie. Do you want to quit? Y/ N

    N then go back to main, repeat the program again.

    Y then exit.

    Neither then tell user that only Y/N can be input and ask the 'Do you want to quit?' question again.

    I could not think of any other way to achieve this.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jim.rattlehead View Post
    I tried writing it as a while loop but I couldn't work out how to make it work like the above if statements should.

    ie. Do you want to quit? Y/ N

    N then go back to main, repeat the program again.

    Y then exit.

    Neither then tell user that only Y/N can be input and ask the 'Do you want to quit?' question again.

    I could not think of any other way to achieve this.
    So, show us the code where you are doing that. What you show above is WRONG, WRONG, WRONG!

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

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    When do you bother to ask for help if you're not listening to us? We say "use a loop," you do "use lots of IFs."
    What part do you not understand? And you have yet to actually give a return type to exitfunction. Use void exitfunction if you don't plan on returning anything from it.

  8. #23
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7
    I just couldn't work out how to use a loop for this situation.

    I have this:

    Code:
    void exitquestion()
    {
         char exitquestion;
         printf("\n\nDo you wanna quit?");
         exitquestion = getchar();
         
         while (exitquestion != 'Y' && exitquestion != 'y' && exitquestion != 'N' && exitquestion != 'n')
               {
                             printf("\n\nOnly [Y]es Or [N]o are acceptable inputs!");
                             printf("\n\nDo you wanna quit?");
                             exitquestion = getchar();
               }
    }
    But I wouldn't know how to add the exit if 'y' and go back to main if 'n' without using if statements.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    You should have posted that before because it looks very correct.
    The preferred method for doing this is to let main handle it. exitquestion should return a bool or int, with true or 1 if the user wants to quit, and 0 or false if the user wants to continue.
    Inside main, have a loop that loops if exitquestion returns false or 0, otherwise it terminates the loop, cleans up and exits.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick, newbie question: Multiple line string
    By crummy in forum C# Programming
    Replies: 2
    Last Post: 03-10-2005, 05:58 AM
  2. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 01:54 AM
  3. * quick question *
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 05:58 PM
  4. just a reaaaaaaly quick question plz help
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2002, 10:39 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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