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.)
Related
I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.
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.
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;
}
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");
}
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.