New Posts  All Forums:Forum Nav:

Zombie program in C

post #1 of 15
Thread Starter 
Hi all
i am trying to use a switch statement to ask if a player wants to play again
Code:
//play again
                printf("would you like to play again?: 1\\0\n");
                scanf("%d", &pagain);
                
                        switch(pagain){
                                case 1:
                                play = 1;
                                printf("success!\n");
                                break;

                                case 0:
                                printf("Goodbye!\n");
                                exit(0);
                                break;
                                
                                default: 
                                printf("please enter a valid key\n");
                                                                
                        }



however when the user hits anything but 1 or 0, cmd goes into a loop of the whole quiz, repasting everything over and over thus the title (zombie program) was wondering if anyone can see something obvious?

I realize that default is meant to be run in the event 1 or 0 have not been pressed, i even tried putting the switch inside a "while(x == 1)" and when the default is run "x = 1;" until one other cases changed it back to " x = 0; "

thanks much Dom
post #2 of 15
Thread Starter 
Bump
post #3 of 15
You need a break; at the end of default otherwise it will not exit the loop.
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
post #4 of 15
Thread Starter 
already tried that
Code:
while(error == 1)
                        switch(pagain){
                                case 1:
                                play = 1;
                                error = 0;
                                printf("success!\n");
                                break;

                                case 0:
                                printf("Goodbye!\n");
                                play = 0;
                                exit(0);
                                break;
                                
                                default: 
                                error = 1;
                                printf("please enter a valid key\n");
                                break;
                                                                
                        }
Thanks for reply
post #5 of 15
Add a line to check if the input is valid, don't rely on the switch statement to do it for you.
post #6 of 15
What about this ?
Code:
//play again
                do {
                printf("would you like to play again?: 1\\0\n");
                scanf("%d", &pagain);
                
                        switch(pagain){
                                case 1:
                                play = 1;
                                printf("Success!\n");
                                break;
                                case 0:
                                printf("Goodbye!\n");
                                exit(0);
                                break;
                                
                                default: 
                                printf("Please enter a valid key.\n");
                        }
                } while ((pagain != 0) || (pagain != 1))

Edited by adridu59 - 11/7/12 at 2:39pm
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
post #7 of 15
That should work, a goto tag works too but I never really like using them. Also, I don't think that break should be there at the end.
Edited by JQuantum - 11/7/12 at 12:28pm
2012
(20 items)
 
Testing v2.5
(8 items)
 
 
CPUMotherboardGraphicsGraphics
Intel i7-3770k Gigabyte Z77X-UD5H MSI nVidia GTX 670 MSI nVidia GTX 670 
RAMHard DriveHard DriveHard Drive
Patriot Viper Xtreme DIV. 2 Samsung 830 OCZ Vertex 3 MAX IOPS OCZ Vertex 3 
Hard DriveOptical DriveOSOS
Seagate LG BluRay Windows 8 Professional x64 Mac OS X 10.8.1 (Pending) 
OSMonitorKeyboardPower
Fedora 17/Ubuntu 12.04 (Pending) Dell ST2220T Logitech G110 Corsair HX1050 
CaseMouseAudioOther
Corsair 600T Special Edition White Logitech G9x Unknown yet Keyboard: Corsair K90 (Pending) 
CPUMotherboardGraphicsRAM
Intel i5-3570k Asus P8Z77-i Deluxe MSI HD 7950 Samsung 
Hard DriveCoolingOSCase
OCZ Agility 3 Corsair H100i Windows 8 Professional x64 Cardboard box 
  hide details  
Reply
2012
(20 items)
 
Testing v2.5
(8 items)
 
 
CPUMotherboardGraphicsGraphics
Intel i7-3770k Gigabyte Z77X-UD5H MSI nVidia GTX 670 MSI nVidia GTX 670 
RAMHard DriveHard DriveHard Drive
Patriot Viper Xtreme DIV. 2 Samsung 830 OCZ Vertex 3 MAX IOPS OCZ Vertex 3 
Hard DriveOptical DriveOSOS
Seagate LG BluRay Windows 8 Professional x64 Mac OS X 10.8.1 (Pending) 
OSMonitorKeyboardPower
Fedora 17/Ubuntu 12.04 (Pending) Dell ST2220T Logitech G110 Corsair HX1050 
CaseMouseAudioOther
Corsair 600T Special Edition White Logitech G9x Unknown yet Keyboard: Corsair K90 (Pending) 
CPUMotherboardGraphicsRAM
Intel i5-3570k Asus P8Z77-i Deluxe MSI HD 7950 Samsung 
Hard DriveCoolingOSCase
OCZ Agility 3 Corsair H100i Windows 8 Professional x64 Cardboard box 
  hide details  
Reply
post #8 of 15
Thread Starter 
Thanks for your replies ill try them now thumb.gif
post #9 of 15
Thread Starter 
okay so i tried the do while loop and now instead of just going in circles it simply exits if the answer is not an integer,
So i did scanf to find value of "pasnwer" when i hit a alphabetical key, and it returns 0.
is there a way to change stop this?

in reply to dyson
i am not suure i understand what you mean


ALSO:
the program allows me to exit but if i wanted to play again, it just repeats the do while loop

thanks dom
Edited by Hatakescreams - 11/7/12 at 12:47pm
post #10 of 15
Quote:
Originally Posted by JQuantum View Post

That should work, a goto tag works too but I never really like using them. Also, I don't think that break should be there at the end.

Yeah, goto's should be avoided at all costs.
Quote:
Originally Posted by Hatakescreams View Post

okay so i tried the do while loop and now instead of just going in circles it simply exits if the answer is not an integer,

Of course, what did you expect ?
You are setting a switch on an integer, if user doesn't input an integer the program will crash.

EDIT: if you want a fail-safe input I advise you to read this:
http://answers.yahoo.com/question/index?qid=20111127194955AAy9ysB
Solution posted shows C and C++ methods.
Edited by adridu59 - 11/7/12 at 2:39pm
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
Ti-89 Titanium
(13 items)
 
   
CPUMotherboardRAMHard Drive
Motorola 68k @16 MHz Ti-89 HW4 256 KB 2.6 MB Flash ROM 
OSMonitorPowerCase
Ti-89 OS 3.10 LCD Dot-Matrix 160x100 4x 1.5V AAA Ti-89 HW4 Black 
CPUMotherboardGraphicsRAM
Intel Core™ i7 930 Gigabyte GA-X58A-UD3R 2.0 Gigabyte GTX 470 SOC G.Skill RX 2x4GB 1600C8 
Hard DriveHard DriveOptical DriveCooling
PNY Prevail Elite 120GB Hitachi 7K1000.C 1TB LG GH24LS50 Noctua NH-C12P SE14 
OSOSMonitorKeyboard
Windows 8 Pro x64 Lubuntu 13.10 x64 Samsung BX2250 Logitech Illuminated 
PowerCaseMouseAudio
Be Quiet! S.P. E⁷ 600W Fractal Design Core 3000 Logitech G300 AKG K240 Studio 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming