shiryu3, you need to stop giving out solutions. Logic is the very key to programming and it is something that any student must learn. If you give out solutions, you hamper their ability to think logically and make the logic bit work!
I admit I'm typing out solutions, but nothing that's going to give away the overall program. The requester here had a lot of broken C syntax so I felt it necessary that he be provided with something with proper syntax.
hey shir, thanks for all the help you've given me. My program was working perfect just a second ago and then I went to run it again and my main menu keeps being displayed twice in a row for some reason now!
So for example, I press a for student name, the name appears but when it goes to the menu it comes upCode:#include <stdio.h> int main() { int MenuOptions() ; char choice ; int number, i ; while ( ( choice = MenuOptions() ) != 'q' ) { switch(choice) { case 'a': printf("Name\n") ; break ; case 'b': printf("Date\n") ; break ; case 'c': printf("Enter a number: ") ; scanf("%d", &number) ; if ( number >= 0 && number <= 50 ) { for ( i = 0 ; i <= number ; i++ ) printf("%d ", i ); printf("\n") ; } else printf("Invalid number!\n") ; break ; default: break ; } } return 0 ; } int MenuOptions() { char letter ; printf("Press a for Student Name\n") ; printf("Press b Tutorial Date & Time\n") ; printf("Press c Positive Numbers\n") ; printf("Press q to Quit\n") ; scanf("%c", &letter) ; return letter ; }
Press a for Student Name
Press b Tutorial Date & Time
Press c Positive Numbers
Press q to Quit
Press a for Student Name
Press b Tutorial Date & Time
Press c Positive Numbers
Press q to Quit
the loop still works, i just can't see why it would display the menu twice?
Give a space in scanf like this-
When for the first time you enter 'a' and hit "ENTER" key this gets stored in the input buffer and the next call to scanf takes this as the next letter, maknig the default case to become true. That's why it breaks and asks the menu shows up twice.Code:scanf(" %c",&letter);
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
ahhh it worked!
thanks! I must have edited it without thinking, I would have never known that was the reason why the menu displays twice
thanks!
No problem DJ, please take the time to compare this working code with your original post and look over all the broken syntax issues.