New Posts  All Forums:Forum Nav:

Zombie program in C - Page 2

post #11 of 15
Code:
char pagain;
        int error = 1;
        while(error){
                printf("would you like to play again?: 1\\0\n");
           scanf("%c", &pagain);
               
           switch(pagain){
                   case '1':
                                printf("success!\n");
                                error = 0;
                                break;

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

This works for me, only problem is that it prints twice when you enter an invalid char. I don't know why eh-smiley.gif.
2013
(11 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2700k GIGABYTE GA-Z77-D3H Sapphire HD 7870 XT Boost G.SKILL 1866 Sniper 2x4 GB 
Hard DriveHard DriveOSMonitor
Samsung 840 Basic Crucial MX200 Windows 10 HP IPS 23-inch 1080p 
PowerCaseMouse
Be quiet! BN144 System Power 7 500W NZXT Tempest 210 Zowie FK1 
  hide details  
Reply
2013
(11 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2700k GIGABYTE GA-Z77-D3H Sapphire HD 7870 XT Boost G.SKILL 1866 Sniper 2x4 GB 
Hard DriveHard DriveOSMonitor
Samsung 840 Basic Crucial MX200 Windows 10 HP IPS 23-inch 1080p 
PowerCaseMouse
Be quiet! BN144 System Power 7 500W NZXT Tempest 210 Zowie FK1 
  hide details  
Reply
post #12 of 15
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,
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

Is your entire main portion of the game in a loop?

If it is I generally just have an exit trigger for the loop for example

while(!varExit){
[main program]
...

if finished game
then varExit = function checkexit()
}

checkexit would return either 1 or 0, or true or false.
loops while invalid entries and exits with valid

while true
print "please enter yes or no"
get input
if input = yes then return 1
if input = no then return 0
print "sorry invalid entry"
end while


Sorry for the bad pseudo-code... so basically the checkexit will be stuck in a loop unless returning a valid entry which will either exit the main loop or keep it inside the main loop.
Edited by JQuantum - 11/7/12 at 10: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 #13 of 15
Thread Starter 
Quote:
Originally Posted by JQuantum View Post

Is your entire main portion of the game in a loop?
If it is I generally just have an exit trigger for the loop for example
while(!varExit){
[main program]
...
if finished game
then varExit = function checkexit()
}
checkexit would return either 1 or 0, or true or false.
loops while invalid entries and exits with valid
while true
print "please enter yes or no"
get input
if input = yes then return 1
if input = no then return 0
print "sorry invalid entry"
end while
Sorry for the bad pseudo-code... so basically the checkexit will be stuck in a loop unless returning a valid entry which will either exit the main loop or keep it inside the main loop.

This is very much the idea I first tried problem comes when I try to scan for user input. If its bit one of yea or no the programngeta stuck in a loop. Also am on my phone so sorry for any mistakes I haven't noticed
post #14 of 15
Quote:
Originally Posted by Hatakescreams View Post

This is very much the idea I first tried problem comes when I try to scan for user input. If its bit one of yea or no the programngeta stuck in a loop. Also am on my phone so sorry for any mistakes I haven't noticed

I don't understand this part of your message
Quote:
If its bit one of yea or no the programngeta stuck in a loop



Anyways, since you've tried it that means you should be able to shove it all into a procedure to test this (Since I am unsure of the full structure of the program I can only assume bits)


I'll post some mock code you should be able to just insert easily later but for now my question is. Have you verified the input? ie. ensure it's catching just 0 or 1 or neither properly?

EDIT - here is some real code vs the mess I had earlier
Code:
void zombieGame(){ 
        ... // your game here
}

int playAgainChecker(){  //alternative is use bool
        int checkVal = -1;
        while(1){ // if using bool use true here
                prinf("Would you like to play again? (Hit '1' for yes and '2' for no): ");
                scanf("%i", &checkVal);  //maybe it was %d
                flushall(); 
                printf("\n");
                checkVal--;
                
                if(checkVal < 0 || checkVal > 1){ 
                        printf("Sorry that was an invalid input\n");
                        // .. continue loop since invalid and should never leave until yes or no
                }else{
                        return !checkVal; // since checkVal-- before causes 1 to be no and 0 to be yes
                }
        }
}

int main(int argc, const char* argv[]){ // Hmmm... or is it void
        int cPlay  = 1;
        while(cPlay)
                zombieGame(); // zombie game is independant of the main function
                
                // once the zombie game is completed then check to play again
                cPlay = playAgainChecker();
        }       
        return 0;
}

Obviously, yours doesn't have to look like that to work, it's just a bad habit of mine lately. BTW typed up on notepad++ but I don't think there are that many mistakes.

I think one mistake in mine is if they type a character or anything not a number it'll ignore it... just a feeling I have.
Edited by JQuantum - 11/8/12 at 8:55am
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 #15 of 15
Quote:
Originally Posted by Sisaroth View Post

Code:
char pagain;
        int error = 1;
        while(error){
                printf("would you like to play again?: 1\\0\n");
           scanf("%c", &pagain);
               
           switch(pagain){
                   case '1':
                                printf("success!\n");
                                error = 0;
                                break;
                        case '0':
                                printf("Goodbye!\n");
                                exit(0);
                                break;
                                
                        default:
                                printf("please enter a valid key\n");
                }
        }
This works for me, only problem is that it prints twice when you enter an invalid char. I don't know why eh-smiley.gif.

Probably due to the lack of buffer clearing... just realized it.
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
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming