Always defaulting to the else

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Always defaulting to the else

 
0
  #11
Jan 7th, 2009
I want to ask them with a prompt, "What day is it?

I then want them to enter Sunday, Monday, etc.

I used %c because I was trying to reading the name of the day, which would be a set of characters.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 629
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 101
Murtan Murtan is offline Offline
Practically a Master Poster

Re: Always defaulting to the else

 
0
  #12
Jan 7th, 2009
You will have to declare an array of characters to input into, you can't input directly into the enum.

The % format for an array of characters is %s

Then you will have to compare what they typed against a list of strings you have to 'look up' the proper enum.

What do you want to do if the user enters a string that doesn't match any of the days?

(What day is it? Orange)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Always defaulting to the else

 
0
  #13
Jan 8th, 2009
Originally Posted by RenFromPenn View Post
I want to ask them with a prompt, "What day is it?

I then want them to enter Sunday, Monday, etc.

I used %c because I was trying to reading the name of the day, which would be a set of characters.
No, you don't want this. At least not in total.
First, %c reads a single character
Second, %s reads a string, but you don't want to use it. Here's why
Third, it also makes your code much more complicated. You have to read in the string, then convert that string to the correct integer. Instead, how about prompting:
What day is it (0=Sunday, 1=Monsay...)?
Last edited by WaltP; Jan 8th, 2009 at 11:22 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Always defaulting to the else

 
0
  #14
Jan 9th, 2009
Originally Posted by WaltP View Post
No, you don't want this. At least not in total.
First, %c reads a single character
Second, %s reads a string, but you don't want to use it. Here's why
Third, it also makes your code much more complicated. You have to read in the string, then convert that string to the correct integer. Instead, how about prompting:
What day is it (0=Sunday, 1=Monsay...)?
That is how I had already decided to set it up since asking for the day by name wasn't working.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Always defaulting to the else

 
0
  #15
Jan 12th, 2009
delete this
Last edited by Alibeg; Jan 12th, 2009 at 8:20 am. Reason: mistake
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Always defaulting to the else

 
0
  #16
Jan 12th, 2009
it looks like finished business to me but i will say it anyway....
using enumeration, for example enum colors {RED, BLUE, YELLOW, GREEN} favouriteColor; you actually make an integer variable that can be assigned integer values using keywords you define. to make it simple:
  1. #include <stdio.h>
  2.  
  3. enum colors {RED, BLUE, YELLOW, GREEN, ORANGE, VIOLET} favouriteColor;
  4. enum shape {TRIANGLE, SQUARE, RECTANGLE ,CIRCLE, OVAL}
  5.  
  6. main ()
  7. {
  8. shape favouriteShape; // This is just a demonstration of second way to define a variable
  9.  
  10. favouriteColor = BLUE;
  11. favouriteShape = SQUARE;
  12. }
what enumeration actually does is making your code much more readable.
consider this:
  1. favouriteColor = 1;
  2. favouriteShape = 1;
and if you have tons of statements like these you will soon find yourself in mass of numbers where you dont know what numbers actually mean, and you would have to go to enumeration on the begining of the code to say: "Ah, yes...2 was YELLOW."

you cannot write something like:
  1. printf ("What is your favourite color?");
  2. scanf ("%d", favouriteColor);
and enter word RED on input, because facouriteColor is an integer.
you also cant use scanf ("%s", favouriteColor); instead, because favouriteColor is far from being a string of chracters

if you want to use form you mentioned on the begining, something like: "What day is it?", you should use array of chars for input, and than compare it with constant strings: "Sunday", "Monday" ... using function strcmp(string1, string2) , where strcmp evaluates (equals) 0 if they are the same, <0 if string1<string2 and >0 if string1>string2 is greater (by their ascii values). you can also use strcmpl(string1, string2) , it is same as first one but doesnt take small and capital letters into account. i dont have any other methods for this on my mind right now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC