![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 8
| Code: #include<stdio.h>
main()
{
int i;
char str[]="You are correct!";
char str2[]="You are incorrect, please start over";
char str3[]="yes";
char str4[]="no";
char str5[5];
printf("Welcome to the quiz\n");
printf("There are 5 questions\nplease type yes or no to the questions\n");
printf("If you get a question wrong, start over!\n");
printf("I am a girl. yes or no?\n");//question 1
gets(str5);
i = 0;
if (str5 == str4){
printf"Please start over\n");
any other improvements on my code would be nice, im always happy to learn Thanks! |
| Sharmz is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| If you feel like being structured, you want to use a loop. You can use a goto, if you have the proper safety equipment. Also, you should note that the condition "str5==str4" will always and forever be false. Two different arrays (which str4 and str5 are) can never compare equal. To compare the strings that str4 and str5 may be, you should use strcmp in the <string.h> header. |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jul 2008
Posts: 8
| I fixed up the comparison of the strings but still am confused about the jumping. how is the goto used? |
| Sharmz is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| If you search the board using "goto" as a keyword, you will find some examples of its use. Further than that I will not go. |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: Sep 2006
Posts: 2,506
| The main() function calls the menu() function. The menu() function has a huge do-while loop in it, so everything branches out from the menu() according to the user's choice, and then it returns to the large do-while loop, back in menu. Code:
int menu() {
char choice;
//etc.
do {
system("cls");
print your header here
print a space or two
print the body of your menu, here
Get user's choice
choice = tolower(choice);
switch(choice) {
case 'a': handle_case_a(); output_from_a(); break;
case 'b': handle_case_b(); break;
//etc.
default: print "Oops! I don't have a response for that choice. Please try again."
}
}while(choice != 'q');
return 0;
}
Last edited by Adak; 07-16-2008 at 01:07 AM. |
| Adak is online now | |
| | #6 |
| Registered User Join Date: Jul 2008
Posts: 8
| after the first post I resulted to goto but i know its a bad habit so I need to change it!! here is my program with goto Code: next:
do{
printf("Magnum opus means great work. yes or no?\n");//question 1
gets(str5);
} while (strcmp (str3, str5) != 0);//correct answer is yes
puts ("Correct answer!");
printf("A paragon is a self contradictory statement. yes or no?\n");//question 2
gets(str5);
if (strcmp (str4, str5) != 0){ //correct answer is no
printf("%s\n", str2);
c = getc(stdin);
goto next;
}
else{
printf("A dystopia is a perfect world. yes or no?\n");
}
gets(str5);
if (strcmp (str4, str5) != 0){ //correct answer is no
printf("%s\n", str2);
c = getc(stdin);
goto next;
}
|
| Sharmz is offline | |
| | #7 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Adak's post should let you figure out how to eliminate the goto. Another hint: Code: do {
/* ... */
} while(quit == 0);
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #8 |
| Registered User Join Date: Sep 2006
Posts: 2,506
| Instead of the ugly line "goto next", why not use the more elegant "continue". ![]() I have nothing against goto's - they are exactly what the assembly language is probably using. But we're not assembly compilers, and we don't want goto's in a program except in a very few circumstances, because they are SO EASY to abuse, and make the resulting program SO HARD to follow, when you have to modify/extend/or correct it. True story: When I was learning to program, the instructor related how he had been offered $50,000 if he would re-write a current program in use by the college. (This was when $50,000 was worth 3-5 X as much as it is, today - so a *lot* of money.) You guessed it, it was an older program, that was riddled with goto's - goto's here, goto's there, goto's coming out your ears, nose, and well, you name it. ![]() He declined, and so did every contractor who reviewed samples of the source code. The "noodle" effect of the goto's was just *unbelievable*. The program was eventually re-written from the ground up, because of it. |
| Adak is online now | |
| | #9 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| gotos and gets() - ugh. Worse still, while you're still proud of it, you're beyond help.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #10 |
| * Join Date: Jun 2008
Posts: 108
| One improvement is to avoid cryptic names for your variables. str4, str3, are not very descriptive. Name them something humanreadable like 'yes_answer', 'no_answer'. |
| noops is offline | |
![]() |
| Tags |
| noob, parts, program |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| my server program auto shut down | hanhao | Networking/Device Communication | 1 | 03-13-2004 10:49 PM |
| fopen(); | GanglyLamb | C Programming | 8 | 11-03-2002 12:39 PM |
| Clocking a program, and parts of it | Boksha | C Programming | 4 | 03-18-2002 06:02 PM |
| My program, anyhelp | @licomb | C Programming | 14 | 08-14-2001 10:04 PM |