why cant my printf pick up my user input? - c

I have this
void main(int argnum, char** input)
{
int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0, n=0, o=0, p=0;
printf("Enter the numbers from 1 to 16, in any order(separate by comma) : ");
scanf_s("%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n, &o, &p);
printf("%d " "%d " "%d " "%d \n", a, b, c, d);
printf("%d " "%d " "%d " "%d \n", e, f, g, h);
printf("%d " "%d " "%d " "%d \n", i, j, k, l);
printf("%d " "%d " "%d " "%d \n", m, n, o, p);
}
my display is
a 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
for some reason my variables other than a, arent being caught.. whats wrong?

(separate by comma)
If you're separating the numbers with commas, the second and subsequent scans will fails, because there's no way to turn a comma into an integer.
Scanning for an %d integer will:
skip white space (which does not include commas); then
scan as many characters as needed to populate an integer, up until the first character that's not part of an integer.
If the first non-whitespace character is a comma, that's not suitable for an integer. Easiest solution is probably just to tell user to separate with spaces rather than commas.
Let's begin with a cut-down version that exhibits the same behaviour (and with single strings rather than those disparate ones):
#include <stdio.h>
int main(void) {
int a=0, b=0;
printf("Enter numbers from 1 to 2 in any order (separate by comma): ");
scanf("%d %d", &a, &b);
printf("%d %d\n", a, b);
}
Entering 1,2 for that gives you 1 0 as per your code. Entering 1 2 (space-separated rather than comma) gives you 1 2, which is what you're after.
Of course, if you need the commas, you can put that into the format string as well:
scanf("%d ,%d", &a, &b);
This will:
%d - skip optional whitespace and read integer;
<space>: skip whitespace before comma;
,: skip comma;
%d - skip optional whitespace and read integer.
Of course, it goes without saying that you should always check things that can fail, if that failure can affect you adversely:
if (scanf("%d %d", &a, &b) != 2) { // expect two things scanned
fprintf(stderr, "Something went horribly wrong!\n");
exit(1);
}

#include<stdio.h>
void main(int argnum, char** input)
{
int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0, n=0, o=0, p=0;
printf("Enter the numbers from 1 to 16, in any order(separate by comma) : ");
scanf("%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n, &o, &p);
printf("%d " "%d " "%d " "%d \n", a, b, c, d);
printf("%d " "%d " "%d " "%d \n", e, f, g, h);
printf("%d " "%d " "%d " "%d \n", i, j, k, l);
printf("%d " "%d " "%d " "%d \n", m, n, o, p);
}
after taking each value from the keyboard please press enter button it will work with this code

Related

Reading from file does not work as expected with fscanf

I have this file:
0 -> 1:50 2:30 3:10
1 ->
2 -> 0:10 3:20
3 -> 1:20 2:10 3:30
And I want to extract all the numbers from the file with this code:
int a, b, c;
while (fscanf(fp, "%d ->", &a) == 1) {
printf("%d ->", a);
while (fscanf(fp, " %d:%d", &b, &c) == 2) {
printf(" %d:%d", b, c);
}
printf("\n");
}
The idea is that on the first loop it will scan the first number followed by space followed by -> and then the inner loop will scan the following sequence of " %d:%d" until the end of line which will then make the outer loop return 1 because it could successfully read "%d ->".
Ouput of code:
0 -> 1:50 2:30 3:10
It seems like it doesn't work as expected, and the outer while loop exits when it tried to read the number 1 from the second line(but it should?). I just find this weird because I've done something similar recently and it was working fine.
After removing the loops and replacing with this code I get the correct results so it had something to do with my loops:
fscanf(fp, "%d ->", &a);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, "%d ->", &a);
fscanf(fp, "%d ->", &a);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, "%d ->", &a);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
An iteration of while (fscanf(fp, " %d:%d", &b, &c) == 2) { is consuming the "\n1" in "\n1 ->". This fouls the following fscanf(fp, "%d ->", &a), which starts at " ->" and stops the loop.
Code needs to detect the '\n'. format directives/specifiers like " " and "%d" consume end-of-line without reporting that.
Direct solution: As the file is lines of data, then read lines of text with fgets().
Use "%n" to record offset of scan.
// Generous maximum line size
#define LINE_N 1024
char line[LINE_N];
while (fgets(line, sizeof line, fp)) {
int a;
int n = 0;
// v--- space needed to consume trailing whitespace
sscanf(line, "%d -> %n", &a, &n) == 1) {
if (n == 0) {
fprintf(stderr, "Invalid line '%s'\n", line);
continue;
}
printf("%d ->", a);
char *p = line + n;
while (*p) {
int b,c;
n = 0;
// v--- space needed to consume trailing whitespace
sscanf(p, "%d:%d %n", &b, &c, &n);
if (n == 0) {
fprintf(stderr, "Invalid rest of line '%s'\n", p);
break;
}
printf(" %d:%d", b, c);
p += n;
}
printf("\n");
}
The algorithm isn't handling the newlines. So it gets stuck and the end of the first line and isn't able to read anything matching n ->.
One way that works reliably for all C RTL implementations is to use fgets() to read each whole line (including the newline at the end) and then use sscanf() to parse the string.
I think you just need to fseek one char ( newline ) at the last printf.

How to prompt user to enter multiple values on the same line?

How can we have a user enter three different values on the same line.
Something like this: enter three numbers: 1, 2, 3 but all three will be stored in 3 different variables.
I tried doing something this like this:
printf("Enter three numbers: ");
scanf("%d %d %d", one,two,three);
But this does not work!
Here is the entire code:
#include <stdio.h>
int main(void) {
int one,two,three;
printf("Enter three numbers: ");
scanf("%d %d %d", &one,&two,&three);
printf("%d %d %d \n", one,two,three);
}
I tried entering 1, 2, 3 and I got this: 1 134513881 -1218503675
scanf("%d %d %d", &one, &two, &three);
You were close, but scanf doesn't care about the value of those variables (which is great, because they're most likely not initialized), it cares about their addresses so it can dereference them and write inside.
If the number are to be separated by commas as in the example, the commas need to be part of the format string. Check the return of scanf to confirm three fields were input.
#include <stdio.h>
int main(void) {
int one = 0,two = 0,three = 0, ch = 0;
printf("Enter three numbers separated by commas: ");
while ( ( scanf("%d ,%d ,%d", &one, &two, &three)) != 3) {
printf("Please enter three numbers separated by commas: ");
while ( ( ch = getchar ( )) != '\n' && ch != EOF) {
//clear input buffer
}
}
printf("%d %d %d \n", one,two,three);
return 0;
}
As commented by #M.M, the scanf format "%d ,%d ,%d" with a space before the comma will allow the commas to be preceded by any amount of whitespace (including none) to allow input of 1,2,3 or 1 , 2,3. %d also skips any leading whitespace.

