| | |
Always defaulting to the else
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2008
Posts: 629
Reputation:
Solved Threads: 101
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)
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)
•
•
•
•
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
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...)?
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
it looks like finished business to me but i will say it anyway....
using enumeration, for example
what enumeration actually does is making your code much more readable.
consider this:
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:
and enter word RED on input, because facouriteColor is an integer.
you also cant use
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
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: C Syntax (Toggle Plain Text)
#include <stdio.h> enum colors {RED, BLUE, YELLOW, GREEN, ORANGE, VIOLET} favouriteColor; enum shape {TRIANGLE, SQUARE, RECTANGLE ,CIRCLE, OVAL} main () { shape favouriteShape; // This is just a demonstration of second way to define a variable favouriteColor = BLUE; favouriteShape = SQUARE; }
consider this:
C Syntax (Toggle Plain Text)
favouriteColor = 1; favouriteShape = 1;
you cannot write something like:
C Syntax (Toggle Plain Text)
printf ("What is your favourite color?"); scanf ("%d", favouriteColor);
you also cant use
scanf ("%s", favouriteColor); instead, because favouriteColor is far from being a string of chractersif 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. ![]() |
Similar Threads
- execute python script from shell/Interactive Window (Python)
- Exit Codes (Python)
- New Hard Drive, now what? (Apple Hardware)
- Browser redirect/about:blank/other problems... (Viruses, Spyware and other Nasties)
- Problems when viewing pages... (Web Browsers)
- wininet.dll after SP2 upgrade (Windows NT / 2000 / XP)
- Home page gets directed to index page (Viruses, Spyware and other Nasties)
- Great search homepage (HIjack log inside) (Viruses, Spyware and other Nasties)
- w2k server errors (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: C code program to Set/Change Linux time ???
- Next Thread: makes pointer from integer without a cast
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop interest kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab windows.h






