Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

error writing output file

I'm getting this error message (in title) when I try to compile this function in my overall program.  The error says this is for line 225. However, there are only 223 lines in my code. I've checked and deleted everything after line 223, just to be sure there were not any stray markings. Can anyone tell me what is wrong here?

/* .......................................................................... */
/* A program to retrieve a four digit string for Mastermind game              */
/* guess - output parameter                                                   */
/* PRE: numcols defined as 4 in main                                          */

void get_guess(char *guess)
{

  int error,
              i,
      j;

  char attempt[numcols],
            guess_out[numcols],
       sol[numcols] = {9, 9, 9, 9};



  do
  {
    error = 0;
    printf("\nEnter four digits> ");
    scanf("%s", attempt);

      printf("%s", attempt);
      if (attempt != sol)
      {
        for (i = 0; i < numcols; i++)
        {
          if (attempt[i] != sol[i])
          {
            for (j = 0; j < numcols; j++)
            {
              if (attempt[i] != sol[j])
              {
                guess_out[i] = ' ';
              }
              else
                guess_out[i] = 'W';
            }
          }
          else
            guess_out[i] = 'B';
        }
      }
      else
        *guess = 'Y';

    *guess = guess_out[numcols];
  }
  while (error);

}
0
Sageguy
Asked:
Sageguy
1 Solution
 
emmonsCommented:
This isn't the entire file, is it?
The problem is most likely an extra or missing curly bracket.
Include the entire file and I will look.
0
 
SageguyAuthor Commented:
Thanks in advance...

This is a work in progress - a school assignment (but since I'm a
grad student in another area, the grade doesn't count. I'm just
doing this to learn some programming).
I know I probably don't need all these includes - I've been experimenting.

Required: I must use the function headers as written. At this point
in the program, I'm trying to write the get_guess function so that
it will return (for example) a "B" if there is one strong match and
no other weak matches... or... a "BWW" if there are one strong match
and two weak matches...etc. If there are no matches, then blank space.
For now, I've set the solution to 9999 for testing.
At this point in the get_guess function, the guess_out isn't holding
B's and W's.
....................................................................

/* A program to play Mastermind game                                          */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stddef.h>

#define numrows 10
#define numcols 4

/* Function prototype to get yes or no to play a game                         */
void get_yes_no(char *answer);

/* Function prototype to retrieve an integer within a specified range         */
int get_int(int n_min, int n_max);

/* Function prototype for user's guess at game solution                       */
void get_guess(char *guess);

int main (void)
 {

  int game_num,               /* Game number to be played                     */
      game_min = 0,           /* Minimum number of previous games             */
              game_max = 9999,        /* Maximum number of previous games             */
      digits_num,             /* Number of digits to be used in game          */
      digits_min = 4,         /* Minimum number of digits in game             */
      digits_max = 8;         /* Maximum number of digits in game             */

  char answer_out,            /* Output of 'another game' function            */
       guess_out;             /* Output from 'get_guess' function             */
       
/* Get user input                                                             */
  printf("How many games have you played?(0-9999)> ");

/* Function call                                                              */
  game_num = get_int(game_min, game_max);

  do
  {
game_num++;
/* Display game number to be played                                           */
  printf("\n                       ***** GAME NUMBER %d *****", game_num);
  printf("\nYou will be able to make up to ten guesses at the solution");
  printf("\nEach guess should be four digits.");

/* Get user input                                                             */
  printf("\nHow many different numbers can appear in the four ");
  printf("digit solution?(4-8)> ");

/* Function call                                                              */
  digits_num = get_int(digits_min, digits_max);

/* Display digits to be used in solution                                      */
  printf("\nThe digits that may appear in the solution ");
  printf("are 0 - %d", (digits_num - 1));
  printf("\nA clue of B means some digit you guessed is in the correct ");
  printf("position.");
  printf("\nA clue of W means some digit you guessed is in the wrong ");
  printf("position.");

/*  Function call for get_guess                                               */
  get_guess(&guess_out);
  printf("%c", guess_out);

/* Prompt and function call to play another game                              */
  get_yes_no(&answer_out);

  }
  while (answer_out != 'n');

return(0);
}

/* .......................................................................... */
/* A program to validate that input is an integer within a specified range    */
/* n_min - input parameter                                                    */
/* n_max - input parameter                                                    */

