scanf_s reading characters what am i doing wrong? - c

{
printf("Please give a random positive number for X: \n");
scanf_s("%i", &X);
char ch = getchar();
char Gender;
if (isdigit)
{
printf("Hay um... sorry what my i call you?");
scanf_s("%i", Gender);
printf("%lf \n" Gender ", if you may, please reread what i said, i asked you to give me a RANDOM NUMBER. \ncan you please explain to me why you gave me out of 1 through infinite numbers, a random letter? \n \n ");
};
my problem is with ["%lf \n" Gender] saying that its expecting a [ ) ] can someone please explain what i am doing wrong?

A few issues here:
scanf_s takes a pointer to where you want to store the read value, so it should be scanf_s("%i", &Gender);
In C, you can't just insert a char into a string. I'm not sure exactly what you want from this, since %lf is a really weird format for printing a char, but that's what I'm assuming you want. In that case, you would want to move Gender to the end: printf("%lf \n...letter? \n \n", Gender);. This will insert Gender in the position of %lf.

Related

Errors about scanf and printf expected types

My issue is coming from the %c name input, I am getting an error that it is expecting type char * but has type char * [15] for the scanf function. I am also getting an error in the printf where the %c expects int but has type char *. I am still quite new at this so if it could be explained as simply as possible that would be amazing.
#include <stdio.h>
struct Student {
int StudentID;
char Name[15];
float Mark1;
float Mark2;
float Mark3;
} a;
int main() {
float ave;
printf("Please input Student's ID \n");
scanf("%d", &a.StudentID);
printf("Please input Student's name. \n");
scanf(" %c", &a.Name);
printf("Input Mark 1. \n");
scanf("%f", &a.Mark1);
printf("Input Mark 2. \n");
scanf("%f", &a.Mark2);
printf("Input Mark 3. \n");
scanf("%f", &a.Mark3);
ave = (a.Mark1 + a.Mark2 + a.Mark3) / 3;
printf("Student Detail\nStudent ID: %d\nName: %c\nMark 1: %.2f\n Mark 2: %.2f\n Mark 3: %.2f\nAverage: %.2f\n",
a.StudentID, a.Name, a.Mark1, a.Mark2, a.Mark3, ave);
return 0;
}
Your problem is related to the difference between an array of chars and a single char.
The %c format only reads in one character at a time.
If you wish to read a string of characters use %s and it will read until a whitespace. (Please make sure you don't try to read a name more than 14 characters long into your 15 character buffer)
In more depth, your char Name[15] is actually a pointer to a series of chars in memory. You are accidentally trying to change to pointer itself, instead of the chars that it points to. This is why the compiler expects a char * .
Instead if you truly meant to only read one char you could use
scanf(" %c", &a.Name[0]);
to place the character in the first block of the Name array.
If this is too complicated don't worry, it will all come eventually :)
For now I think %s will suffice.
You can use %14s to be extra safe.
Also don't forget to use %s in the final printf as well
In your scanf() call, %c tells scanf() to accept a single character. But your argument is a character array. C is a low-level language; it's not smart enough to realize you wanted a string (char array) as input. You have to tell it by using %s instead of %c.

why can't I input a character with scanf

why is this not working? im new to C...
the scanf function works just fine with other data types, its just the char thats not giving me the option to input a character
char grade;
printf("Enter your grade: ");
scanf("%c", &grade);
printf("Your grade is %c", grade);
It seems before entering the character there are inputs of other objects in your original program.
In this case you need to write
scanf(" %c", &grade);
Pay attention to the blank before the conversion specifier %c. It allows to skip white space characters.

Segmentation fault when handling characters

Fair warning, I am quite new to C, and am still very much a beginner with the language. This problem has stuck with me for a while and I thought someone here might be able to help.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old \n", age);
double gpa;
printf("Enter your GPA: ");
scanf("%lf", &gpa);
printf("You're GPA is %lf \n", gpa);
char name[20];
printf("Please enter your name: ");
scanf("%s", &name);
printf("Your name is %s \n", name);
char grade;
printf("Enter your grade: ");
scanf("%s", &grade);
printf("Your grade it %s \n ", grade);
return 0;
}
The problem lies in the 'grade' section, everything else works perfectly.
The output of the program with user input is
Enter your age: 3
You are 3 years old
Enter your GPA: 4.5
You're GPA is 4.500000
Please enter your name: Test
Your name is Test
Enter your grade: B
Segmentation fault (core dumped)
Hopefully someone here can help. All the best!
Input for getting the input of grade would be
scanf(" %c",&grade);
The issue arises because the grade is a char variable, not a char array.
char -> %c
char array(aka String in C) -> %s
The reason you get segmentation fault is that you are scanning the input B as a string instead of a character (it is equivalent to {'B','\0'}, so it is not just B) and you are trying to assign this string into a char .
If you want to get a character as input (seems like you want it this way) you must do :
scanf ("%c",&grade);
The %c format specifier is for scanning chars, and &grade is basically the address of grade.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
However, if you want to get a string as input (i.e. character array in C) , you must define grade like this :
char grade [n];
(n is a number telling the size of array)
and the scanf line must be :
scanf ("%s",grade);
The %s format specifier is for getting strings (i.e. character arrays in C). The reason why we used grade without an address-of (&) operator is related with pointers. Because grade is already a pointer; we need its value (i.e. the address of the first character of array), not address. Check this link for better understanding. Also check this post after ensuring you've understood the logic of pointers.
Note : This is a self study question. Avoid getting downvoted as possible.

C : when asking for 2 inputs from a user the 2nd question will not prompt an input to be stored in a variable

If I try grade or fullname one at a time I will get the expected result
"Enter your Grade: "
"Your grade is x"
"Enter your full name:"
"Your full name is xxxx xxxx"
If I run below the print out is
Enter your Grade:2
Your grade is 2
Enter your full name: Your full name is
I can't figure out why I am not been prompted for a second input especially as I know it works when tried on its own */
int main()
{
char grade;
printf("Enter your Grade: ");
scanf("%c", &grade);
printf("Your grade is %c \n", grade);
char fullName[20];
printf("Enter your full name: ");
fgets(fullName, 20, stdin); /*2nd argument specify limits of inputs*, 3rd means standard input ie command console */
printf("Your full name is %s \n", fullName);
return 0;
}
scanf("%c"); buffer problem, %c will only eat one character, so or \n will stay in the buffer for the next one to read.
Try this
#include <stdio.h>
#include <stdlib.h>
int main()
{
char grade;
printf("Enter your Grade: ");
scanf("%c", &grade);
getchar(); // here
printf("Your grade is %c \n", grade);
char fullName[20];
printf("Enter your full name: ");
fgets(fullName, 20, stdin); /*2nd argument specify limits of inputs*, 3rd means standard input ie command console */
printf("Your full name is %s \n", fullName);
return 0;
}
use getchar(); to eat the extra character in the buffer.
Don't mix scanf() with fgets() - in this case, the newline present in the buffer, left untouched by scanf() will be fed to fget() and it won't "ask" for any new input.
Better to use fgets() in all the user-inputs.
The problem lies in this line scanf("%c", &grade); Whenever you use scanf() always keep in mind that the enter key will be stored in your buffer. Since enter key was in your buffer, it went right into fullName when fgets(fullName, 20, stdin); is executed. That's way you got your output:
Enter your Grade:2
Your grade is 2
Enter your full name: Your full name is
You can solve the problem by using getchar(); or getch(); right after scanf(); to capture the Enter key and ensures that fullName get the correct input. Another way to solve it is to use fflush(stdin);. They basically do the same thing, but fflush(stdin); clear Enter key from the buffer. Therefore, fflush(stdin); should be used after scanf(); to clear the unwanted Enter key left by scanf();
This is a long one and have a lot to take in, but I hope this information helps :)))

Why gets() function skips when preceded by scanf("%d")?

I want to make a program which take Roll No and Full name as input and simply display it
My code is . this code skip scaning value of n through gets function. Why this error occur and how to over come this?
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30];
printf("enter your roll no");
scanf("%d",&r);
printf("enter your full name");
gets(n);
printf("roll no is %d ",r);
printf("name is %s ",n);
getch();
}
while the below code scan the first gets value and skips the second one.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30], f[30];
printf("enter your roll no");
scanf("%d",&r);
printf("enter your full name");
gets(n);
printf("enter your full name of your father ");
gets(f);
printf("roll no is %d ",r);
printf("name is %s ",n);
printf("father name is %s ",f);
getch();
}
The code DOES NOT skip scanning the value of 'n'.
I believe that when you run the program, you enter the Roll No and then press the ENTER key on your keyboard.
This is the cause.
As soon as you press the ENTER key, the escape sequence '\n' is saved in the array n. Your gets() command is executing perfectly.
In the second case, the variable 'n' stores the escape sequence and the next variable 'f' takes the string you enter next.
To make your code work just enter your scanf statement like this:-
scanf("%d ",&r);
Notice the space after %d.
Try this code-
#include<stdio.h>
int main(void)
{
int r;
char n[30], f[30];
printf("Enter your roll no");
scanf("%d ",&r); // I have inserted a space after %d
printf("Enter your full name");
gets(n);
printf("Enter your full name of your father ");
gets(f);
printf("\nRoll no is %d ",r);
printf("\nName is %s ",n);
printf("\nFather name is %s ",f);
return 0;
}
TIP:- You must try not to use gets() and puts()
You can read more about it here.
The simple solution for the problem is to add fflush(stdin); between scanf(); and gets();
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30],fn[30];
clrscr();
printf("\nEnter roll ");
scanf("%d",&r);
fflush(stdin);
printf("\nEnter name ");
gets(n);
printf("\nEnter father name ");
gets(fn);
printf("\n\nRoll %d",r);
printf("\nname %s",n);
printf("\nfather name %s",fn);
getch();
}
Using scanf instead of gets will solve your problem:
scanf("%s", n); // Read in your name
Please note that when reading in any string like this you should use safe functions that are passed the length of the string (for example scanf_s from MSDN).
I don't know why it gets skipped but what you could do to avoid any other confusion like fflush(stdin) or fgets etc etc.
Just use gets(string) on the next line. So when it skips the first gets command it goes onto the other one.
Try that
Cheers,
;)
I just had the same problem two hours ago, but to solve this situation easily, all you have to fo is to add a "getchar()" after the "scanf()" and before the "gets()", so that the extra "\n" goes to the "getchar()" and you can type as you want in the next "gets()".
I was also facing the same problem as mentioned above.. so with the help of the answers mentioned here and using hit and trial method, I found that when we press enter after giving input to any variable using scanf(), \n is stored in the next gets() function.. and next time it doesn't take any input from the keyboard.. so to avoid this just use getchar() in between the scanf() nd gets() nd also remember that getchar() takes only 1 character.. so don't give any extra input to scanf() as again this will be stored and will be used in gets() nd the problem will remain the same....
hope this will help..
thank u..

Resources