Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,339 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,798 people online right now. Registration is fast and FREE... Join Now!




C program exists prematurely

 
Reply to this topicStart new topic

C program exists prematurely

bpatzin2
post 13 Mar, 2008 - 07:39 AM
Post #1


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


This program is a NIM game. It seems to work perfectly until the line of ***'s I added to show where the mistake seems to be. I need help finding my mistake. I think it might be with my integer "pile'". I am not sure if I used scanf incorrectly or what else the error could possibly be. Any help is appreciated.

CODE


   * This program implements a NIM game with normal
   * win rule.  The game starts with three piles
   * of sticks each containing between 1 and 50 sticks
   * which are represented with a “*”. The number of
   * sticks in each pile is specified by the players.
   * The two players take turns removing any positive
   * number of sticks from a pile that is not empty.  
   * The player that takes the last stick wins.  The
   * program refreshes the number of sticks in each pile
   * after every turn and prints out graphically the number
   * of sticks in each pile.  The program checks for several
   * different errors throughout.
   *
*/  
  
#include <stdio.h>  
#include <stdlib.h>  
  
#define MSG_0 0  
#define MSG_1 1  
#define MSG_2 2  
#define MSG_3 3  
#define MSG_4 4  
#define MSG_5 5  
#define MSG_6 6  
#define MSG_7 7  
#define MSG_8 8  
#define MSG_9 9  
  
void printPiles (int pile1, int pile2, int pile3);  
int printMsg (int msgCode, int player);  
  
/* Main function    */  
int main ()
{  
  
  
     /* declare variables */  
         #define EMPTY 0  
         int pile1;  
         int pile2;  
         int pile3;  
         int A;  
         int pile;  
         int player;  
         int condition;  
         int flag;  
  
     /* implement the game */  
         player = 1;  
         printMsg (MSG_0, 1);  
        flag = scanf("%d %d %d", &pile1, &pile2, &pile3);  
          
           if(flag != 3)
       {  
                 printMsg (MSG_5, 1);  
                 exit(1);  
           }  
        
    condition = 1;  
    if(condition == 1)  
    {
         if(pile1 > 50 || pile1 < 1)  
             printMsg (MSG_6, 1);  
              
         else if(pile2 > 50 || pile2 < 1)  
             printMsg (MSG_6, 1);      
          
         else if(pile3 > 50 || pile3 < 1)  
             printMsg (MSG_6, 1);  
  
         else  
             condition = 0;  
          
       }
          
    else    
         printMsg (MSG_7, 1);


if (pile1 > 0 || pile2 > 0 || pile3 >0)
{    
    printPiles(pile1, pile2, pile3);  
  
  
  
         if (player == 1)
     {  
             printMsg (MSG_1, player);  
             player = player + 1;  
         }  
  
         else
    {  
             printMsg (MSG_1, 2);  
             player = player - 1;  
         }  
  
         printMsg (MSG_2, 1);  
         flag = scanf("%d", &pile);  
        
     if (flag == 0)
    {      
             printMsg (MSG_5, 1);  
             exit(1);  
         }  

         else printMsg (MSG_3, 1);  
        
    flag = scanf("%d", &A);  

         if (flag == 0)  
         {  
             printMsg (MSG_5, 1);  
             exit(1);  
         }  
          
/ ********************************************************************************
**************************/          
         if (pile == 1)  
         {  
                 if (pile1 != 0)  
                 {  
                     pile1 = pile1 - A;  
                     if (pile1 <= 0)  
                         pile1 = 0;  
                 }  
                  
                 else  
                     printMsg (MSG_8, 1);  
  
         }  
                  
  
         else if (pile == 2)  
         {  
                 if (pile2 != 0)  
                 {  
                     pile2 = pile2 - A;  
                     if (pile1 <= 0)  
                         pile2 = 0;  
                 }  
                  
                 else  
                     printMsg (MSG_8, 1);  
  
         }  
          
         else if (pile == 3)  
         {  
                 if (pile3 != 0)  
                 {  
                     pile3 = pile3 - A;  
                     if (pile3 <= 0)  
                         pile3 = 0;  
                 }  
                  
                 else  
                     printMsg (MSG_8, 1);  
  
         }  
  
          
          
      
     else  
     {  
         if (player == 1)  
             player = player + 1;  
         else  
             player = player - 1;  
          
         printMsg (MSG_1, 1);  
             exit(1);  
     }      
          
}

  
else
{

    if (player == 1)  
          player = player + 1;  
    else  
          player = player - 1;

    printMsg (MSG_4, player);
}
return 0;  

}
  
