FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Simple menu written in C - Problem with loop





carlospro7
I'm working on a program for a beginning C class. I'm trying to make a menu where the user can input a choice. The program does fine, but everytime a key is entered no matter which I get kicked out of the loop, therefore quiting my program, and I don't quite understand why. I want to stay in the loop unless i tell it to, by pressing q, but it doesn't seem to be working. Can anyone tell why my loop isn't working. It probably is a rookie mistake. Thank you in advanced

Code:

void menu ()
{
   int done = false;
   char choice;
   while (!done)
   {
      print_menu ();
      scanf (" %c", &choice);
      done = do_it (choice);  //Call function do_it
   }
}

void print_menu ()   //Prints menu on screen
{
   system ("clear"); //Clears system screen
   printf ("\nDate Program                      Carlos Espinoza");
   printf ("\n\nChoose one of the following:\n");
   printf ("\n  L: Test for Leap year.");
   printf ("\n  V: Valid date.");
   printf ("\n  Y: day in Year.");
   printf ("\n  W: Day in Week.");
   printf ("\n  E: Expanded date.");
   printf ("\n  Q: Quit.");
   printf ("\n\n  Enter a choice in { L, V, Y, W, E, Q } : ");
}

//The input character is sent
int do_it (char choice)
{
   int r_v = 0;
   switch (choice)
   {
      case 'q': case 'Q':
         r_v;
         break;
      default:
         printf ("\n  ERROR: Wrong input! Try again.");
         r_v = 1;
         break;
   }
}
HoboBarticus
dont you need a: return r_v; at the end there?
carlospro7
lol yeah, i just figured that out and felt really stupid and was about to delete this thread, but i guess i cant do that, oh well. thanks anyway
pspcoder05
Couldnt you just include conio.h and
replace system("cls") with clrscr() ?

This makes the code run faster.
Optimisation is as important as code logic mate.

Cool
Indi
pspcoder05 wrote:
Couldnt you just include conio.h and
replace system("cls") with clrscr() ?

This makes the code run faster.
Optimisation is as important as code logic mate.

Cool

