You are here:

C/Need help with a basic C program

Advertisement


Question
I just started to learn the C language today and I am trying to get this simple program to work. I want the program to let each player take a turn typing in a number until one of them types in 7. At this point I want the program to use the win() function and end the program. As of now the program has each player go twice with only the second turn counting toward the win() function. Also the win() function just ends the program without showing the "You Win". I don't think I am completely grasping the idea of passing arguments which I think I need here. Any input would be appreciated. Code to follow:

#include <stdio.h>
int onePlayer();
int twoPlayer();
int threePlayer();
int fourPlayer();
char win();
int takeTurns();


int main(){
takeTurns();
}

int takeTurns(){

while (true) {
onePlayer();
if (onePlayer() == 7) {
break;
win();
}
twoPlayer();
if (twoPlayer() == 7) {
break;
win();
}
threePlayer();
if (threePlayer() == 7) {
break;
win();
}
fourPlayer();
if (fourPlayer() == 7) {
break;
win();
}
}
return 0;
}

char win(){
printf("You Win");
return 0;
}

int onePlayer(){
int choice;
printf("Player 1 choose a number\n");
scanf("%d", &choice);
return choice;
}

int twoPlayer(){
int choice;
printf("Player 2 choose a number\n");
scanf("%d", &choice);
return choice;
}

int threePlayer(){
int choice;
printf("Player 3 choose a number\n");
scanf("%d", &choice);
return choice;
}

int fourPlayer(){
int choice;
printf("Player 4 choose a number\n");
scanf("%d", &choice);
return choice;
}

Answer
Hello Zac.

I think you did pretty well on your program. I have 2 versions for you. The first is your program with corrections made so that it works as you expect it to. My corrections are explained in the comments. The second version shows you how to use parameters to reduce the repetition of the onePlayer, twoPlayer, ... functions.

Are you sure you're using C, and not C++ ? The keyword "true" as in "while (true)" is a C++ keyword.
Version 1 follows

#include <stdio.h>

int onePlayer();
int twoPlayer();
int threePlayer();
int fourPlayer();
char win();
int takeTurns();


int main(){
   takeTurns();

   return 0;/*main needs a return value */
}

int takeTurns(){

   while (true) {
       /*the first call to xxxPlayer is not needed here.
       If I understand your question, you do not want each player
       to go twice, with only the second try counting toward the win

       onePlayer();
       */

       /*
       This call to onePlayer is good. The return value is
       checked for 7 correctly
       */
       if (onePlayer() == 7) {
           /*
           This break will cause the loop to exit immediately, so if the break is before the win
           the win will not be called. You want the break after the win
           */
           win();
           break;
       }
       
       
       /*
       Again, I expect you don't want player 2 to have to do two inputs.
       twoPlayer();
       */
       if (twoPlayer() == 7) {
           win();
           break; /* break after win */
       }
       /* threePlayer(); */
       if (threePlayer() == 7) {
           win();
           break; /* break after win */
       }
       /* fourPlayer(); */
       if (fourPlayer() == 7) {
           win();
           break; /* break after win */
       }
   }
   return 0;
}

char win(){
   printf("You Win\n"); /* added newline */
   return 0;
}

int onePlayer(){
   int choice;
   printf("Player 1 choose a number\n");
   scanf("%d", &choice);
   return choice;
}

int twoPlayer(){
   int choice;
   printf("Player 2 choose a number\n");
   scanf("%d", &choice);
   return choice;
}

int threePlayer(){
   int choice;
   printf("Player 3 choose a number\n");
   scanf("%d", &choice);
   return choice;
}

int fourPlayer(){
   int choice;
   printf("Player 4 choose a number\n");
   scanf("%d", &choice);
   return choice;
}


Version 2 is a more compact and flexible program. I think you will be able to understand it. I have some comments in it for explanation. If you have more questions, just ask.

#include <stdio.h>

int player(int playerNumber);
char win();
int takeTurns(int numberOfPlayers);


int main(){
   takeTurns(4); /* Try this with 5, or 6 */

   return 0;
}

int takeTurns(int numberOfPlayers){

   int done = false; /* if using C++, this should be a bool, not an int */
   while (! done) /* read this as "while not done" */ {

       int playerNumber;
       /* go through all the players */
       for(playerNumber = 1; playerNumber <= numberOfPlayers; ++playerNumber) {
           if (player(playerNumber) == 7) {
               win();
               /* here we have a for loop nested in a while loop.
               a break will break out of the innermost loop only, so we need
               a second flag variable to quit the while.
               */
               done = true;
               break;
           }
       }
   }
   return 0;
}

char win(){
   printf("You Win\n");
   return 0;
}

/*
By passing the player number in as a parameter,
we can use one function for all players
*/
int player(int playerNum){
   int choice;
   printf("Player %d choose a number\n", playerNum);
   scanf("%d", &choice);
   return choice;
}  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.