/* Function: printPiles()
* This function visually prints out the three piles with asterisks
* Inputs: the number of sticks in pile1, pile2, pile3
* Output: none
*/  
void printPiles (int pile1, int pile2, int pile3) {  
   /* declare variables */  
  
int counter;    
  
  
  
   /* write your printing code here */  
  
         counter = pile1;  
         printf("Pile #1: ");  
         while (counter > 0)  
         {  
             printf("*");  
             counter = counter - 1;                  
         }  
  
         counter = pile2;  
         printf("\nPile #2: ");  
         while (counter > 0)  
         {  
             printf("*");  
             counter = counter - 1;  
         }  
  
         counter = pile3;  
         printf("\nPile #3: ");  
         while (counter > 0)  
         {  
             printf("*");  
             counter = counter - 1;  
         }          
  
  
}  
  
/* Printing messages:
* This function prints out a message corresponding to a message code.  
* Input: the message code, the player ID for the outputs for a particular player.
* Output: 0 if no error, 1 if error.
*/  
int printMsg (int msgCode, int player)  
{  
  
   if ( msgCode < 0 || msgCode > 9)  
     return 1;  
   if ( msgCode == 1 || msgCode == 4 )  
     if ( player < 1 || player > 2 )  
       return 1;  
  
   switch (msgCode)  
   {  
     case 0: printf("Enter pile sizes (separated by spaces): "); break;  
     case 1: printf("\nPlayer %d's turn:\n", player); break;  
     case 2: printf("Pick a pile #: "); break;  
     case 3: printf("Remove how many sticks? "); break;  
     case 4: printf("Congratulations! Player %d is the winner!\n", player); break;  
     case 5: printf("Invalid input, end of game!\n"); break;  
     case 6: printf("Invalid pile sizes. Try again.\n"); break;  
     case 7: printf("Invalid pile. Try again.\n"); break;  
     case 8: printf("That pile is empty. Try again.\n"); break;  
     case 9: printf("You have to take at least one stick!\n"); break;  
     default: break;  
   }  
   return 0;  
  }

User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 13 Mar, 2008 - 08:42 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,157



Thanked 30 times

Dream Kudos: 25
My Contributions


CODE

scanf("%d", &flag);
//or
scanf("%d", &A);
flag=A;

What value are you entering before it exits? Because you have an exit statement in there...
User is online!Profile CardPM

Go to the top of the page

bpatzin2
post 13 Mar, 2008 - 08:58 AM
Post #3


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


I have 'if' statements to do the subtraction if pile = 1,2, or 3 it seems to skip past that part and exit
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Mar, 2008 - 09:22 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,157



Thanked 30 times

Dream Kudos: 25
My Contributions


Does it allow you to enter anything? Doe sit exit right after you enter the values for pile? note the difference in my use of scanf() versus yours.
User is online!Profile CardPM

Go to the top of the page

bpatzin2
post 13 Mar, 2008 - 09:33 AM
Post #5


New D.I.C Head

*
Joined: 9 Mar, 2008
Posts: 33


My Contributions


nevermind, I found my error. I was using an If loop instead of a while loop. This is my first C program, but if I am correct it will leave an If loop after one pass but will keep going through a while loop until the condition is changed. It works in the code but is that the correct reasoning?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Mar, 2008 - 09:43 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,157



Thanked 30 times

Dream Kudos: 25
My Contributions


There is no if loop - only an if statement. It will only assess the condition one time, unless nested within a loop of some sort.

http://www.cprogramming.com/tutorial/c/lesson3.html
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/19/08 03:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month