Crash after first scanf novice c programming

My code ( gist link )
//pre-processor directives
#include <stdio.h>
#include <math.h>
//Main function
int main() {
int month, day, year, lowest_temp, highest_temp;
float temp;
char fname[30];
FILE * ifp;
printf("Tell me about your dragon's preferred temperature for flying: What is the coldest temperature they can fly in?\n");
scanf("%d", lowest_temp);
printf("What is the hottest temperature they can fly in?\n");
scanf("%d", highest_temp);
printf("Please enter the name of the weather data file for Dragon Island.\n");
scanf("%s", fname);
ifp = fopen(fname, "r");
fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");
return 0;
}
Crashes after first scanf no errors
If you can tell me what the error is I'd appreciate it. Also if you can help me with the next steps in my problem that would be awesome as well.
My assignment
https://gist.github.com/anonymous/e9292f14b2f8f71501ea/88e9e980632871f1f1dedf7589bb518f1bba43e8
scanf("%d", lowest_temp);
...
scanf("%d", highest_temp);
%d require address of int argument you pass it just int variable . Pass their address -
scanf("%d",&lowest_temp);
...
scanf("%d",&highest_temp);
And these both statements-
fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");
should be-
fscanf(ifp, "%d %d %d %f", &month &day &year &temp);
printf("%d %d %d %f", month ,day,year ,temp);

C, Reading double values from text file

Hi just getting some weird outputs from trying to read the inputs as double values in C. This issue does not occur when the inputs are integers is there anyway to make it work with double?
#include "stdafx.h"
int main(void)
{
double a, b, c, d, i;
FILE *inp;
inp = fopen("C:\\Users\\student\\Documents\\Visual Studio2012\\Projects\\ConsoleApplication3\\test.txt", "r");
i = fscanf(inp, "%f %f %f %f", &a, &b, &c, &d);
while (i != EOF)
{
printf("a = %f & %d \n", a, i);
printf("b = %f & %d \n", b, i);
printf("c = %f & %d \n", c, i);
printf("d = %f & %d \n", d, i);
printf("%d \n", EOF);
i = fscanf(inp, "%f %f %f %f", &a, &b, &c, &d);
}
fclose(inp);
return 0;
}
Figured it out, the %f in fscanf should be %lf
Change the specifier to %lf in fsacnf and printf statements. Like this -
fscanf(inp,"%lf %lf %lf %lf", &a, &b, &c, &d);
Other problems -
1.Also i is declared as double but in printf you print it with specifier %d ,so you pass wrong argument -
printf("a = %f & %d \n", a, i); // similar in all printf's
So according to me declare i as int and then print it.
2.Also you should always check return of fopen so check it.

sscanf reading a string within a string

I have a small problem that I can't seem to fix. Say I have a string,
buffer = "1 1 X ./simple E"
And I want to extract the 2 ints, 2 chars and the filename,
sscanf(buffer, "%d %d %c %s %c, &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
I don't get back quite what I expect. I get "11 1 X (null)". Any help appreciated.
You are declaring char *d, which will fail because it has no valid place to point. Use an array that has enough space will do:
#include <stdio.h>
#include <string.h>
int main()
{
int a, b;
char c, e;
char d[20];
char buffer[] = "1 1 X ./simple E";
sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
}
OUtput: 1 1 X ./simple E
c and e can be int's or chars. Note the overflow issues with d[100].
int a, b, c, e;
char d[100];
sscanf(buffer, "%d %d %c %s %c, &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
#include <cstdio>
#include <cstdlib>
int main() {
char buffer[] = "1 1 X ./simple E", c, d[10], e;
int a, b;
//sscanf(buffer, "%d %d %c %*[./]%s %c", &a, &b, &c, d, &e); //To ignore "./"
sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, d, &e); //Don't ignore "./"
printf("%d %d %c %s %c\n", a, b, c, d, e);
return 0;
}
Output:
1 1 X ./simple E
No blank space delimiter need in the sscanf function argument.
sscanf(buffer, "%d%d%c%s%c", &a, &b, &c, d, &e);
%d is blank separated when when reading buffer, and there should not be a space between %c and %s, cause it swallow the space, leaving the buffer with no separator between char and string.

Resources