C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 25th, 2012, 11:25 PM
GugaVerdao GugaVerdao is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 GugaVerdao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 54 sec
Reputation Power: 0
Need help with C code

//• Write a program that reads letters from a file called“inputLetter.txt”.
• Your program will ask the user to enter the number of games they wish to play (1 to 4)
• Your program will open the inputLetter.txt file read in one character at a time and repeat this for the number of games the user wants to play.
• For this assignment the test file will contain at least 15 letters, all lowercase
• When the number of games has been played, the program will end
• A sample of an input file that you can use to test your program is included with the assignment.
• A preprocessor directive must be used to define the maximum number of guesses as 6
• If the player has used up all of their guesses, a message that the game is over should be displayed along with the letter they were trying to guess.
• You must have at least 4 user defined functions as follows:

//this function provides instructions to the user on how to play the game
void Instructions( );

//this function runs one entire game. It for checks either 6 incorrect guesses or a correct guess.
//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int PlayGuess(char solution);

//this function prompts the player to make a guess and returns that guess
//this function is called from inside the PlayGuess( ) function described above
char GetLetter( );

//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//It lets the user know if the guess comes alphabetically before or after the answer
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
int CompareLetters(char guess, char solution);


//this is my code, i'm completely lost, i have no idea what else to do.



Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#define MAXGUESSES 6

//function prototypes with a comment for each one describing what the function does.
//Copy and paste from assignment

int main()
{

	
    //variable declarations
    int 
    i = 0;
    FILE* infile;
    int numberofgames;
    char ans, gameFunction;
        
    //prompt and get number of games
    
    printf("Welcome to Letter Guess\n");
    printf("You will enter the number of games you want to play (1 - 4 games)\n");
    printf("You have 6 chances to guess each letter\n");
    printf("Let's begin:\n\n");
    
    printf("How many games do you want to play (1-4)");
    scanf("%d",& numberofgames);
    
    
    //getting the file
    
    infile = fopen("inputLetter.txt", "r");
    
    
    	
    for(i=1;i<=numberofgames;i++)
    	{
            fscanf(infile," %c",&ans);
            printf("Let's play game %d\n", i);
            PlayGuess(ans);
 
         }
    
    fclose(infile);
    system("pause");
    return 0;
}

int PlayGuess(char answer)
{	
    
    char getguess = 0;
	int numGuesses = 0, numberofgames = 0;
	while(numGuesses < MAXGUESSES)
	{	
            printf("Enter a guess\n");
            scanf("%c", getguess);
            if(answer==getguess)
            {
             printf("You guessed it!!!\n");  
                            
            }else{
                 if(answer>getguess)
                 {
                    printf("the letter you are trying to guess comes before:%d\n", getguess);
                 }else if(answer<getguess){
                       printf("the letter you are trying to guess comes after:%d\n", getguess);
                 }
        
            }
numGuesses = numGuesses +1;
	}
	
}


The out put looks like this


How many games do you want to play (1-5) 3

************************************
Let's play game 1
Enter a guess: e

the letter you are trying to guess comes before e
Enter a guess: c

the letter you are trying to guess comes before c
Enter a guess: a
You guessed it!!!

************************************
Let's play game 2
Enter a guess: t

the letter you are trying to guess comes before t
Enter a guess: a

the letter you are trying to guess comes after a
Enter a guess: p

the letter you are trying to guess comes before p
Enter a guess: n

the letter you are trying to guess comes before n
Enter a guess: g

the letter you are trying to guess comes after g
Enter a guess: k

the letter you are trying to guess comes after k
You did not guess the letter. It was m

Reply With Quote
  #2  
Old September 26th, 2012, 03:39 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by GugaVerdao
this is my code, i'm completely lost, i have no idea what else to do.


You could tell us what you expected to happen when you run your program. And what effectively happens. The compilation errors or warnings if that's your problem.

Also using code tags makes the code easier to study; and gets you faster (and better) answers.

Reply With Quote
  #3  
Old September 26th, 2012, 08:48 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
I rearranged your code a bit and compiled with warning enabled: -Wall
c.c: In function ‘PlayGuess’:
c.c: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

This is a rather critical problem since it appears in scanf. scanf changes data at a memory location you provide, which is worse? (oh, remember that lawsuit against Microsoft when excel displayed the wrong magnitude of numbers in cells. I was aware of that problem at the time because I observed it. I knew what the data should be, and knew that the representation didn't match) than merely using bad data.
Code:
conversions = scanf("%c",address_of_a_location_big_enough_for_at_least_one_character); /* you need this, then make sure conversions matches your expectation. */


When I ran your program it core dumped before it got there. That's because I didn't have an input file named as you've named it. You also must check that

infile = fopen("inputLetter.txt", "r");

succeeds.

if (NULL == infile) open_failure_action();
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : September 26th, 2012 at 08:53 AM.

Reply With Quote
  #4  
Old September 26th, 2012, 09:21 AM
GugaVerdao GugaVerdao is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 GugaVerdao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 54 sec
Reputation Power: 0
Thanks! Can you clarify, I'm new to C

Reply With Quote
  #5  
Old September 26th, 2012, 10:23 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
The output you presented must have been the desired output rather than actual from running your program.

warnings are critical! turn on your compiler warnings -Wall to see them, then fix them.

Code:
/* see my comments marked with XXXXXXXXXXXXXXXX */


#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h> /* declare system for the compiler XXXXXXXXXXXXXXXX */
#define MAXGUESSES 6

void PlayGuess(char); /* declare play guess for the compiler XXXXXXXXXXXXXXXX*/

int main() {
  //variable declarations
  int
    i = 0;
  FILE* infile;
  int numberofgames;
  char ans; /* gameFunction;  eliminate unused variable, XXXXXXXXXXXXXXXX */

  /* fail as soon as possible XXXXXXXXXXXXXXXX */
  /* don't make the user enter something if the program can't succeed. XXXXXXXXXXXXXXXX */
  /* (on that theme, we could read 4 characters here as well.)XXXXXXXXXXXXXXXX */
  infile = fopen("inputLetter.txt", "r");

  if (NULL == infile) {		/* verify your file of letters XXXXXXXXXXXXXXXX*/
    fprintf(stderr,"\nQuitting!  Failed to open %s\n","inputLetter.txt");
    return -1;
  }

  //prompt and get number of games

  puts("Welcome to Letter Guess"); /* puts is more efficient XXXXXXXXXXXXXXXX */
  puts("You will enter the number of games you want to play (1 - 4 games)");
  puts("Lambert Electronics, LLC.  USA, NY.");
  puts("You have 6 chances to guess each letter");
  puts("Let's begin:\n");

  printf("How many games do you want to play (1-4)"); /* XXXXXXXXXXXXXXXX */
  if (1 != scanf("%d",& numberofgames))	/* verify that scanf worked */
    numberofgames = 0;
  else if (numberofgames < 1) /* validate numberofgames.  Not important, don't belabor this, clip to interval [1,4] */
    numberofgames = 1;
  else if (4 < numberofgames)
    numberofgames = 4;

  for(i=1;i<=numberofgames;i++) { /* for loop clearer than while with scattered initializer and increment XXXXXXXXXXXXXXXX */
    fscanf(infile," %c",&ans);
    printf("Let's play game %d\n", i);
    PlayGuess(ans);
  }

  fclose(infile);
  system("pause");
  return 0;
}

void /* int unused, unsupplied return type. XXXXXXXXXXXXXXXX */ PlayGuess(char answer) {

  char getguess = 0;
  int numGuesses /*, numberofgames = 0 eliminate unused variable XXXXXXXXXXXXXXXX */;
  for (numGuesses = 0; numGuesses < MAXGUESSES; ++numGuesses) {
    puts("Enter a guess");  /* puts more efficient XXXXXXXXXXXXXXXX */
    scanf(" %c", &getguess);  /* crazy!!!  you need the address of the character XXXXXXXXXXXXXXXX */
    /* inserted a space character before % in previous scanf XXXXXXXXXXXXXXXX */
    if(answer>getguess)	      /* AFTER  XXXXXXXXXXXXXXXX */
      printf("the letter you are trying to guess comes after:%c\n", getguess); /* I admit the integer is more useful but I doubt you intended it  changed %d to %c XXXXXXXXXXXXXXXX */
    else if(answer<getguess)	/* BEFORE XXXXXXXXXXXXXXXX */
      printf("the letter you are trying to guess comes before:%c\n", getguess);	/* The integer IS more useful.  I doubt you intended it.  Changed %d to %c XXXXXXXXXXXXXXXX */
    else	/* rearrangement eliminates a test XXXXXXXXXXXXXXXX */
      puts("You guessed it!!!");
  }
  /* ++numGuesses; useless, removed XXXXXXXXXXXXXXXX */
}

Reply With Quote
  #6  
Old September 26th, 2012, 10:39 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
Quote:
Originally Posted by GugaVerdao
Thanks! Can you clarify, I'm new to C

Stating it another way:
Warnings are very important. They are even more important than error messages. Never ignore warnings! And if you have your compiler set up to not display warnings, then you need to tell it to display them, all of them. Or as we will often say here: "Turn warnings on, and up!"

Exactly how you turn warnings on differs from one compiler/IDE to another. b49P23TIvg was offering -Wall because that's how we do it with a rather common compiler, gcc. However that macro, _CRT_SECURE_NO_DEPRECATE, tells me that you're using Microsoft's Visual Studio, possibly 2008 or 2010.

In Visual Studio 2008, the Warning Level is the setting you'd want to change. You would find that in the project's property pages (accessible via the Main Menu under Project), then on the property page: Configuration Properties, C/C++, General. If warnings are turned off, then set them to at the very least Level 3.

However, if warnings are indeed turned on (I've never changed that setting, so it apparently defaults to Level 3), then the problem is that you have been ignoring them. You must not ignore warnings!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help with C code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap