I want to make a program in C, that calculates GPS coordinates do degrees. However, I have a trouble reading and input.
As an example, input will always be in this format NUMBER CHAR NUMBER.NUMBER (49 E 54.2333 as an example).
I have tried this:
int d, m, s, f;
char xy;
printf("enter coordinates: ");
scanf("%d %c %d.%d", &d, &xy, &s, &f);
but this seems to not work
Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
I did put scanf_s and got "Debug Assertion failed" error
Related
I apologies in advance if there is a stupid error in this code, but I can't seem to trouble shoot it. My problem is this, I compile with GCC-8 (installed on Mac via home-brew), then execute in the terminal. When using int do define variables s & a, I get zeros as output using the print statements below. If I declare s & a variables as double I still get zeros for the first two print statements, and 1024 for the last print statement. I'm just lost as to what is going on. Appreciate any help!
/* square code */
#include <stdio.h>
int main() {
int s, a;
printf("enter the length of your square \n");
scanf("%f", &s);
a= s * s;
printf("the area of your square is %f cm using f placeholder \n", a);
printf("the area of your square is %lf cm uning fl placeholder\n", a);
printf("the area of your square is %d cm using d placeholder \n", a);
return(0);
}
int s;
scanf("%f", &s);
If s is an int then you need to scan it in like an int using "%d" instead of "%f". What you are doing is undefined behavior, both in scanf() and printf(). An integer needs to be scanned and printed as an integer.
I think the misunderstanding is that you treat the conversion specifiers %d and %f as defining only the output formatting, regardless of the type of value passed.
Actually each conversion specifier is tightly coupled with a particular type of argument it will accept; For example, %d is defined for integral types only, and all other types like floating point or string used together with %d will yield undefined behaviour (cf, for example, cpp-reference of printf for a more detailed explanation).
So double f=1.2;printf("%d",f) will not format a floating point value 1.2 as integral (someone might expect 1 being the output); It rather yields undefined behaviour, having any output (even no output), a crash, a... whatever.
Same applies to scanf-format specifiers, of course.
So if data type is int, use %d; if data type is double, use %f.
In your code you use s as an int, and when you use scanf you are using %f instead of %d. That's the reason why you are getting the first two outputs as a zero.
As for the third output, you will get the expected value, because in that case you are correctly using %d. See the code below:
int s;
printf("enter the value");
scanf("%d", &s);
printf("the area of square is=", s*s);
Hey guys thanks so much for the help. Before checking back this morning I tried one more thing, and alas it was a stupid error.
When declaring "double s,a", I realized my scanf() function was using scanf("%f",s) as opposed to the correct scanf("%lf", s)".
Also thanks for the help for use of %d when using "int s,a".
Here in this code, when I execute it, it sometimes display correct value of nT and nF, sometimes it display correct nF but incorrect nT(i.e 0).
Why is it so??
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned char nT,nF;
puts("Enter No. of Testcases & Faults");
scanf(" %hhu %hhu",&nT,&nF);
printf("\n %hhu %hhu",nT,nF);
}
You need to use %hhu as the format specifier for an unsigned char. (Obscure I know: one for the pub quiz.) Also, you might want to introduce some spaces between your formatters:
int read = scanf("%hhu %hhu %s", &nT, &nF, extension);
Currently the behaviour of your program is undefined.
Prior to C99 you're pretty much at the mercy of your compiler.
Further notes:
Always check the return value of scanf which gives you useful information about the number of inputs that are read successfully.
extension is only good for 4 characters plus the nul-terminator.
This question already exists:
scanf to read multiple data types from one input
Closed 8 years ago.
I am trying to read in an equation in the form of mx+b=y where m, b, and y are integers and x is a character. How do I read in both integers and characters and check that the user inputs the data in that exact form. That is, I somehow need to check that the user inputs a '+' sign an '=' sign and uses the correct variable. eg) I want it to accept mx+b=y but not mX+b=y or another example: accept mx+b=y but not mx-b=y. And I also need it to ignore any additional input after the equation.
I've tried using something like:
scanf(" %d %c %c %d %c %d", &m, &x, &sign, &b, &equal_sign, &y);
Using this format it reads in the first number correctly but skips putting the next character into the variable x and instead puts it in sign.
Sorry for the lengthy wording but I'd appreciate any help and just let me know if I need to clarify anything. Thanks.
I would read the entire line using getline(3) or perhaps fgets(3) then manually parse the buffer containing the line, using strtol(3) with a given end pointer, or sscanf(3) (explicitly testing the returned count, and perhaps using the %n format specifier).
You might want to parse an expression into an AST, e.g. using some recursive descent parsing technique. See also the infix-calc example of GNU bison
If you are sure that your input is exactly of the form a x + b = c (which is unnatural for 3x+-5=8, since you want to type just 3x-5=8) you could use a mix of strtol. See also strncmp(3) and strtok(3)
PS. Follow all the links I am giving you here. They are all relevant!
Get the equation in string format(buffer) and parse this buffer using the sscanf function. By using scanf function there may be a problem of flush kind of things
when you got input prompt , you should try to input one value than tab than another value than tab..and so on .until you entered all values.
scanf(" %d %c %c %d %c %d", &m, &x, &sign, &b, &equal_sign, &y);
first value you entered will be assign to m,
after press tab
second value will be assign to x
press tab..
do this until you entered all values..
When I try to use scanf
int main() {
int x1,x2,x3,y1,y2,y3;
printf("Enter 3 pairs of positive integers separated by spaces:\n");
scanf("%u %u %u %u %u %u", &x1, &y1, &x2, &y2, &x3, &y3);
I get the program running, like for an input.
Then I put the input, but it prints the "enter 3 pairs..." and does nothing
why is that?
%u is unsigned integer. %d or %i is signed integers. Please take care of these quirks and gotchas in C. Be careful to oblige to correct format specifiers.
Maybe you have to enter values; it is the goal of scanf.
By the way, your program contains an undefined behavior : %u mismatchs with int pointers. Use rather %d/%i format in printf. An other solution is to declare your variables as unsigned int type, to match with the printf format. Moreover, a part of your source code is missing.
You might find it more convenient to use fgets instead of scanf. The eclipse terminal is a little funky.
Refer to this post
I'm trying to match lines with a format like "point %d %d". So I only need to two those two integers, then the "point" is hard-coded in the format string. As I understand reading Linux man pages of scanf, this should work correctly.
The next code, the way I want to use, the first call to scanf works, but the next calls scanf return with an error code and never take more numbers from the stdin (scanf doesn't block waiting for more input from stdin):
for (;;)
{
scanf("point %d %d", &x, &y);
printf("=> point %d %d\n", x, y);
}
In this way, everything work as expected:
int x, y;
char s[10];
for (;;)
{
scanf("%s %d %d", s, &x, &y);
printf("=> point %d %d\n", x, y);
}
Any suggestion about what could I am misunderstanding?
Thanks.
There's still unconsumed data such as end-of-line characters in stdin that make the upcoming scans to stop with a non-match. In the second version this end-of-line data gets consumed by the %s.
I suggest you fgets to a buffer first and then sscanf it. And do check your return values.
My guess is that you are not giving it proper input. For example this input will not work:
4 5
This should work:
point 4 5
You didn't mention the error code, but it is probably saying that you didn't follow the format correctly (i.e. put in point before the numbers).
As a good programming practice you should flush the standard input before taking inputs from user.