Assuming it actually works. <conio.h> is not actually a standard C header, and clrsrc() is not actually a standard C function. It's only available on DOS/Windows compilers, and even then, not all of them, and even the ones it is included with don't have the same functions (note in the link i give that clrscr() is not included). If he's using a *nix based OS, for example, he can't use <conio.h> at all. (See: http://en.wikipedia.org/wiki/Conio.h)

The way he did it is still not entirely portable, but at least it will compile on every C compiler everywhere. Optimization is good, but not nearly as important as logic. Premature optimization can be a cause of great woe. Make it work, then make it fast. And always, always, use standard options before non-standard ones, unless you have an extremely good reason to go non-standard. "It can be faster" is not good reason.
eznet
Indi wrote:

Optimization is good, but not nearly as important as logic. Premature optimization can be a cause of great woe. Make it work, then make it fast. And always, always, use standard options before non-standard ones, unless you have an extremely good reason to go non-standard. "It can be faster" is not good reason.


Question, slightly off base - but here it goes never the less. You seem to give the impression that you are familiar, off hand, with the specifics of the language. I am a junior computer science major. I have taken 2 C++ classes and am currently in advanced logic with Java. My question is how does one find this level of familiarity with the languages as you have? Practice? Good books? Best practices? I would love to be able to know, off hand, where the classes and and pre-written functions are. I know that it requires work, but I often feel that I cannot get the information in there fast enough. Any tips?
Indi
eznet wrote:
Indi wrote:

Optimization is good, but not nearly as important as logic. Premature optimization can be a cause of great woe. Make it work, then make it fast. And always, always, use standard options before non-standard ones, unless you have an extremely good reason to go non-standard. "It can be faster" is not good reason.


Question, slightly off base - but here it goes never the less. You seem to give the impression that you are familiar, off hand, with the specifics of the language. I am a junior computer science major. I have taken 2 C++ classes and am currently in advanced logic with Java. My question is how does one find this level of familiarity with the languages as you have? Practice? Good books? Best practices? I would love to be able to know, off hand, where the classes and and pre-written functions are. I know that it requires work, but I often feel that I cannot get the information in there fast enough. Any tips?

The secret to being a good programmer - in any language, and in general - is "RTFM". Read the effin manual.

Now that sounds really kind of simple and silly, but it's actually really all you need to master programming, no matter what type of programming you do. Whenever you start a new programming language, project, whatever, the first thing you should do is get hold of good documentation about what you're doing. Without good documentation, you're not programming, you're just doing trial and error with a very expensive piece of technology.

And no, you don't really need to memorize anything. i don't. ^_^ Hell, you ask me to tell you the parameters to something as simple as strncpy() (or even whether it's strncpy() or strcpyn()) and i honestly don't know. i can't be arsed to retain that kind of information. But i can look it up in about 5 seconds. Space in my head is at a premium, so i don't want to waste it on minutae like that. Instead i memorize grander concepts and design patterns (and not even all of them), and consult documentation for everything else. (Of course, after time, some things you do retain, so you don't need to go back to the manuals FOREVER for every little thing - but not really by trying to memorize it, just because of the repetition.)

For C, you need (at least) a language reference, and a reference to the standard library - the C Runtime Library or C RTL. A good place to start is here: http://dinkumware.com/manuals/. The neat thing about that reference is that it's pretty clear what is actually standard C (or C++), and what is Dinkumware extensions. Another handy reference to have is the Rogue Wave docs that you can download from Borland here: http://info.borland.com/techpubs/bcppbuilder/v5/updates/pro.html. All you need is "B5LANG.ZIP" (C and C++ language reference), "B5RTL.ZIP" (C RTL reference) and "B5SCL.ZIP" (C++ STL reference, if you're doing C++), but you can just download the "core" package and get them all in one shot. Plus there's other stuff there, like Win32 API docs. The neat thing about these is that they're all help files you can keep on your computer even when you're not online. Of course, there are a ton of other references that you can find on Google, and they do come in handy.

Once you have those basic bits of your toolset, you just have to start collecting references like it's an addiction. You can never have too much. You don't even need to buy any books - most everything can be found free online (legally) - although if you want to, go ahead. i've found the help files and occasional web search work fine for me.

All that's left to do after that is use your references. Read them; familiarize yourself with them. Get in the habit of, whenever you have to do something new, looking at your references to see if the functionality you need is already provided. It depresses me how often programmers reinvent the wheel. (Even pros do it. i stood there and watched an extremely well-paid software development consultant write an algorithm to extract the mean from a set of (rational) numbers from scratch. i said nothing, but he could have done that in pretty much a single line of code if he'd just taken a moment to consult his STL reference, and saved himself all of the testing that he had to do.)

That's about it. 90% of programming is using references. And it's a transferrable skill, so once you learn how to use references properly for C and/or C++, the same reference-wrangling skills work when you're learning Java or Lisp or whatever. Master the art and science of using your documentation, and then all that's left before you functionally master any given language is just a little bit of practice.
eznet
Awesome! Thank you, that is what I needed to know (and wanted to hear). You have pretty much confirmed my suspicions. The method that you have described is exactly what I am currently doing. I just have little access to fellow programmers here in my town (and I am a social entity so I often find myself hashing details with people with WTF written on their faces) and so little input on best practices outside of the virtual world. I have quite an extensive library of reference documentation on a separate partition from my OS which grows every week in size from the constant adding of everything from HTML tutorials to compiler tools and libraries.

From what you have said I have inferred that I need to pretty much keep on the track/path that I am on. I will keep downloading info I feel pertinent and helpful and keep printing the jack daniels cheat sheets I have become dependent, at times, on.

Side question: I have been told my multiple individuals that I need to read the Pragmatic Programmer - from journeyman to master. I have been told that the concepts given are much the holy grail of programming. Hype or fact in your opinion?

Again, THANKS!
Indi
eznet wrote:
Side question: I have been told my multiple individuals that I need to read the Pragmatic Programmer - from journeyman to master. I have been told that the concepts given are much the holy grail of programming. Hype or fact in your opinion?

No idea. Never even heard of it.
redice
Use fgetc() , getchar() to get choose.
Scanf will wait for a "return" key. If you use a linux or unix like host, you may use a curses/libtermcap for the console menu.
wilan
I could be wrong but I don't think fgetc() or getchar() will solve the "instant" key press problem. They will be buffered input and as a result will only get executed when the input buffer is flushed, for example by a return key.

Also for the Pragmatic Programmer book - it's a good and worthy read, it's got some good tips and insights - some pretty obvious and some not so obvious. It's a light read and isn't dead serious so you can probably read it quickly if you have access to it.
Related topics
Database Privilege
Interesting problem
Programming in C problem
Will pay 100 FRIH$ for simple Windows program...
Dropdown menu in iframe problem
10 to 200 Frih for Simple Menu Icons!!!
[TUTORIAL] Simple HTML & CSS Menu
Zoo Chimp "planned" rock attack on visitors ....
Joomla! Menu setting up problem
Menu slide
JavaScript (Menu)
Ca avance
call by reference (C++)
Frames for any resolution
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.