C If Loop Fails to Set Struct Data - c

This has stumped me for a few days now. I have tried searching but have turned up mostly just definitions for struct...
In an if loop I set 2 struct variables, but neither of them come out correct. guests[x].plusone is stuck at 0 and guests[x].plusonefood is simply empty. If I set them (with the same line) outside of the loop, however, there are no issues. My compiler shows no warnings or errors. What am I missing?
This is my first project with C, so please point out anything else you notice, too.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct guestinfo
{
char name[50];
short int plusone;
char food[50];
char plusonefood[50];
};
char temp[5];
char setfood (int f)
{
char foodtemp[f];
int x;
for (x=0; x < f; x++)
{
printf("Food option %d:\n", (x+1));
fgets(&foodtemp[x],100,stdin);
}
return *foodtemp;
}
int main ()
{
int number_of_rsvp=0;
int number_of_food=0;
printf("Number of food choices:\n");
fgets(temp,5,stdin);
number_of_food = atoi (temp);
while (number_of_food <= 0)
{
printf("Please enter a number greater than 0\n");
fgets(temp,5,stdin);
number_of_food = atoi (temp);
}
char food [50] [number_of_food];
**food = setfood(number_of_food);
printf("Number of RSVPs:\n");
fgets(temp,5,stdin);
number_of_rsvp = atoi (temp);
while (number_of_rsvp <= 0)
{
printf("\nPlease enter a number greater than 0\n");
fgets(temp,5,stdin);
number_of_rsvp = atoi (temp);
};
struct guestinfo guests[number_of_rsvp];
int x;
int f;
for (x=0; x < number_of_rsvp; x++)
{
// add input validation to this section
printf("Guest Number %d:\n\nGuest Name:\n", (x+1));
fgets(guests[x].name,50,stdin);
printf("Food Choice #:\n");
fgets(temp,3,stdin);
f = atoi(temp);
f--;
*guests[x].food = food [50] [f];
printf("Plus One? Y/N\n");
fgets(temp,3,stdin);
}
if (strchr (temp,'Y') != NULL || strchr (temp,'y') != NULL) //This loop
{
guests[x].plusone = 1;
printf("Plus one food choice #:\n");
fgets(temp,3,stdin);
f = atoi(temp);
f--;
*guests[x].plusonefood = food [50] [f];
}
else if (strchr (temp,'N') != NULL || strchr (temp,'n') != NULL)
{
guests[x].plusone = 0;
};
FILE *guestlist = fopen ("guestlist.txt","w");
// debugging
printf("%d\n",guests[0].plusone);
printf("%s\n",guests[0].plusonefood);
for (x=0; x < number_of_rsvp; x++)
{
fprintf(guestlist,"Guest Number %d:\n\nName: %s\nFood Choice: %s\n",(x+1),guests[x].name,guests[x].food);
switch (guests[x].plusone)
{
case 1:
{
fprintf(guestlist,"Plus One: Yes\n\tPlus One Food: %s\n\n",guests[x].plusonefood);
break;
}
case 0:
{
fprintf(guestlist,"Plus One: No\n\n");
break;
}
default:
{
break;
}
}
}
fclose (guestlist);
printf("Printing Guest List...\n");
return 0;
}

The problem is that you are accessing an invalid memory place.
The array has number_of_rsvp elements.
Your for() loop interates over the variable x up to the value number_of_rsvp and exits.
After that, the value of x is equal to number_of_rsvp.
Since the index of the array only can go from 0 to number_of_rsvp - 1, the element guests[x], which is guests[number_of_rsvp], is out-of-bound.

Related

I want to compare multiple strings using the strcmp and display the output after it succesfully verify both inputs