int get_int(int n_min, int n_max)
{

  int status,
       error,
       in_val;

  char skip_ch;

  do
  {
  error = 0;
  status = scanf("%d", &in_val);

   if (status != 1)
   {
     error = 1;
     printf("Enter an integer between %d and %d.> ", n_min, n_max);
   }
   else if (in_val < n_min || in_val > n_max)
   {
     error=1;
        printf("Number %d is not in range.\n", in_val);
     printf("Enter an integer between %d and %d.> ", n_min, n_max);
      }

/* Skip rest of data line.                                                    */
       do
       {
    scanf("%c", &skip_ch);
      if (skip_ch != '\n')
      {
       error = 1;
       printf("Enter an integer between %d and %d.> ", n_min, n_max);
      }
    }
    while (skip_ch != '\n');
  }
  while (error);

return (in_val);
}

/* .......................................................................... */
/* A program to determine if user wants to play another game                  */
/*  answer - output parameter                                                 */

void get_yes_no(char *answer)

{
  int error;

  char game[4],
       game_no[3],
       game_NO[3],
       game_yes[4],
       game_YES[4];

  printf("\nAnother game?(YES/NO)> ");
 
  do
  {
   error = 0;
   scanf("%s", game);
   strcpy(game_no, "no");
   strcpy(game_NO, "NO");
   strcpy(game_yes, "yes");
   strcpy(game_YES, "YES");

    if (strcmp(game, game_no) == 0)
      *answer = 'n';

    else if (strcmp(game, game_NO) == 0)
      *answer = 'n';

    else if (strcmp(game, game_yes) == 0)
      *answer = 'y';

    else if (strcmp(game, game_YES) == 0)
      *answer = 'y';

    else
    {
      error = 1;
      printf("\nPlease type YES or NO> ");
    }
  }
  while (error);
}

/* .......................................................................... */
/* A program to retrieve a four digit string for Mastermind game              */
/* guess - output parameter                                                   */
/* PRE: numcols defined as 4 in main                                          */

void get_guess(char *guess)
{

  int error,
              i,
      j;

  char attempt[numcols],
            guess_out[numcols],
       sol[numcols] = {9, 9, 9, 9};



  do
  {
    error = 0;
    printf("\nEnter four digits> ");
    scanf("%s", attempt);

      printf("\n%s\n", attempt);
      if (attempt[numcols] != sol[numcols])
      {
        for (i = 0; i < numcols; i++)
        {
          if (attempt[i] != sol[i])
          {
            for (j = 0; j < numcols; j++)
            {
              if (attempt[i] != sol[j])
              {
                guess_out[i] = ' ';
              }
              else
                guess_out[i] = 'W';
            }
          }
          else
            guess_out[i] = 'B';
        }
      }
      else
        *guess = 'Y';


      *guess = guess_out[numcols];

  }
  while (error);
}
0
 
q2guoCommented:
change all your
else if statements
to elseif
with nospace in between else and if
This should fix your problems.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
SageguyAuthor Commented:
error writing output file seems to me to be indicative of the compiler having trouble emitting its output (i.e. the object file). Things like unmatched braces would be more along the lines of: unexpected end of file.

A couple of reasons I can think of for there being trouble with writing the output file would be the disk being full, or maybe writeprotected (a floppy?). There are undoubtedly other possibilities depending on your environment.

What environment are you in?

0
 
