um.. i have a question when i study standard input / output file system in C lang.
what's difference '$%f' and '%f' ??
int main()
{
int ctr;
struct bookInfo books[3];
for (ctr = 0; ctr < 3; ctr++)
{
printf(" what is book #%d? \n", (ctr + 1));
gets(books[ctr].title);
puts("who is author ? ");
gets(books[ctr].author);
puts("how much price this book?");
scanf(" $%f", &books[ctr].price);
}
return 0;
}
%f is just normal float, and the $ in the last scanf, is just a symbol: dollar symbol. There is actually no character such as $%f. Since the last scanf requires price of the book, you should put $ before it (e.g. $20 equals 20 dollars).
scanf(" $%f", &books[ctr].price); scans from stdin
0 or more white-spaces
a single '$'. If not, scanning stops anscanf() returns 0 (or maybe EOF).
If input text then matches a floating point number, it is saved in books[ctr].price.
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 9 months ago.
I'm trying to read distance between two nodes in a Graph and store it in an array but the loop doesn't work as expected. It suddenly stops.
Output:
Edge Number: 4
Enter distance between two nodes, Example: A B 10
A C 3
A B 2
C B 2
...Program finished with exit code 0
Press ENTER to exit console.
For example, when edgeNumber is 4, it stops at 3. Here's my code. Thanks in advance.
Code:
#include <stdio.h>
#define S 50
int main(){
int dist[S][S], edgeNumber, i, temp;
char node1, node2;
printf("Edge Number: ");
scanf("%d", &edgeNumber);
printf("Enter distance between two nodes, Example: A B 10 \n");
for(i = 0; i < edgeNumber; i++){
scanf("%c %c %d", &node1, &node2, &temp);
dist[((int)node1) - 65][((int)node2) - 65] = temp;
dist[((int)node2) - 65][((int)node1) - 65] = temp;
}
return 0;
}
Just rewrite this call of scanf
scanf("%c %c %d", &node1, &node2, &temp);
^^^
like
scanf( " %c %c %d", &node1, &node2, &temp);
^^^
The leading space in the format string allows to skip white space characters in the input buffer including the new line character '\n'.
Also it is a bad idea to use magic numbers like 65. And you should check entered characters that they are letters and convert them to upper case. Otherwise your code is very unsafe. Also you should test the result of calls of scanf.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 9 months ago.
I'm trying to read distance between two nodes in a Graph and store it in an array but the loop doesn't work as expected. It suddenly stops.
Output:
Edge Number: 4
Enter distance between two nodes, Example: A B 10
A C 3
A B 2
C B 2
...Program finished with exit code 0
Press ENTER to exit console.
For example, when edgeNumber is 4, it stops at 3. Here's my code. Thanks in advance.
Code:
#include <stdio.h>
#define S 50
int main(){
int dist[S][S], edgeNumber, i, temp;
char node1, node2;
printf("Edge Number: ");
scanf("%d", &edgeNumber);
printf("Enter distance between two nodes, Example: A B 10 \n");
for(i = 0; i < edgeNumber; i++){
scanf("%c %c %d", &node1, &node2, &temp);
dist[((int)node1) - 65][((int)node2) - 65] = temp;
dist[((int)node2) - 65][((int)node1) - 65] = temp;
}
return 0;
}
Just rewrite this call of scanf
scanf("%c %c %d", &node1, &node2, &temp);
^^^
like
scanf( " %c %c %d", &node1, &node2, &temp);
^^^
The leading space in the format string allows to skip white space characters in the input buffer including the new line character '\n'.
Also it is a bad idea to use magic numbers like 65. And you should check entered characters that they are letters and convert them to upper case. Otherwise your code is very unsafe. Also you should test the result of calls of scanf.
I'm new to C programming. I was trying to write a program that accepts an integer from user and displays its multiplication table up to 10 multiples.
This is my program:
#include <stdio.h>
int main ()
{
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf(" %i ", &number);
while (count <=10)
{
sum = number * count;
printf("%i x %i = %i\n", number, count, sum);
count += 1;
}
return 0;
}
Compilation successfully completes, but when I execute the output file, nothing happens, the terminal is stuck at nothing, i've to press ctrl+c to get out..
This is due to the spaces used in your scanf command.
If you replace that with
scanf("%i", &number);
you get an instant response.
With your scanf format " %i ", the scanf function will read (and skip) possible leading spaces because of your leading space in the format.
Then it will read the integer.
Then, due to the trailing space, it will read and discard space until it find a non-space input.
Since there's no non-space input afterward, then scanf will block until you give some non-space input.
Solve simply by not having any spaces in the format. Or by entering some extra dummy input (followed by Enter).
The problem resides with the scanf.
Just replace
scanf(" %i ", &number);
with:
scanf("%i", &number);
and it will work.
#include <stdio.h>
int main (){
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf("%d", &number);
while (count <=10){
sum = number * count;
printf("%d * %d = %d\n", number, count, sum);
count += 1;
}
return 0;
}
Note: You can use both %d or %i where %d specifies signed decimal integer while %i specifies integer.
Problem: The problem of your code was using a whitespace before %i.
Wrong:
scanf(" %i ", &number); //Wrong
Right:
scanf("%i", &number); //Right.
This is a simple program to compute ages of people in the room.
I am at the very initial stage, and now I see that I do not know which variables (I mean variables that I declare before scanf and then placeholders within scanf) to use for scanf; how to choose and apply a correct variable. Is there a resource that could explain in plain English these issues?
Here is the program:
// Ages people by a year. Arrays
#include <stdio.h>
int main (void)
{
// determine number of people
int n;
do
{
printf("Number of people in room: ");
scanf ("%i", &n);
}
while (n<1); // get the number of people in the room, pass through user
// again and again until the user gives a positive integer
// declare array in which to store everyone's age
int ages[n];
int i;
for (i = 0; i < n; i++)
{
printf("Age of person #%i: ", i + 1); // person number 1, person number 2, etc
scanf ("%d", ages[i]); // store the age in the i-th part of the array ages
}
// report everyone's age a year hence
printf("Time passes...\n\n");
for (i = 0; i < n; i++)
{
printf(" A year from now person #%i will be %i years old.\n", i + 1, ages[i] + 1);
// we add 1 year to previous age
}
}
scanf("%d") expects an address as an argument. Therefore, replace
scanf ("%d", ages[i]);
with
scanf ("%d", ages + i);
(or &ages[i] but that's personal preference.)
scanf expects pointer to some variable in order to change it's value - otherwise it will get some copy that won't affect the real variable.
this line :
scanf ("%d", ages[i]);
dereference ages and returns an integer, not a pointer to an integer.
change it to be
scanf ("%d", &ages[i]);
the & will extract the memory address of ages[i] and pass it as a pointer to scanf
what is mistake ? i cant find any mistake in my code but it does not take input properly and it also displays some garbage values.
Thanks:
int main()
{
struct book
{
char name;
int price;
int pages;
};
struct book b[5];
int i;
for (i = 0; i < 5; i++)
{
printf("enter name price pages\n");
scanf("%c", &b[i].name);
scanf("%d", &b[i].price);
scanf("%d", &b[i].pages);
}
for (i = 0; i < 5; i++)
printf("%c %d %d\n", b[i].name, b[i].price, b[i].pages);
return 0;
}
Most books have a name that is longer than a single letter.
You need to adjust the structure accordingly:
enum { MAX_BOOKNAMELEN = 32 }; // Probably too short
struct book
{
char name[MAX_BOOKNAMELEN];
int price;
int pages;
};
You also need to adjust the input:
if (scanf("%31[^\n]", b[i].name) != 1)
...report error; do not use this book entry...
The '31' is magicked out of thin air; it is one less than MAX_BOOKNAMELEN. Note that book titles frequently contain spaces; thus %s which skips leading spaces and stops at the first space after one or more non-space characters is not appropriate for reading titles. One way to create the format string is via sprintf() — this will adapt if MAX_BOOKNAMELEN changes size:
char name_fmt[16];
snprintf(name_fmt, sizeof(name_fmt), "%%%d[^\n]", MAX_BOOKNAMELEN-1);
if (scanf(name_fmt, b[i].name) != 1)
...error...
A more thorough revision would probably use fgets() and sscanf() or other tools instead of calling scanf() directly.
this line
scanf("%c",&b[i].name);
should be
scanf(" %c",&b[i].name);
See How to do scanf for single char in C
Since I've been informed to specifically answer the question (sorry, Im new here), the problem lies in the %c picking up whitespace and/or newline characters. The easiest way to prevent this is put a " " in front of it.
#include <stdio.h>
int main()
{
struct book
{
char name;
int price;
int pages;
};
struct book b[5];
int i;
for(i=0;i<5;i++)
{
printf("enter name price pages\n");
scanf(" %c %d %d",&b[i].name, &b[i].price, &b[i].pages); //here
//%c was grabbing whitespace and/or newline causing the problem.
}
for(i=0;i<5;i++)
printf("Name: %c Price: %d Pages: %d\n",b[i].name,b[i].price,b[i].pages);
return 0;
}
Output:
enter name price pages
a 1 2
enter name price pages
b 3 4
enter name price pages
c 5 6
enter name price pages
d 7 8
enter name price pages
e 9 10
Name: a Price: 1 Pages: 2
Name: b Price: 3 Pages: 4
Name: c Price: 5 Pages: 6
Name: d Price: 7 Pages: 8
Name: e Price: 9 Pages: 10
What happens is that the last scanf("%d"), reads the number and let a '\n' at the buffer, so next time you call scanf("%c"), it will read the '\n' in the buffer, and not the new character.
One possible solution to use scanf, is to tell scanf to read everything in the line, and discard it:
scanf("%c%*[^\n]\n", &b[i].name);
scanf("%d%*[^\n]\n", &b[i].price);
scanf("%d%*[^\n]\n", &b[i].pages);
This way, scanf will block until you press enter, and will read the '\n'.
try it like this :
scanf(" %c %d %d",&b[i].name, &b[i].price, &b[i].pages);
while(getchar()!='\n');