typedef struct uLogin
{
char user1,user2,user3,user4;
} uLogin;
void loginUser() {
uLogin x1;
int i, m, n;
char pass_list[20] = "12345678",name[10],pass[10];
x1.user1 = "DSET2G1";
x1.user2 = "DSET2G2";
x1.user3 = "DSET2G3";
x1.user4 = "DSET2G4";
printf("\nEnter username: ");
scanf("%s",&name);
fflush(stdout);
printf("\nEnter Password :");
scanf("%s",&pass);
fflush(stdout);
m = (strcmp(name,x1.user1)==0 && strcmp(name,x1.user2)==0 && strcmp(name,x1.user3)==0 && strcmp(name,x1.user4)==0);
n = strcmp(pass,pass_list);
if(m == 0 && n == 0)
{
system("CLS");
printf("Hello");
}
else
{
system("CLS");
printf("Bye");
}
}
Im trying to get this code to work but I dont understand why does the code terminates after typing the username and password for this simple user login page. Any ideas and can someone explain whats wrong? Sorry for bad english
the first thing that you should notice is that the member of your struct are just char, and you are trying to assign a full string to them. You should see that from the compiler warnings.
However, I saw several issues in your code and I provide here an improved version which should be more safe/stable in terms of memory corruption. Note that I used a fixed memory length for the strings, you can upgrade the code with dynamic memory but only if you know what you are doing.
See comments in the code for the description of the changes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERNAME_LEN 256
// Change 1 -> Declare the member of the struct as pointers to char
typedef struct uLogin
{
char* user1,*user2,*user3,*user4;
} uLogin;
void loginUser() {
uLogin x1;
int i, m, n;
char pass_list[MAX_USERNAME_LEN] = "12345678";
char name[MAX_USERNAME_LEN],pass[MAX_USERNAME_LEN];
// Change 2: declare the stings as local variables,
//then assign the address of them to the struct members
char s1[MAX_USERNAME_LEN] = "DSET2G1";
char s2[MAX_USERNAME_LEN] = "DSET2G2";
char s3[MAX_USERNAME_LEN] = "DSET2G3";
char s4[MAX_USERNAME_LEN] = "DSET2G4";
x1.user1 = s1;
x1.user2 = s2;
x1.user3 = s3;
x1.user4 = s4;
// Change 3: use a safer way to read the input
// I leave the error handling to you
char line[MAX_USERNAME_LEN];
if (fgets(line, sizeof(line), stdin)) {
if (1 == sscanf(line, "%s", (char*)&name)) {
/* i can be safely used */
}
}
if (fgets(line, sizeof(line), stdin)) {
if (1 == sscanf(line, "%s", (char*)&pass)) {
/* i can be safely used */
}
}
// Change 4 - Use strncmp to compare exactly MAX_USERNAME_LEN bytes
// Change 5 - Fix the logic to have at least one combination right
int ret1 = strncmp(name,x1.user1,MAX_USERNAME_LEN)==0;
int ret2 = strncmp(name,x1.user2,MAX_USERNAME_LEN)==0;
int ret3 = strncmp(name,x1.user3,MAX_USERNAME_LEN)==0;
int ret4 = strncmp(name,x1.user4,MAX_USERNAME_LEN)==0;
m = ret1 || ret2 || ret3 || ret4;
n = strncmp(pass,pass_list,MAX_USERNAME_LEN) == 0;
printf("%d %d %d %d -> m %d n %d \n",ret1,ret2,ret3,ret4,m,n);
if(m == 1 && n == 1)
{
//system("CLS");
printf("Hello\n");
}
else
{
//system("CLS");
printf("Bye\n");
}
}
int main(){
while(1)
loginUser();
}
Some output samples:
DSET2G1
12345678
1 0 0 0 -> m 1 n 1
Hello
DSET2G1
fdgsfhjsdf
1 0 0 0 -> m 1 n 0
Bye
DSET2G6
12345678
0 0 0 0 -> m 0 n 1
Bye

I have problem with reading input with blank spaces