imladrisCommented:
Are you still getting the same "error writing file" message?
I compiled the program and it compiled cleanly (didn't run, but it did compile).

q2guo: Both "elseif" and "else if" are perfectly valid syntax.
0
 
emmonsCommented:
SageGuy, there are several problems in your code.
At this point, I would guess that you are no longer getting the failed write error, but you are having a problem with the return value from get_guess.
You need to review the difference between pointers and characters.
I have a better working version of the subroutine if you want it.

0
 
emmonsCommented:
I made a few changes. You should go through this again.
I am not sure about your logic for evaluating the turn because I am not familiar with the game.
If this works you can reject Imladris (sorry Imladris) and I will submit another answer.

BTW, does your name mean that you are going to RSC?



/* A program to play Mastermind game */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stddef.h>

#define numrows 10
#define numcols 4

/* Function prototype to get yes or no to play a game */
void get_yes_no(char *answer);

/* Function prototype to retrieve an integer within a specified range */
int get_int(int n_min, int n_max);

/* Function prototype for user's guess at game solution */
void get_guess(char *guess);

int main (void)
{

    int game_num;        /* Game number to be played */
    int game_min = 0;    /* Minimum number of previous games */
    int game_max = 9999; /* Maximum number of previous games */
    int digits_num;      /* Number of digits to be used in game */
    int digits_min = 4;  /* Minimum number of digits in game */
    int digits_max = 8;  /* Maximum number of digits in game */

    static char answer_out;     /* Output of 'another game' function */
    static char guess_out[numcols+1];      /* Output from 'get_guess' function */
    guess_out[numcols]=0;   /* null terminate the expression */

    /* Get user input */
    printf("How many games have you played?(0-9999)> ");

    /* Function call */
    game_num = get_int(game_min, game_max);

    do {
        game_num++;
        /* Display game number to be played */
        printf("\n ***** GAME NUMBER %d *****", game_num);
        printf("\nYou will be able to make up to ten guesses at the solution");
        printf("\nEach guess should be four digits.");

        /* Get user input */
        printf("\nHow many different numbers can appear in the four ");
        printf("digit solution?(4-8)> ");

        /* Function call */
        digits_num = get_int(digits_min, digits_max);

        /* Display digits to be used in solution */
        printf("\nThe digits that may appear in the solution ");
        printf("are 0 - %d", (digits_num - 1));
        printf("\nA clue of B means some digit you guessed is in the correct ");
        printf("position.");
        printf("\nA clue of W means some digit you guessed is in the wrong ");
        printf("position.");

        /* Function call for get_guess */
        get_guess( guess_out);
        printf("%s", guess_out);

        /* Prompt and function call to play another game */
        get_yes_no(&answer_out);
        printf ("I received '%c' as your answer\n", answer_out);

    } while (answer_out != 'n');

    return(0);
}

/* .......................................................................... */
/* A program to validate that input is an integer within a specified range */
/* n_min - input parameter */
/* n_max - input parameter */

int get_int(int n_min, int n_max)
{

    int status,
    error,
    in_val;

    char skip_ch;

    do {
        error = 0;
        status = scanf("%d", &in_val);

        if (status != 1) {
            error = 1;
            printf("Enter an integer between %d and %d.> ", n_min, n_max);
        } else if (in_val < n_min || in_val > n_max) {
            error=1;
            printf("Number %d is not in range.\n", in_val);
            printf("Enter an integer between %d and %d.> ", n_min, n_max);
        }

        /* Skip rest of data line. */
        do {
            scanf("%c", &skip_ch);
            if (skip_ch != '\n') {
                error = 1;
                printf("Enter an integer between %d and %d.> ", n_min, n_max);
            }
        } while (skip_ch != '\n');
    } while (error);

    return (in_val);
}

/* .......................................................................... */
/* A program to determine if user wants to play another game */
/* answer - output parameter */

void get_yes_no(char *answer)

{
    int error;

    char game[10];
    int  i;
    char *game_NO = "NO";
    char *game_YES = "YES";

    printf("\nAnother game?(YES/NO)> ");

    do {
        error = 0;
        scanf("%s", game);
        for ( i=0; i<4; i++)
            game[i] = (char )toupper( game[i]);

        if (strcmp(game, game_NO) == 0)
            *answer = 'n';
        else if (strcmp(game, game_YES) == 0)
            *answer = 'y';
        else {
            error = 1;
            printf("\nPlease type YES or NO> ");
        }
    } while (error);
    printf ("I heard '%c' as your answer\n", *answer);
}

/* .......................................................................... */
/* A program to retrieve a four digit string for Mastermind game */
/* guess - output parameter */
/* PRE: numcols defined as 4 in main */

void get_guess(char *guess)
{

    int error, i, j;

    char attempt[numcols+1];
    char guess_out[numcols+1];
    char sol[numcols+1] = {'1','1','1','1', 0};

    for( i=0; i<=numcols; i++) {    /* clear everything to NULL */
        attempt[i] = 0;
        guess_out[i] = 0;
        guess[i] = 0;
    }

    do {
        error = 0;
        printf("\nEnter four digits> ");
        scanf("%4s", attempt);

        printf("\n%s\n", attempt);
        for (i = 0; i < numcols; i++) {
            if (attempt[i] != sol[i]) {
                for (j = 0; j < numcols; j++) {
                    if (attempt[i] != sol[j]) {
                        guess_out[i] = ' ';
                    } else
                        guess_out[i] = 'W';
                }
            } else
                guess_out[i] = 'B';
        }

        for (i = 0; i < numcols; i++)
            guess[i] = guess_out[i];
    } while (error);
}
0
 
emmonsCommented:
Thanks to everyone for the help. I'll look at the comments
more closely after submitting this. Sorry for being a pain
in the butt.

My original problem had something to do with my compiler. I just assumed
I was using bad code. Anyway, after rebooting, etc.,no more errors.

As per emmons suggestion, I'm leaving the question open. Since emmons
was involved earlier and has gone the extra mile with his code help,
the credit should go to him (or her). So, (if this is the way it works)
let emmons be the "expert on record" so I can give him the 'A'.

I've included a modified version of my problematic function. My dilemma is
this: If the game solution is 1234... and the player inputs 1212...I would
like the hint to be BB (strong matches for the first two numbers). Right now,
the hint displays BBWW (2 strong matches and WW for 2 weak matches). I would
like the code to only default to a W only if a strong match wasn't found...
and to print a space otherwise. In otherwords, if the user inputs 1111, I want
a B and not a BWWW.

Finally (if I can get that working), I want the B's listed first, then the
W's, and the spaces listed last. Otherwise, displays such as "W BW" or "BW B"
would display, making the hint too easy.

I only have 30 credits left and I'm a broke student from Mass., so I'll
toss in my final credits and shut up. Much appreciated...




/* .......................................................................... */
/* A program to retrieve a four digit string for Mastermind game              */
/* guess - output parameter                                                   */

      void get_guess(char *guess)
{

      int error, i, j;

   char attempt[numcols+1];
   char guess_out[numcols+1];
   char sol[numcols+1] = {'1','2','3','4', 0};

   for( i=0; i<=numcols; i++) { /* clear everything to NULL */
     attempt[i] = 0;
     guess_out[i] = 0;
     guess[i] = 0;
   }

         do {
        error = 0;
        printf("\nEnter four digits\n");
        scanf("%4s", attempt);

        printf("\n%15s\n", attempt);
        for (i = 0; i < numcols; i++) {
               if (attempt[i] == sol[i]) {
            guess_out[i] = 'B';
          }
          if (attempt[i] != sol[i]) {
            for (j = 0; j < numcols; j++) {
              if (attempt[i] == sol[j] && guess_out[i] != 'B') {
                guess_out[i] = 'W';
              }

            }
          }

        }

        for (i = 0; i < numcols; i++) {
          if (guess_out[i] != 'B' && guess_out[i] != 'W')
            guess_out[i] = ' ';
              }
        printf("%40s\n", guess_out);
        guess = guess_out;  

      } while (error);

}
0
 
SageguyAuthor Commented:
Thanks SageGuy.
You are most kind.
OK. Give me a few minutes to check out your latest code...
0
 
emmonsCommented:
I think that this will do the trick...

void get_guess(char *guess)
{

    int error, i, j;
    int exact_count;
    int near_count;

    char attempt[numcols+1];
    char guess_out[numcols+1];
    char sol[numcols+1] = {'1','2','3','4', 0};
    char tempsol[numcols+1];

    do {
        exact_count = near_count = 0;
        error = 0;
        for( i=0; i<=numcols; i++) { /* clear everything to NULL */
            attempt[i] = 0;
            guess_out[i] = 0;
            guess[i] = 0;
            tempsol[i] = sol[i];        /* grab the solution again */
        }
        printf("\nEnter four digits\n");
        scanf("%4s", attempt);

        printf("\n%15s\n", attempt);

/*
** find the exact matches
*/
        for (i = 0; i < numcols; i++) {
            if (attempt[i] == tempsol[i]) {
                exact_count++;
                attempt[i] = ' ';   /* set the matches to impossible values */
                tempsol[i] = -1;
            }
        }
/*
** find the near matches
*/
        for( i = 0; i < numcols; i++) {
            for( j = 0; j < numcols; j++) {
                if( attempt[i] == tempsol[j]) {
                    near_count++;
                }
            }

        }

        for (i = 0; i < numcols; i++)       /* clear to spaces */
            guess_out[i] = ' ';
        for( i=0; i<exact_count; i++)       /* put in the B's */
            guess_out[i] = 'B';
        for( i=0; i<near_count; i++)        /* put in the W's */
            guess_out[i+exact_count] = 'W';

        printf("%40s\n", guess_out);
        for (i = 0; i <= numcols; i++)       /* clear to spaces */
            guess[i] = guess_out[i];

    } while (error);

}
0
 
emmonsCommented:
Sageguy?
Did this code work for you?
0
 
emmonsCommented:
Here's your 'A'

Since my last posting I was able to solve my last problem. In a nutshell, I made a static copy of the solution, checked for strong matches, and changed that match in the solution from a number to 'X'. Then I checked for weak matches and did the same. This prevented multiple hits off the same solution number.

Thanks, emmons, I'll check out your code. Especially, I'll check to see if your ouput parameter gives a better reading of the user's guess than mine does.
0
 
SageguyAuthor Commented:
Here's your 'A'

Since my last posting I was able to solve my last problem. In a nutshell, I made a static copy of the solution, checked for strong matches, and changed that match in the solution from a number to 'X'. Then I checked for weak matches and did the same. This prevented multiple hits off the same solution number.

Thanks, emmons, I'll check out your code. Especially, I'll check to see if your ouput parameter gives a better reading of the user's guess than mine does.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now