Find a char that equals an input - c

In the code below I take the input from the user and then I find the digit that was introduced by comparing it to every other digit.
This my code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int i = 0;
int input;
printf("Input a digit: \n");
scanf("%d", &input);
for(; i < 10; i++)
{
if(i == input)
{
printf("Your input is %d\n", i);
break;
}
}
return 0;
}
How do I check for a letter instead? Say the user inputs letter G, and I take that in a loop to compare it to every other character until I find the one it equals to?

You can use pretty much the same code you already have, just scan for a char:
char input;
scanf("%c", &input);
To check the input, use what fits your expectations best. A switch is probably a solid choice:
switch (input) {
case 'G':
printf("G was received\n");
break;
default:
printf("Uninteresting character %c was received\n", input);
}

Related

Trying to find duplicates in a Struct Array in C

I've attempted to find if the user has inputted a product id value that's a duplicate and if so, it just tells them that it's a duplicate value and then returns to the menu in my switch statement.
The actual result i get, is that after "productsfilled == 0", it won't utilise the For Loops to check for the duplicates and productsfilled will remain at 1. I've looked online and this way of finding duplicates tends to work and i have used it previously in my code, so I don't think that could be the issue.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <conio.h>
int productsfilled;
struct ProductData{
int product_id;
char product_name[120];
int price;
};
int quiz_5(){
char ch;
int size, input = 0;
struct ProductData products[20];
while(1){
printf("\nWelcome To The Super Mall's Product List. Please Select An Option:\n1. Add Product\n2. Display Product\n3. Delete Product\n");
fflush(stdin);
switch(getchar()){
case '1':
printf("\nPlease Enter Product ID:");
scanf("%d",&products[productsfilled].product_id);
printf("\nPlease Enter Product Name:");
scanf("%s",&products[productsfilled].product_name);
printf("\nPlease Enter Product Price:");
scanf("%d",&products[productsfilled].price);
printf("Productsfilled: %d",productsfilled);
if(productsfilled == 0){
productsfilled = 1;
break;
}
for(int i = 0; i < productsfilled;i++){
for (int j = i + 1; j < productsfilled;j++){
if(products[i].product_id == products[j].product_id){
printf("\nPlease Use Different Product ID");
break;
}else{
printf("test");
productsfilled += 1;
break;
}
}
}
break;
case '2':
while(1){
for(int i = 0;i < productsfilled;i++){
printf("Product ID: %d Product Name: %s Product Price: %d\n",products[i].product_id,products[i].product_name,products[i].price);
}
printf("Please Press Enter To Continue");
fflush(stdin);
if(getchar() == '\n'){
break;
}
}
case '3':
break;
case '\n':
break;
default:
printf("Please Select An Option:\n1. Add Product\n2. Display Product\n3. Delete Product: ");
}
}
}
int main() {
int input = 1;
printf("Welcome to my assignment. Which quiz do you want to run (please input the number of the quiz e.g. for quiz 1, type 1): \n-Quiz 1\n-Quiz 2\n-Quiz 3\n-Quiz 4\n-Quiz 5\n-Quiz 6\n-Quiz 7\n");
while(input == 1){
fflush(stdin);
switch(getchar()){
case '5':
quiz_5();
break;
case '\n':
printf("Welcome to my assignment. Which quiz do you want to run (please input the number of the quiz e.g. for quiz 1, type 1): \n-Quiz 1\n-Quiz 2\n-Quiz 3\n-Quiz 4\n-Quiz 5\n-Quiz 6\n-Quiz 7\n");
getchar();
default:
printf("Invalid Input\n");
} }
return 0;
}
The problem is that you don't increment productsfilled before you enter the loop...therefore, productsfilled is always 1 less than the actual length of your array which means that you don't compare all elements in the array.
Try your program on 2 inputs, both with the same ID. You'll see that you don't compare anything.
You are wrong when using scanf for string input:
scanf("%s",&products[productsfilled].product_name);
You should not use &, you should use as below:
scanf("%119s",products[productsfilled].product_name);
OT, in main function:
switch(getchar()){
case '5':
...
Because getchar() will return int value, so if you want to access to quiz_5, you have to type 35 (ANSCI code) instead of type 5 when you run your program.
char a = '5';
similar to:
int a = 35;

C coding: Can't seem to understand why code doesn't wait for standard input, but if I run independently it works fine [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 6 years ago.
I will try to explain the issue here.
I have written this code that accepts various types of inputs:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main()
{
int number;
printf("press <ENTER> to continue...");
while( getchar() != '\n' );
char *p, s[100];
int n=0;
printf("enter a number: ");
while (fgets(s, sizeof(s), stdin))
{
n = strtol(s, &p, 10);
if (p == s || *p != '\n')
{
printf("Invalid integer, please try again: ");
}
else
break;
}
printf("You entered: %d\n", n);
printf("Enter an integer between 10 and 20: ");
scanf("%d", &number);
while (1)
{
if (number < 10 || number > 20)
{
printf("Invalid value, 10 < value < 20: ");
scanf("%d", &number);
}
else
{
break;
}
}
printf("You entered: %d\n", number);
//part 3
double decpart;
printf("Enter a floating number num: ");
char buf[100];
int len;
char *endptr;
while (1)
{
fgets(buf,sizeof(buf),stdin);
len = strlen(buf)-1;
// right strip spaces (replace by linefeed like fgets ends the input)
while(len>0)
{
len--;
if (buf[len]==' ')
{
buf[len]='\n';
}
else
{
break;
}
}
double floatnum = strtod(buf,&endptr);
if (endptr[0]!='\n')
{
printf("Invalid floating point number, enter again: ");
}
else
{
int intpart = (int)floatnum;
double decpart = floatnum - intpart;
if (decpart == 0.000000){
printf("Invalid floating point number, enter again: ");
}
else
{
printf("Number entered = %.2f\n", floatnum);
break;
}
}
}
double floatnum1;
printf("Enter a floating point number between 10.00 and 20.00: ");
scanf("%lf", &floatnum1);
while (1)
{
if (floatnum1 < 10.00 || floatnum1 > 20.00)
{
printf("Invalid value, 10.000000 < value < 20.000000: ");
scanf("%lf", &floatnum1);
}
else
{
break;
}
}
printf("You entered: %0.2lf\n", floatnum1);
printf("End of tester program for milestone one!\n");
return 0;
}
Problem occurs on Part 3 of this code. I see on screen Enter a floating number num: and immediately without waiting for user input it prints Invalid floating point number, enter again:
This is not the case if I just run part3(commented here in code as //part3) independently, it just works fine.
Any idea, why that is happening?
The reason for this behaviour lies in the usage of scanf followed by fgets
scanf reads a number from standard input, and stops as soon as it encounters a non-digit character, which is the newline in this case.
Next fgets reads a whole line. But now there's still the single newline in the input, which satisfies fgets even though this is only an empty line.
When you skip over whitespace and finally check for a newline, endptr only points to a \0 character. Thus the message
Invalid floating point number, enter again:
To fix this, you must first skip whitespace before reading further with fgets.

how to perfect hangman looping issue in the end C language?

the hang man has a problem please identify it??
the letters when typed the code shows 'not found'and the same letter has to be typed twice to get it accepted? and the chances to guess the letter decreases how to fix it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define WORD_COUNT 3
#define MAX_LENGTH 10
typedef char string[MAX_LENGTH];
void main(void) {
string words[WORD_COUNT] = { "bird","fish","lion","ants","bear","deer","fowl" };
char answer[MAX_LENGTH];
char guess;
int count = -0, index, i, found, choice = -7;
char mysteryWord[MAX_LENGTH];
printf("Welcome to Hangman!\n");
printf("\n\nChoose an option\n"
"1) Easy\n"
"2) Moderate\n"
"3) Hard\n"
"Your choice: ");
scanf("%i", &choice); a biref menu case
switch (choice) {
case 1:
count = 5;
break;
case 2:
count = 2;
break;
case 3:
count = 1;
}
srand(time(NULL));
index = rand() % WORD_COUNT;
strcpy(mysteryWord, words[index]);/*actual comparing */
for (i = 0; i < strlen(mysteryWord); i = i + 1)
{
answer[i] = '-';
}
answer[i] = '\0';
printf("%s \n", answer);
while (1 > 0) {
printf("\n %i guess(es) left\n", count);
printf("Guess a letter:");
scanf("%c\n", &guess);
guess = tolower(guess);
found = 0;
for (i = 0; i < strlen(mysteryWord); i++)
{
if (mysteryWord[i] == guess) {
answer[i] = guess;
found = 1;
}
}
if (found == 0) {
printf("Not found!\n");
--count;
}
if (count == 0) {
printf("Game over\n");
printf("The answer is %s.", mysteryWord);
break;
}
else {
what should be here instead of if(answer==mysteryWord) ?
if (strcmp(answer, mysteryWord) == 0)
{
printf("Yes, it's a %s\n", answer);
break; /* or return */
} else
printf("%s", answer);
}
} end of while loop ?
} end of main ?
Change
scanf("%c\n", &guess);
To
scanf(" %c", &guess);
Note the space before %c. The space discards all blanks like newlines and spaces and the %c will then scan the next non-whitespace character.
In your case,when you input data for any scanf,you enter the data and press the enter key.scanf reads the data entered and leaves the \n(newline character) in the stdin. When you scan a character using %c , scanf reads the \n left out by the previous scanf and thus,does not wait for input.
scanf(" %c", &guess);
Please make sure your scanf() is like above with a space before %c
The purpose of space is it gobbles whitespace and special characters

What is the best way to read a string?

So I'm really confused. I have to write a program in C which is basically an address book, thus I have to get multiple strings from the user (name, ID, phone etc.)
In the beginning I tried using scanf() only but it messed up sometimes with the newline character '\n'. After some googling, I ended up using scanf() for getting single chars or ints (where user answers yes or no questions, or chooses an action from a menu) and fgets() to read the fields of the address book. However, I also had to use fflush(stdin) multiple times after using scanf() which is not recommended here as I have seen. This method worked so far as intended.
So what's the optimal way to read a string from the user? Does fflush(stdin) not offer portability? This is an assignment so I have to think for portability too, since I will execute my code on another computer.
Thank you in advance.
EDIT: So here is what I've got so far. Excuse some words that are written in another language (Albanian). I believe you can understand what's going on.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void regjistrim();
void kerkim();
void modifikim();
void fshirje();
void rradhitje();
void display();
#define EMRI 50
#define MBIEMRI 50
#define ID 20
#define TEL 20
#define EMAIL 25
typedef struct addressbook
{
char emri[EMRI];
char mbiemri[MBIEMRI];
char id[ID];
char tel[TEL];
char email[EMAIL];
} addressbook;
FILE* Addressbook;
int main(void)
{
char input[2];
int choice;
printf("----------------ADDRESS BOOK----------------");
printf("\n\n\t1 - Regjistrimi i ri\n");
printf("\n\t2 - Kerkim\n");
printf("\n\t3 - Modifikim\n");
printf("\n\t4 - Fshirje\n");
printf("\n\t5 - Rradhitje\n");
printf("\n\t6 - Afishim i address book\n");
printf("\n\t0 - Exit\n");
fgets(input, 2, stdin);
sscanf(input, "%d", &choice);
while (choice < 0 || choice > 6)
{
printf("\nShtypni nje numer nga 0 - 6: \n");
fgets(input, 2, stdin);
sscanf(input, "%d", &choice);
}
switch (choice)
{
case 1:
regjistrim();
break;
case 2:
kerkim();
break;
case 3:
modifikim();
break;
case 4:
fshirje();
break;
case 5:
rradhitje();
break;
case 6:
display();
break;
case 0:
exit(0);
break;
}
return 0;
}
//Regjistron nje qytetar ne addressbook
void regjistrim()
{
char answer;
addressbook entry;
do
{
Addressbook = fopen("Addressbook.txt", "a+");
printf("\nShtypni emrin: ");
fgets(entry.emri, EMRI, stdin);
printf("\nShtypni mbiemrin: ");
fgets(entry.mbiemri, MBIEMRI, stdin);
printf("\nShtypni ID-in: ");
fgets(entry.id, ID, stdin);
printf("\nShtypni nr. telefoni: ");
fgets(entry.tel, TEL, stdin);
printf("\nShtypni email-in: ");
fgets(entry.email, EMAIL, stdin);
fprintf(Addressbook, "Emri: %sMbiemri: %sID: %sNr. telefoni: %sEmail: %s\n", entry.emri, entry.mbiemri, entry.id, entry.tel,entry.email);
fclose(Addressbook);
printf("\nShtypni y/Y neqoftese doni te regjistroni person tjeter: ");
fgets(answer, 1, stdin);
}
while(answer == 'y' || answer == 'Y');
}
With scanf, you can clear the newlines, like that. Also, I have included fgets too.
#include<stdio.h>
int main(void)
{
int i, N = 5;
char buffer[N];
printf("Enter %d characters\n", N+1);
scanf("%5s", buffer); /* MUST check comments below on this page for this! */
/* Clear trailing input */
while(getchar() != '\n')
/* discard */ ;
for(i = 0 ; i < 5 ; ++i)
printf("|%c|\n", buffer[i]);
printf("End with scanf\n\n");
/*****************************************************/
printf("Enter %d characters\n", N+1);
fgets(buffer, 5, stdin);
for(i = 0 ; i < 5 ; ++i)
printf("|%c|\n", buffer[i]);
printf("End with fgets\n\n");
return 0;
}
Also, this code demonstrates the limit you can put to every function for the input.
Source
Well "optimal" is maybe a bit subjective but I found
doing a separate function to read the number makes things
a bit easier and avoid scanf if you don't absolutely need it e.g.
int readNumber(int min, int max)
{
char number[32];
do
{
if ( fgets( number, sizeof(number), stdin ) != NULL )
{
// note that if 'number' is not a number atoi returns 0
int n = atoi(number);
if ( n>= min && n <= max )
{
return n;
}
}
printf( "please enter a valid value between %d and %d\n", min, max );
}
while ( 1 );
return -1; // never reached
}

Why does this C loop skip the first attempt to input a letter?

This is a program I am making for a class. It is supposed to read a letter from a file, and then in the game the user tries do guess the letter. with every wrong attempt the program tells you if the actual letter comes before or after your guess in the alphabet.
For some reason when I run it, the loop skips the first attempt in the getLetter function and does not let you input the letter. Why is this?
#include <stdio.h>
#include <ctype.h>
#define MaxGuesses 5
void instructions();
int playGuess (char solution);
char getLetter ();
int compareLetters (char guess, char solution);
int main()
{
int numGames;
int i;
char solution;
char guess;
int result;
FILE *inFile;
inFile=fopen("inputLet.txt","r");
instructions();
scanf("%d", &numGames);
for(i=1; i<=numGames; i++)
{
printf ("\nThis is game %d\n", i);
fscanf(inFile, " %c", &solution);
result = playGuess(solution);
if (result == 1)
printf("You've WON!\n");
else
printf("You've LOST :(\n");
}
//close file
fclose(inFile);
return 0;
}
void instructions ()
{
printf ("This game consists of guessing letters.\nThe user will have up to 5 chances of guessing correctly,\nupon every failed attempt,\na hint will be provided regarding alphabetical position.\n\nEnter the number of games you wish to play (max 4): ");
}
char getLetter()
{
char userGuess;
printf("\nPlease enter your guess: ");
scanf("%c", &userGuess);
userGuess = tolower(userGuess);
return userGuess;
}
int compareLetters(char guess, char solution)
{
if (guess == solution)
return 1;
else if (guess < solution)
{
printf("\nThe letter that you are trying to guess comes before %c", guess);
return 0;
}
else if (guess > solution)
{
printf("\nThe letter that you are trying to guess comes after %c", guess);
return 0;
}
}
int playGuess (char solution)
{
int numGuesses = 0;
int winOrLose = 0;
char guess;
while(numGuesses < MaxGuesses && winOrLose == 0)
{
guess = getLetter();
winOrLose = compareLetters(guess, solution);
numGuesses++;
}
return winOrLose;
}
It may be consuming a character left in the input buffer (possibly a newline or other whitespace character). You could try changing the format string from "%c" to " %c" as you've done elsewhere, which will skip all the whitespace characters in the buffer before trying to read a character.

Resources