I made this function to get input:
void entrada_dados(Time* time, int i){
scanf("%s %d %d", time[i].nome, &time[i].gols_marcados, &time[i].gols_sofridos);
};
The input is in this form:
2
Campinense
23
12
ABC
30
13
The main is:
int main(void) {
int n = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++){
entrada_dados(time, i);
}
....
My problem is when the team name have some space like to "São Paulo". I have tried some forms to solve, but no one solved my problem.
I tried:
void entrada_dados(Time* time, int i){
fscanf(stdin, "%[^\n] %d %d", time[i].nome, &time[i].gols_marcados, &time[i].gols_sofridos);
};
and:
void entrada_dados(Time* time, int i){
fgets(time[i].nome, 100, stdin);
scanf("%d", &time[i].gols_marcados);
scanf("%d", &time[i].gols_sofridos);
}
but in the first case the output have nothing, and second case the output miss some cases. Someone can help me to understand this problem?
Edit 1:
The definition of .name is:
typedef struct Time{
char nome[100];
int gols_marcados;
int gols_sofridos;
} Time;
Edit 2:
Solution:
One way to solve it:
Try two fscanfs fscanf(stdin, " %[^\n]", time[i].nome);
fscanf(stdin, "%d %d", &time[i].gols_marcados, &time[i].gols_sofridos);
Thank you guys.
Because you have to handle strings with spaces, it's better to use fgets for those.
But mixing fgets and scanf doesn't work too well. We can replace scanf with fgets followed by sscanf.
To decode numbers, we can use strtol or sscanf
We take advantage of the fact that each element/member of Time appears on a separate line in the input file, so we can do fgets for every line. This simplifies the code and makes error checking easier.
Here is the refactored code. It is annotated.
I didn't do this, but, if these sequences are done a lot, we can combine some of these sequences in helper functions to reduce some code replication (e.g. a function that combines the fgets followed by the sscanf)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Time {
char nome[100];
int gols_marcados;
int gols_sofridos;
} Time;
// RETURNS: 1=valid, 0=syntax error
int
entrada_dados(Time *timelist, int i)
{
char buf[100];
char *cp;
Time *tim = &timelist[i];
int valid = 0;
do {
// get name
if (fgets(tim->nome,sizeof(tim->nome),stdin) == NULL)
break;
// strip newline
tim->nome[strcspn(tim->nome,"\n")] = 0;
// get number using strtol
if (fgets(buf,sizeof(buf),stdin) == NULL)
break;
tim->gols_marcados = strtol(buf,&cp,10);
if (*cp != '\n')
break;
// get number using sscanf
if (fgets(buf,sizeof(buf),stdin) == NULL)
break;
if (sscanf(buf,"%d",&tim->gols_sofridos) != 1)
break;
// all input is okay
valid = 1;
} while (0);
return valid;
};
int
main(void)
{
int n = 0;
#if 0
scanf("%d", &n);
#else
char buf[100];
if (fgets(buf,sizeof(buf),stdin) == NULL)
exit(1);
sscanf(buf,"%d",&n);
#endif
// allocate sufficient space
Time *timelist = malloc(sizeof(*timelist) * n);
// read in data
int valid = 0;
for (int i = 0; i < n; i++) {
valid = entrada_dados(timelist, i);
if (! valid)
break;
}
// show the data
if (valid) {
for (int i = 0; i < n; i++) {
Time *tim = &timelist[i];
printf("nome='%s' gols_marcados=%d gols_sofridos=%d\n",
tim->nome,tim->gols_marcados,tim->gols_sofridos);
}
}
return 0;
}
Here is the program input:
3
Campinense
23
12
ABC
30
13
São Paulo
17
82
Here is the program output:
nome='Campinense' gols_marcados=23 gols_sofridos=12
nome='ABC' gols_marcados=30 gols_sofridos=13
nome='São Paulo' gols_marcados=17 gols_sofridos=82

Is there way to store an array with reccurring values at different indices in C?

I am creating a Hangman Project in C, where the word to be guessed is represented by dashes that are replaced with correct guesses as the game progresses. I used a linked list to store all of the words. I have figured out almost all of the functions however, my latest version of the game runs perfectly unless there is a recurring character in the word to be guessed. This occurs because I created a function that takes a guess from the user and compares it to the secret word. If the guess is correct on a specific index of the "secret" string, it sets the index of the char array of the dashes to that same guess(and displays the dashes for the player to guess). The problem is the dashes string is an array and if a character occurs twice it messes up the length of the dashes.
I'm not sure what to do from here. Is there a way to store a recurring value at two different indices or should I try a different method to store the string? Also if the question is confusing it may help to read the program and understand the code a little bit. I have formatted it with comments at every function so it shouldn't be too hard to understand even though my code is a bit messy. All help is greatly appreciated.
Link to the words.txt file in case you need it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int nodeNum, chances, misses, score, corrGuess;
char* secret;
char incorrGuess[50], guess, dashes[];
/*Node of linked list*/
typedef struct node {
char *data;
struct node *next;
} node;
node *start = NULL;
node *current;
/*Appending nodes to linked list*/
void add(char *line) {
node *temp = malloc(sizeof(node));
temp->data = strdup(line);
temp->next = NULL;
current = start;
if(start == NULL) {
start = temp;
} else {
while(current->next != NULL) {
current = current->next;
}
current->next = temp;
}
}
/*read text file*/
void readfile(char *filename) {
FILE *file = fopen(filename, "r");
if(file == NULL) {
exit(1);
}
char buffer[512];
while(fgets(buffer, sizeof(buffer), file) != NULL) {
add(buffer);
}
fclose(file);
}
/*Set initial dashes string*/
void setInitDash(){
int i;
for(i=0; i < strlen(secret)-1; i++){
dashes[i] = '-';
}
}
/*Generate random number between 0 and 2999*/
void randNum(int lower, int upper)
{
srand(time(0));
nodeNum = (rand() % (upper - lower + 1)) + lower;
}
/*Choose random node based on random number generated*/
void chooseRand(struct node* start)
{
node* p;
int n;
p = start;
for(n = 0; n != nodeNum; n++)
{
p = p->next;
}
secret = p->data;
setInitDash();
}
/*Check guess for correct guess*/
void checkGuess(){
int i, j = strlen(secret) - 1;
for(i = 0; i != j; i++){
if(secret[i] == guess) {
dashes[i] = guess;
score++;
}
}
}
/*Check if score changes*/
void checkScore(){
if(score == 0){
misses++;
chances--;
incorrGuess[misses] = guess;
}
else{
score = 0;
}
printf("%s", dashes);
}
/*Print current board*/
void printBoard(){
printf("__________________________________________________________________\n");
printf("\n\nCurrent Word: ");
checkGuess();
checkScore();
printf(" Chances: %d Incorrect Guesses: ", chances);
for(int i = 0; i <= misses; i++)
printf("%c", incorrGuess[i]);
printf("\nPlease enter your guess: ");
scanf(" %c", &guess);
printf("\n__________________________________________________________________\n");
}
/*Function for if player wins*/
void winCase(){
printf("\n\n\n\n\nGood Job! You have guessed the word correctly. Restart the program to play again!");
}
/*Function for if player loses*/
void loseCase(){
printf("\n\n\n\n\nUnfortunately, you have run out of chances. The word to guess was %s.", secret);
}
int main(){
int i;
score = 0;
corrGuess = score;
int stillPlaying = 1;
printf("Hello User, Welcome to Hangman\nPlease enter number of chances you would like: "); /*Welcome Message*/
scanf("%d", &chances);
readfile("words.txt"); /*Readfile and choose random word*/
randNum(0, 2999);
chooseRand(start);
printf("%s", secret); /*Print secret word for easy testing*/
printf("__________________________________________________________________\n"); /*Print first iteration of the board*/
printf("\n\nCurrent Word: %s", dashes);
printf(" Chances: %d Incorrect Guesses: %s", chances, incorrGuess);
printf("\nPlease enter your guess: ");
scanf(" %c", &guess);
printf("\n__________________________________________________________________\n");
while(stillPlaying == 1){ /*While loop to print board until win or lose case is true*/
printBoard();
if(dashes == secret){
stillPlaying = 2;
}
else if(chances <= 0){
stillPlaying = 0;
}
}
if(stillPlaying == 0){
loseCase();
}
else if(stillPlaying == 2){
winCase();
}
return 0;
}
You never initialized 'dashes'. Give it a size and you should be good. I plugged in 50 and the dash-replacement worked fine.
** Edit **
Thinking about it more, I recommend using malloc/calloc/realloc instead. So, declare dashes as:
char *dashes;
Then when you need to use it, allocate space:
void setInitDash() {
int i;
// calloc fills the space with 0x00, ensuring that your string has a null terminator
dashes = calloc(strlen(secret) + 1, sizeof(char);
//I don't know why you had strlen(secret-1), strlen ignores the null terminator
for(i = 0; i < strlen(secret); i++) {
dashes[i] = 'i';
}
}
Another big problem with this code:
if(dashes == secret)
Will always evaluate to FALSE, because it compares the addresses dashes and secret point to, not the values. To compare strings you either need to compare each individual array member or use the strcmp function from string.h

Add value of playing cards - with pointers and structures

I'm going through some c programming questions and I'm currently stuck on a pointer related question
Q: Write a function that takes the values of a two-card blackjack HAND as input, and returns the point total of the hand. The value
of the cards '2' through '9' is equal to their face value, the cards 'T', 'K', 'Q', 'J' are worth 10 points and the ace ('A') is worth 11 points
unless it comes with another ace, then that second ace is worth 1 point. The program should be able to catch incorrect input.
Examples:
Enter cards: A Q
The score is 21
Enter cards: A A
The score is 12
I've tackled this question before, but this time I'd have to use pointers which I'm still fairly new towards. Getting card values and calculating cards must be done in one function. Here's what i have so far:
#include <stdio.h>
#define HAND 2
struct player_hand
{
char card1;
char card2;
};
void getHandValue(struct player_hand * hnd_ptr, char size, char size2)
{
int first_card;
int second_card;
//get cards from user
scanf("%c %c",&hnd_ptr->card1, &hnd_ptr->card2);
printf("Enter Cards: %c %c", &hnd_ptr->card1, &hnd_ptr->card2);
//check value of first card in hand
if(hnd_ptr->card1<='9' && hnd_ptr->card1>='2')
{
first_card=(int)hnd_ptr->card1 -48;
}
//check for special cards: king, queen, jack, ten
else if(hnd_ptr->card1=='T'||hnd_ptr->card1=='K'||hnd_ptr->card1=='Q'||hnd_ptr->card1=='J')
{
first_card=10;
}
//if first card is Ace
else if(hnd_ptr->card1=='A')
{
first_card=11;
}
else
{
//card not valid
printf("Not a valid card: %c",hnd_ptr->card1);
return;
}
//check value of 2nd card
if(hnd_ptr->card2<='9' && hnd_ptr->card2>='2')
{
second_card=(int)hnd_ptr->card2 -48;
}
//if 2nd card is a special kind
else if(hnd_ptr->card2=='T'||hnd_ptr->card2=='K'||hnd_ptr->card2=='Q'||hnd_ptr->card2=='J')
{
second_card=10;
}
//if 2nd card is Ace
else if(hnd_ptr->card2=='A')
{
if(hnd_ptr->card1=='A')
second_card=1;
else
second_card=11;
}
else
{
//if 2nd card not valid
printf("Not a valid card: %c",hnd_ptr->card2);
return;
}
add cards
printf("\nThe total card value is: %d",first_card+second_card);
}
//call function, test if works
//calling it wrong?
int main(void)
{
struct player_hand hnd [HAND] = { {'A', 'A'}};
getHandValue (hnd, HAND);
return;
}
You have a few bugs.
Incorrect call in main.
The function doesn't need size arguments and if it did they should be int.
Bad return from main.
In the function, the printf is wrong.
Things are much more complicated than they need to be because the struct uses two scalars instead of an array.
I've created two versions of your program. One with bugs annotated. And another that cleans things up.
Here's the annotated version:
#include <stdio.h>
#define HAND 2
struct player_hand {
char card1;
char card2;
};
// NOTE/BUG: use 'int' for size and size2
void
getHandValue(struct player_hand *hnd_ptr, char size, char size2)
{
int first_card;
int second_card;
// get cards from user
scanf("%c %c", &hnd_ptr->card1, &hnd_ptr->card2);
// NOTE/BUG: this would print the _address_ of the values vs. the values
printf("Enter Cards: %c %c", &hnd_ptr->card1, &hnd_ptr->card2);
// NOTE/BUG [sort of]: the code below is cut-n-paste replication because you
// have separate card1 and card2 in the struct -- this "cries out" for an
// array and a loop. Consider the general case where you have 5 cards in the
// hand (e.g. five card charlie). The code would be easier even with an array
// of only two
// check value of first card in hand
if (hnd_ptr->card1 <= '9' && hnd_ptr->card1 >= '2') {
first_card = (int) hnd_ptr->card1 - 48;
}
// check for special cards: king, queen, jack, ten
else if (hnd_ptr->card1 == 'T' || hnd_ptr->card1 == 'K' || hnd_ptr->card1 == 'Q' || hnd_ptr->card1 == 'J') {
first_card = 10;
}
// if first card is Ace
else if (hnd_ptr->card1 == 'A') {
first_card = 11;
}
else {
// card not valid
printf("Not a valid card: %c", hnd_ptr->card1);
return;
}
// check value of 2nd card
if (hnd_ptr->card2 <= '9' && hnd_ptr->card2 >= '2') {
second_card = (int) hnd_ptr->card2 - 48;
}
// if 2nd card is a special kind
else if (hnd_ptr->card2 == 'T' || hnd_ptr->card2 == 'K' || hnd_ptr->card2 == 'Q' || hnd_ptr->card2 == 'J') {
second_card = 10;
}
// if 2nd card is Ace
else if (hnd_ptr->card2 == 'A') {
if (hnd_ptr->card1 == 'A')
second_card = 1;
else
second_card = 11;
}
else {
// if 2nd card not valid
printf("Not a valid card: %c", hnd_ptr->card2);
return;
}
printf("\nThe total card value is: %d", first_card + second_card);
}
//call function, test if works
//calling it wrong?
int
main(void)
{
// NOTE: based on usage, this is only an array because you're not using &hnd
// below
struct player_hand hnd[HAND] = {
{'A', 'A'}
};
// NOTE/BUG: too few arguments to function, but why pass count at all?
getHandValue(hnd, HAND);
// NOTE/BUG: need to return value (e.g. return 0)
return;
}
Here's the cleaned up version:
#include <stdio.h>
#define CARDS_PER_HAND 2
struct player_hand {
char card[CARDS_PER_HAND];
};
void
getHandValue(struct player_hand *hnd_ptr)
{
int idx;
int card;
int sum;
int count[CARDS_PER_HAND];
// get cards from user
printf("Enter Cards:");
fflush(stdout);
for (idx = 0; idx < CARDS_PER_HAND; ++idx)
scanf(" %c", &hnd_ptr->card[idx]);
// print cards
printf("Cards entered:");
for (idx = 0; idx < CARDS_PER_HAND; ++idx)
printf(" %c", hnd_ptr->card[idx]);
printf("\n");
for (idx = 0; idx < CARDS_PER_HAND; ++idx) {
card = hnd_ptr->card[idx];
// simple cards
if (card <= '9' && card >= '2') {
count[idx] = (card - '2') + 2;
continue;
}
switch (card) {
case 'A':
count[idx] = 11;
if ((idx == 1) && (count[0] == 11))
count[idx] = 1;
break;
case 'T':
case 'K':
case 'Q':
case 'J':
count[idx] = 10;
break;
default:
printf("Not a valid card: %c", card);
return;
break;
}
}
sum = 0;
for (idx = 0; idx < CARDS_PER_HAND; ++idx)
sum += count[idx];
printf("The total card value is: %d\n", sum);
}
int
main(void)
{
struct player_hand hnd;
getHandValue(&hnd);
return 0;
}
If, in addition to the other answers, your intent was to pass a 2-hand array, you would need to handle both hands within a loop in your scoring function. For example:
#include <stdio.h>
#define HAND 2
struct player_hand
{
char card1;
char card2;
};
void getHandValue (struct player_hand *hnd_ptr, int size)
{
int first_card;
int second_card;
/* get cards from user */
for (int i = 0; i < size; i++) {
printf ("\nenter cards for hand %d (card1 card2): ", i);
/* you must handle the '\n' that remains after last char */
if (scanf ("%c %c%*c", &hnd_ptr[i].card1, &hnd_ptr[i].card2) != 2) {
fprintf (stderr, "error: invalid entry.\n");
return;
}
printf ("you entered: %c %c\n", hnd_ptr[i].card1, hnd_ptr[i].card2);
}
for (int i = 0; i < size; i++)
{
/* check value of first card in hand */
if(hnd_ptr[i].card1 <= '9' && hnd_ptr[i].card1 >= '2')
{
first_card = (int)hnd_ptr[i].card1 - '0';
}
/* check for special cards: king, queen, jack, ten */
else if (hnd_ptr[i].card1 == 'T' || hnd_ptr[i].card1 == 'K' ||
hnd_ptr[i].card1 == 'Q' || hnd_ptr[i].card1 == 'J')
{
first_card = 10;
}
/* if first card is Ace */
else if (hnd_ptr[i].card1 == 'A')
{
first_card = 11;
}
else
{
/* card not valid */
printf("Not a valid card: %c",hnd_ptr[i].card1);
return;
}
/* check value of 2nd card */
if(hnd_ptr[i].card2 <= '9' && hnd_ptr[i].card2 >= '2')
{
second_card=(int)hnd_ptr[i].card2 - '0';
}
/* if 2nd card is a special kind */
else if (hnd_ptr[i].card2 == 'T' || hnd_ptr[i].card2 == 'K' ||
hnd_ptr[i].card2 == 'Q' || hnd_ptr[i].card2 == 'J')
{
second_card = 10;
}
/* if 2nd card is Ace */
else if (hnd_ptr[i].card2 == 'A')
{
if (hnd_ptr[i].card1 == 'A')
second_card = 1;
else
second_card = 11;
}
else
{
/* if 2nd card not valid */
printf ("Not a valid card: %c", hnd_ptr[i].card2);
return;
}
/* add cards */
printf ("\nThe total cards value (hand %d) is: %d\n",
i, first_card + second_card);
}
}
int main(void)
{
struct player_hand hnd[HAND] = { {'A', 'A'}, {'A', 'A'} };
getHandValue (hnd, HAND);
return 0;
}
Example Use/Output
$ ./bin/cards
enter cards for hand 0 (card1 card2): A A
you entered: A A
enter cards for hand 1 (card1 card2): 8 K
you entered: 8 K
The total cards value (hand 0) is: 12
The total cards value (hand 1) is: 18
If your intent was not to pass an array of struct, then obviously looping would not be necessary. Note: two loops were used. The first to get the cards for both hands, the second to compute the scores for both. (you could do it with one, but it looked as if you intended to have all cards entered before scoring) Look things over and let me know if you have further questions.
You're not passing the address of hnd to the function getHandValue(). To do this you have to pass the address using the & operator getHandValue(&hnd).
You're also not initializing struct player_hand hnd correctly. There's one set of {} too many.
Here's an edited version of your main() code that works, just some minor edits to how your pointer is set up.
// main
int main(void)
{
// minor edits to fix the code here
struct player_hand hnd = {'A', 'A'};
struct player_hand *hndPtr = &hnd;
getHandValue (hndPtr);
return 0;
}

Logic in detecting int in C

I would appreciate some help with this. I'm trying to create this simple program that repeatedly loops asking for the user to enter in an int. If he enters an int, it exits but if he enters something else or bigger than int (ex.4gs4r33) it will loop again asking to enter an int. This is what I have tried, and it's not working. It says it's an int even if it's not.
#include<stdio.h>
unsigned int findInt();
int main() {
printf("Please input an int.\n");
findInt();
}
unsigned int findInt() {
char input;
long num = 0;
int b = 0;
do {
scanf("%c", &input);
if (isdigit(input)){
num = num*10+input+'0';
b = 1;
}
else if (input == '\n')
b = 1;
else
b = 0;
} while(input != '\n');
if (b == 1)
printf("Great!\n");
else{
printf("Not an int \n");
findInt();
}
return 0;
}
Two possible approaches. One would be to modify your code:
b = 1; // start off with good intentions…
do {
scanf("%c", &input);
if (isdigit(input)){
num = num*10+input -'0'; // *** SUBTRACT '0', don't add it!
}
else if (input != '\n') {
b = 0;
break; // *** break when you find non-digit
}
} while (input != '\n');
Two changes: getting the math right as you compute the integer, and fixing the logic (so you break out of your loop when you find a non digit character)
Second approach:
char buf[100];
char intAsString[100];
fgets(buf, 100, stdin);
sscanf(buf, "%d", num);
sprintf(intAsString, "%d\n", num);;
if(strcmp(buf, intAsString) == 0 ) {
printf("yay - you entered an integer!\n");
}
I'm sure you can figure out how that works.
update a complete code snippet that solves the issue of "loop logic" as well: you call the findInt function once from the top level, and it keeps going until you get the int. Note - in order for this to work properly, I read the entire input at once (rather than one at a time), then pick off the characters one by one using sscanf (and updating the pointer manually). It has a number of advantages - not least of which is that you start with a fresh input every time you call findInt, instead of having the rest of the input buffer that still needs reading (and which was giving rise to "no,no,no,great!" - as you would keep reading the bad input until you got to the newline, and accept that...)
#include<stdio.h>
#include <ctype.h>
unsigned int findInt();
int main() {
findInt();
}
unsigned int findInt() {
char input;
char buf[100];
char *temp;
long num = 0;
int b = 0;
printf("please enter an int:\n");
fgets(buf, 100, stdin);
temp = buf;
do {
sscanf(temp++, "%c", &input);
if (isdigit(input)){
num = num*10+input-'0';
b = 1;
}
else if (input == '\n')
{
b = 1;
break;
}
else {
b = 0;
break;
}
} while(input != '\n');
if (b == 1)
printf("Great! %d is an integer!\n", num);
else{
printf("Not an int \n");
findInt();
}
return 0;
}
In the else branch - i.e. not a digit or a newline - you set b to 0. Now if a digit DOES follow you reset that to 1.
You'll probably want to break or somehow record the permanent failure instead of just continuing.
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
void findInt(){
int x;
bool ok;
do{
char buff[32], *endp;
long long num;
ok = true;//start true(OK)
printf("Enter a number: ");
fgets(buff, sizeof(buff), stdin);
//strtoll : C99
x=(int)(num=strtoll(buff, &endp, 0));//0: number literal of C. 10 : decimal number.
if(*endp != '\n'){
if(*endp == '\0'){
printf("Too large!\n");//buffer over
while('\n'!=getchar());
} else {
printf("Character that can't be interpreted as a number has been entered.\n");
printf("%s", buff);
printf("%*s^\n", (int)(endp - buff), "");
}
ok = false;
} else if(num > INT_MAX){
printf("Too large!\n");
ok = false;
} else if(num < INT_MIN){
printf("Too small!\n");
ok = false;
}
}while(!ok);
}
,

Resources