943,553 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1354
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 7th, 2009
0

Re: Always defaulting to the else

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 7th, 2009
0

Re: Always defaulting to the else

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)
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 8th, 2009
0

Re: Always defaulting to the else

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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Jan 9th, 2009
0

Re: Always defaulting to the else

Click to Expand / Collapse  Quote originally posted by WaltP ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 12th, 2009
0

Re: Always defaulting to the else

delete this
Last edited by Alibeg; Jan 12th, 2009 at 8:20 am. Reason: mistake
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Jan 12th, 2009
0

Re: Always defaulting to the else

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C code program to Set/Change Linux time ???
Next Thread in C Forum Timeline: makes pointer from integer without a cast





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC