How to print loop result in printf statement in C - c

I built a letter guessing game and basically the user enters the number of games they want to play, I get a letter from the file, user enters a guess, and i tell them if they are right or not. What i want to do is print "Ready for game #1" after they enter the number of games and also "Getting guess #1" before getting the guess letter. I know its from the loop "i" but i can't seem to figure it out. Here is my code
C
#define _CRT_SECURE_NO_WARNGINGS
#include<stdio.h>
#include<ctype.h>
#define MAXGUESSES 5
//this function provides instructions to the user on how to play the game
void GameRules();
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function
void GuessTheLetter(char);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above
char GetTheGuess();
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer
int CompareLetters(char, char);
int main() {
FILE *inPtr;
int numGames, i = 0;
char letter;//letter from file
printf("Welcome to the Letter Guessing Game\n");
GameRules();
printf("How many games? (1 to 8)\n");
scanf("%d", &numGames);
inPtr = fopen("letterList.txt", "r");
for (i = 0; i < numGames; i++) {
fscanf(inPtr, " %c", &letter);
letter = tolower(letter);
GuessTheLetter(letter);
}
return 0;
}
void GuessTheLetter(letter) {
int win = 0;
int numGuesses = 0;
char guess;
while (numGuesses < MAXGUESSES && win == 0) {
guess = GetTheGuess();
guess = tolower(guess);
win = CompareLetters(letter, guess);
numGuesses++;
}
if (win == 1) {
printf("And you Won !!!\n");
}
else {
printf("SORRY, you did not win this round\n");
}
}
char GetTheGuess() {
char guessEntered;
printf("Enter a guess\n");
scanf(" %c", &guessEntered);
return guessEntered;
}
int CompareLetters(letter, guess) {
int winGet;
if (letter == guess) {
printf("The solution and the guess are the same ( %c )", letter);
winGet = 1;
}
else if (letter != guess) {
printf("The solution comes before your guess ( %c )", letter);
winGet = 0;
}
else {
printf("Error!");
}
return winGet;
}
void GameRules() {
printf("First, you will enter the number of games you want to play(1 - 8 games)\n");
printf("For each game you will have 5 chances to guess each letter\n");
printf("Let's begin:\n\n");
}

Is it ok now?
#define _CRT_SECURE_NO_WARNGINGS
#include<stdio.h>
#include<ctype.h>
#define MAXGUESSES 5
//this function provides instructions to the user on how to play the game
void GameRules();
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function
void GuessTheLetter(char);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above
char GetTheGuess();
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer
int CompareLetters(char, char);
int main() {
FILE *inPtr;
int numGames, i = 0;
char letter;//letter from file
printf("Welcome to the Letter Guessing Game\n");
GameRules();
printf("How many games? (1 to 8)\n");
scanf("%d", &numGames);
inPtr = fopen("letterList.txt", "r");
for (i = 0; i < numGames; i++) {
printf("Ready to play Game %d\n", i);
fscanf(inPtr, " %c", &letter);
letter = tolower(letter);
GuessTheLetter(letter);
}
return 0;
}
void GuessTheLetter(char letter) {
int win = 0;
int numGuesses = 0;
char guess;
while (numGuesses < MAXGUESSES && win == 0) {
guess = GetTheGuess();
guess = tolower(guess);
win = CompareLetters(letter, guess);
numGuesses++;
}
if (win == 1) {
printf("And you Won !!!\n");
}
else {
printf("SORRY, you did not win this round\n");
}
}
char GetTheGuess() {
char guessEntered;
printf("Enter a guess\n");
scanf(" %c", &guessEntered);
return guessEntered;
}
int CompareLetters(char letter, char guess) {
int winGet;
if (letter == guess) {
printf("The solution and the guess are the same ( %c )\n", letter);
winGet = 1;
}
else if (letter != guess) {
printf("The solution comes before your guess ( %c )\n", letter);
winGet = 0;
}
else {
printf("Error!\n");
}
return winGet;
}
void GameRules() {
printf("First, you will enter the number of games you want to play(1 - 8 games)\n");
printf("For each game you will have 5 chances to guess each letter\n");
printf("Let's begin:\n\n");
}

Related

having problems with the while loop in C

this code is not workin for some reason. i tried doing the while loop with stdbool.h lib but that doesn't work. can someone tell me where am i wrong?. i was just trying to make a rock paper game in c in which the right the the rules for winning and when the user has not won i declare it that he has lost. its probably something with loop
#include <stdio.h>
#include <string.h>
void win(){
printf("You win!\n");
}
int main(){
int i =1;
int var=1;
char machine_choice[3];
char ask='\0';
//things that can happen
machine_choice[0]="rock";
machine_choice[1]="paper";
machine_choice[2]="scissor";
srand((unsigned int)time(NULL));
int machine = rand() % 3; //machines actuall choice
char choice[50];
while (i=1) {
printf("what is ur choice. write it in lowercase pls.\n");
scanf("%s", choice);
printf("%s\n", choice);
printf("%s\n", machine_choice[machine]);
if (choice==(machine_choice[machine]))
{
printf("its a tie!\n");
var++;
}
if (choice=="paper" && machine==0)
{
win();
var++;
}
if (choice=="scissor" && machine==1)
{
win();
var++;
}
if (choice=="scissor" && machine==1)
{
win();
var++;
}
if (var==1)
{
printf("do u want to play again(y/n)\n");
scanf(" %c", &ask);
if (ask=='n')
{
printf("okayy!");
break;
}
else
{
var=1;
}
}
}
return 0;
}```
The problem is in the while (i=1) the i always remains equal to 1 so it will never exit that loop, even if you entered an assignment with i = 1 and not a condition. If you wanted to enter a condition you had to enter while(i==1).

Read values into an array fails

I want to use a function to scanf up to 10 values for an array with the size 10, and also keep track of the number of values that are in the array because I'll need it later for solving some maths about the array, (max value, min value, etc.).
#include <stdio.h>
int enter(int MeasurmentData[], int nrOfmeasurments)
{
for(int i=0;i<10;++i)
{
int MeasurmentData[10];
scanf("%d",&MeasurmentData[i]);
int nrOfmeasurments = 0;
nrOfmeasurments ++;
return nrOfmeasurments;
}
int main()
{
int MeasurmentData[10];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n");
printf("v (View)\n");
printf("e (Enter)\n");
printf("c (Compute)\n");
printf("r (Reset)\n");
printf("q (Quit)\n");
printf("enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') \\ enter values
{
int MeasurmentData[10];
int nrOfmeasurments;
enter(MeasurmentData, nrOfmeasurments);
}
else if(menuoption == 'v') \\\ view values
{
//printf("%d", MeasurmentData[]);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
return 0;
}
}
}
When I run the program it should print Measurment tool 2.0, after the the user has the choice of inputting e(enter) which will scan in up to 10 values into an array, if the user clicks q(quit) while in the enter option already he will be returned to the main menu where he can do whatever.
V(view) prints out the array for the user so that he can view what elements are inside.
C(compute) uses the elements inside and the nr of elements to calculate the highest value element, lowest.
There are some errors in your code. Ill try to explain. You have over declared your variables too many times. And since you have a fixed loop you don't need to count the measurements you will always read 10 measurements.
Below are the code with some modifications. Feel free to ask anything about it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXIMUM_MEASURMENT 10
int enter(int MeasurmentData[])
{
char input[100];
int nrMeasurement = 0;
// reseting Measurment data
for(int i=0;i<MAXIMUM_MEASURMENT;++i) MeasurmentData[i] = 0;
for(int i=0;i<MAXIMUM_MEASURMENT;++i)
{
scanf("%99s", input);
if(strcmp(input, "q") == 0) {
break;
}
MeasurmentData[i] = (int) strtol(input, (char **)NULL, 10);
nrMeasurement++;
}
return nrMeasurement;
}
void showMeasurments(int* MeasurmentData, int length) {
int i = 0;
printf(" ======== Measurment ======== \n");
for(i = 0; i < length; i++) {
printf("%d ", MeasurmentData[i]);
}
printf("\n");
}
int main()
{
int MeasurmentData[MAXIMUM_MEASURMENT];
int nrOfmeasurments;
char menuoption;
while (1)
{
printf("Measurment tool 2.0\n" "v (View)\n" "e (Enter)\n" "c (Compute)\n" "r (Reset)\n" "q (Quit)\n enter your option:\n");
scanf(" %c", &menuoption);
if (menuoption =='e') // enter values
{
enter(MeasurmentData);
}
else if(menuoption == 'v') // view values
{
// show
showMeasurments(MeasurmentData, MAXIMUM_MEASURMENT);
}
else if(menuoption == 'c')
{
}
if(menuoption == 'q')
{
printf("Exiting Measurment tool 2.0\n");
break;
}
}
return 0;
}
Edit: i have updated the code. So i have read the comments of your question and there you have explained a little better what you are trying to accomplish. So since you have the requirement to press 'q' to stop reading values. I have to read all measurments as string and convert to integer if it is not the character q.
Edit 2: Thanks to #user3629249 to point out some of the flaws from the code ill update with his suggestions.

Function Crashes Main After Finishing Execution - C

I made this simple program that asks the user to input the number of columns the matrix called arp is going to have because, that way when the program asks the user to input a number so it can find matches on the numbers stored at the array without comparing all 10 columns allocated on memory with array to pointer type.
The problem here comes when the user inputs into the columns size definition 2. All works fine before the last function of p3() does its job, then it doesn't even return to main to execute the infinite loop defined there. I have tried removing the pointers and didn't work; I also tried removing other parts of the code but still nothing...
Update: Tried removing the function to find elements felmnt() and still the same.
Here is The Buggy Code:
#include <stdio.h>
#include <stdlib.h>
int loop = 1;
void keepalive(void)
{
int ckr = 0;
fflush(stdin);
printf("\n\n ******[s]<< CONTINUE | EXIT >>[n]******\n");
while(printf(" > ") && (ckr = getchar()) != 's' && ckr != 'n') fflush(stdin);
getchar();
if(ckr == 'n') loop = 0;
system("CLS");
}
void felmnt(int *colu, int (*arp)[10])
{
int nius=0, colmts, x, i, ejct;
do
{ // loop to let the user find more elements inside matrix
colmts=0;
printf("\n Insert The Number To Find\n > ");
scanf("%d", &nius);
for(x=0; x<*colu; x++) // search of element inside matrix
{
for(i=0; i<=9; i++)
if(nius == arp[i][x])
colmts++;
}
if(colmts>1) // results printing
{
printf("\n %d Elements Found", colmts);
}else if(colmts)
{
printf("\n 1 Element Found");
}else
{
printf("\n Not Found");
}
printf("\n TRY AGAIN? s/n\n > ");
ejct=getchar();
getchar();
}while(ejct=='s');
}
void mat(int *colu, int (*arp)[10])
{
int ci, cn, tst=0;
for(ci=0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++)
{
printf("\n Input The Number [%d][%d]\n > ", ci+1, cn+1);
scanf(" %d", &arp[cn][ci]);
}
}
printf(" Numbers Inside Matrix> ");
for(ci = 0; ci<*colu; ci++)
{
for(cn=0; cn<10; cn++) printf(" %d", arp[cn][ci]);
}
}
void p3(void)
{ // >>>>main<<<<
int colu=0;
while(loop)
{
printf("\n Input The Quantity Of Columns To Use\n > ");
scanf("%d", &colu);
int arp[10][colu];
mat(&colu, arp);
felmnt(&colu, arp);
keepalive();
}
}
int main(void)
{
int ck = 0, ndx;
while(1)
{ // infinite loop
p3();
fflush(stdin);
printf("\nPause !!!");
getchar();
}
return 0;
}

C Outputting array from another function

I am trying to copy the array winner from my function 'enter', so that i am able to just output it on the 'previous' function. When picking the option for the previous option I have gotten nothing outputting. Its only the last function named 'previous' that is not working, but to produce the problem the majority of the code is needed.
#include <stdio.h>
#include <string.h>
char enter(char names[][20]);
void menu();
void previous(char winner[][8]);
int main()
{
char names[16][20];
int i;
printf("Please enter the names of the players:\n");
/*Making the user enter 16 times*/
for (i = 0; i < 16; i++)
{
scanf("%9s", &names[i]);
fflush(stdin);
}
/*Clearing Screen*/
system("cls");
menu(names);
return names[16][20];
}
void menu(char names[][20], char winner[][8])
{
int choice;
printf("Please select one of the following options:\n\n"
"Press 1 to enter game results\n"
"Press 2 to display the current round\n"
"Press 3 to display the players advancing to the next round\n"
"Press 4 to display the previous round\n"
"Press 5 to exit the program\n");
scanf("%d", &choice);
if(choice == 1)
{
enter(names);
}
system("cls");
if(choice == 3)
{
previous(winner);
}
}
char enter(char names[][20])
{
int result;
int score1;
int score2;
int p, c, j, l, i;
char winner[8][8];
system("cls");
for(i = 0; i < 8; i++)
{
printf("\n\n%s vs %s",names[i],names[i+8]);
score1 = 0;
score2 = 0;
for(j = 0; j < 5; j++)
{
printf("\n\nEnter game %d results, press 1 if %s won or"
" 2 if %s won :\n",(j+1), names[i], names[i+8]);
scanf("%d", &result);
if(result == 1)
{
score1++;
}
if(result == 2)
{
score2++;
}
printf("\n\n1Current score is %d-%d", score1, score2);
if(score1 == 3)
{
printf("\n\n%s adavances to the next round!",names[i]);
strncpy(winner[i], names[i], 10);
printf("\n\nPress Enter to Continue");
getch();
system("cls");
break;
}
if(score2 == 3)
{
printf("\n\n%s adavances to the next round!",names[i+8]);
strncpy(winner[i], names[i+8], 10);
printf("\n\nPress Enter to Continue");
getch();
system("cls");
break;
}
}
}
system("cls");
printf("The players advancing to the next round are:\n\n");
for(p = 0; p < 8; p++)
{
for(c = 0; c < 8; c++)
{
printf("%c",winner[p][c]);
}
printf("\n");
}
printf("\n\nPress Enter to Continue");
getch();
system("cls");
menu(names, winner);
return winner[8][8];
}
void previous(char winner[][8])
{
int i, j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
printf("%c",winner[i][j]);
}
printf("\n");
}
}
There is no data for the array winner in your program! At least not when you call it for the first time.
The signature for the menu function is:
void menu(char names[][20], char winner[][8]);
but you call it from main like this:
menu(names);
The winner parameter is missing. This shouldn't happen, but you have declared a prototype for this function, namely:
void menu();
Unfortunately, C treats the empty parens as meaning "whatever parameters you pass", not as function that takes no parameters. That means that your function call slips by. The fix is to provide the correct signature for the prototype and also to pass a suitable winner array from main.
Strangely, your enter function provides a local array winner. This array will always be a new array when you call enter. That's probably not what you want. As is, your program should have one names and one winner array. (You can pass these arrays around, but you should make sure that tese arrays are consistent. Don't create new arrays when you really want to operate on existing ones.)
You also call your menu recursively. That means the you go ever deeper into the call structure without real benefit. Dont do that; use a loop instead: do display the menu while the user hasn't chosen "quit". (There are applications for recursive functions, but this isn't one.)

C Problems with menu

I am having problems with the menu function of my program. The last 2 parts of the printf for the menu are continuing onto the next function.
Code for menu function -
#include <stdio.h>
#include <string.h>
void enter(char names[16][20]);
void menu();
int main()
{
char names[16][20];
int i;
printf("Please enter the names of the players:\n");
/*Making the user enter 16 times*/
for (i = 0; i < 16; i++)
{
scanf("%9s", &names[i]);
fflush(stdin);
}
/*Clearing Screen*/
system("cls");
menu();
return names[16][20];
}
void menu(char names[][20])
{
int choice;
printf("Please select one of the following options:\n\n"
"Press 1 to enter game results\n"
"Press 2 to display the current round\n"
"Press 3 to display the players advancing to the next round\n"
"Press 4 to display the previous round\n"
"Press 5 to exit the program\n");
system("cls");
scanf("%d", &choice);
if(choice == 1)
{
enter(names);
system("cls");
}
}
void enter(char names[][20])
{
int result;
int score1;
int score2;
int p, c, j, l, i;
char winner[8][8];
system("cls");
for(i = 0; i < 8; i++)
{
printf("\n\n%s vs %s",names[i],names[i+8]);
score1 = 0;
score2 = 0;
for(j = 0; j < 5; j++)
{
printf("\n\nEnter game %d results, press 1 if %s won or"
" 2 if %s won :\n",(j+1), names[i], names[i+8]);
scanf("%d", &result);
if(result == 1)
{
score1++;
}
if(result == 2)
{
score2++;
}
}
}
Somehow the press 4 and 5 options are getting into the next next function
Image -
https://gyazo.com/7e99cfb42a18d04a144d3d139409d6ec
Ok.. I ran your code with some changes and it ran fine, without the two printf lines that you have shown in your image file.
First thing is, the menu() function you have written wrong. It's prototype showing that it takes no arguments while in it's declaration it take an array. You need to pass the names array to menu function as you are going to pass it to the enter function. So, both the functions menu() and enter() will be taking names array as an argument.
I used the code without system("cls") function as my compiler didn't find it.
So, it has worked at my side, I am getting feeling like, system("cls") is causing you the problem that you shown in the image.

Resources