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.
Related
I wrote a program which takes three numbers from the user and prints out the maximum number, but when I run the program it is not taking the numbers from the user correctly
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2, num3;
printf ("PROGRAM TO FIND THE BIGGEST NUMBER\n");
printf ("\n");
printf ("enter first number : ");
scanf ("%d ",&num1);
printf ("enter second number : ");
scanf ("%d ",&num2);
printf ("enter third number : ");
scanf ("%d ",&num3);
printf("%d - %d - %d \n",num1,num2,num3);
if (num1>num2 && num1>num3){
printf ("the biggest number is %d",num1);
}
else if (num2>num1 && num2>num3){
printf ("the biggest number is %d",num2);
}
else if (num3>num1 && num3>num2){
printf ("the biggest number is %d",num3);
}
return 0;
}
Remove the space after the %d in all the scanf calls. The space will match any character including the newline. The scanf will keep reading until the match fails. By removing the space the match fails when the newline character is entered and the code continues.
When I run it and try ending the loop I have to enter -1 twice.
Does anyone know why?
I'm a beginner so I don't really know what's wrong with the code.
I need to know what's wrong cause the compiler doesn't tell me the problem
I'm using Codeblocks.
This is my code :
#include <stdio.h>
int main()
{
int grilled_duck = 1; /*initializing grilled duck to 1 portion*/
int salad = 3; /*initializing salad to 3 portions*/
int rice = 4; /*initializing rice to 4 portions*/
int soup = 2; /*initializing soup to 2 portions*/
int order; /*initializing order variable*/
/*printing the menu on the screen and giving codes to every meal*/
printf(" the menu:\n");
printf(" %d portions of grilled duck (order code = 1).\n", grilled_duck);
printf(" %d portions of salad (order code = 2).\n", salad);
printf(" %d portions of rice (order code = 3).\n", rice);
printf(" %d portions of soup (order code = 4).\n", soup);
/*taking the orders from the user input using an event controlled loop
and decreasing the meals portion*/
printf(" what would you like to order (to finish your order enter -1).\n");
while(order!=-1)
{
scanf(" %d\n", &order);
order=order;
if(order==1 && grilled_duck!=0)
{
grilled_duck=grilled_duck-1;
printf("what else?\n");
}
else if(order==2 && salad!=0)
{
salad=salad-1;
printf("what else?\n");
}
else if(order==3 && rice!=0)
{
rice=rice-1;
printf("what else?\n");
}
else if (order==4 && soup!=0)
{
soup=soup-1;
printf("what else?\n");
}
else if(order==-1)
{
printf("thanks for ordering, your meal will be ready soon.\n");
}
else
{
printf("sorry we have any, anything else?\n");
}
}
/*after taking the user orders the program print the updated menu*/
printf("\n the updated menu:\n");
printf(" %d portions of grilled duck.\n", grilled_duck);
printf(" %d portions of salad.\n", salad);
printf(" %d portions of rice.\n", rice);
printf(" %d portions of soup.\n", soup);
return 0;
}
Change this
scanf(" %d\n", &order);
to
scanf("%d", &order);
The trailing whitespace character (\n) in format specifier means it ignores any number of whitespace after scanning an int. You don't need any whitespace character at all in scanf here because %d already ignores any left over whitespace characters.
Also note order is not initialized during the first iteration. Using do {..} while(..); loop is probably better suited.
Also see: Why does everyone say not to use scanf? What should I use instead?
The problem is here:
scanf(" %d\n", &order);
The format string contains a newline after the %d. This causes the newline you enter after inputting a number to be consumed. As a result, scanf is still waiting on input before it returned.
Remove the trailing newline after %d (as well as the leading space). This will allow pressing ENTER to terminate the scanf call.
scanf("%d", &order);
I have written a program to take input of two numbers and either add or subtract the numbers depending the operation specified.
Here is my code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
float i, j, k;
char a;
printf("This is a program to add or subs two number.\n");
printf("Enter the first number : ");
scanf("%f", &i);
printf("Enter the second number : ");
scanf("%f", &j);
printf("Give your choice(+ or -): ");
scanf("%c", &a);
switch(a){
case '+' :
k = i + j;
printf("Sum = %f\n", k);
break;
case '-' :
k = i - j;
printf("Difference = %f\n", k);
break;
default:
printf("Cannot do this operation\n");
}
return 0;
}
This program takes input for the two numbers but skips input for operation and runs the default case. Please help!
(I am using gcc compiler).
The %c conversion specifier won't automatically skip any leading whitespace, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.
One way around the problem is to put a blank space before the conversion specifier in the format string:
scanf(" %c", &a);
The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.
-Aditya
int input;
printf("Type in an odd number less than or equal to 9: \n");
int correctInput = 0;
do {
scanf("%d", &input);
if((input % 2) == 0) {
printf("You have not entered an odd number. Please try again. \n");
}
else if(input > 9 || input < 1) {
printf("Your input is not from 1 to 9. Please try again. \n");
}
else {
correctInput = 1;
}
} while(correctInput == 0);
printf("Input: %f. \n", input);
All I want to do is get an odd integer from 1-9 into the input variable. However when I run this code and enter in something like 7, I get
Type in an odd number less than or equal to 9:
3
Input: -1.#QNAN0.
printf("Input: %f. \n", input);
use this instead:
printf("Input: %d. \n", input);
f conversion specifier is to print double values, use d conversion specifier to print int values.
input is of integer type. Please try with printf("Input: %d. \n", input);
This line is not correct:
printf("Input: %f. \n", input);
The variable input is of int type so you should use the %d formatting sequence:
printf("Input: %d. \n", input);
Following is the correct syntax,
printf("Input: %d. \n", input);
format specifier %d stands for integers.
I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by doing:
scanf( "%i", &minx);
But I would like the user to be able to do something like:
Enter Four Ints: 123 234 345 456
Is it possible to do this?
You can do this with a single call, like so:
scanf( "%i %i %i %i", &minx, &maxx, &miny, &maxy);
Yes.
int minx, miny, maxx,maxy;
do {
printf("enter four integers: ");
} while (scanf("%d %d %d %d", &minx, &miny, &maxx, &maxy)!=4);
The loop is just to demonstrate that scanf returns the number of fields succesfully read (or EOF).
int a,b,c,d;
if(scanf("%d %d %d %d",&a,&b,&c,&d) == 4) {
//read the 4 integers
} else {
puts("Error. Please supply 4 integers");
}
Just to add, we can use array as well:
int i, array[4];
printf("Enter Four Ints: ");
for(i=0; i<4; i++) {
scanf("%d", &array[i]);
}
Could do this, but then the user has to separate the numbers by a space:
#include "stdio.h"
int main()
{
int minx, x, y, z;
printf("Enter four ints: ");
scanf( "%i %i %i %i", &minx, &x, &y, &z);
printf("You wrote: %i %i %i %i", minx, x, y, z);
}
The question is old, but if someone could help from this with real-life example.
For Single Input data -
int number;
printf("Please enter number : ");
scanf("%d", &number);
For Multiple Input data at a line -
int number1, number2;
printf("Please enter numbers one by one : ");
scanf("%d %d", &number1, &number2);
%d %d is for decimal format. You could use format which is suitable for your data type as many as needs with just a space
&number1, &number2 - Use comma between variable names
If needs More real-life example check this practical example - https://devsenv.com/tutorials/how-to-take-input-and-output-in-c-programming
Hope, this will help someone.
Passable for getting multiple values with scanf()
int r,m,v,i,e,k;
scanf("%d%d%d%d%d%d",&r,&m,&v,&i,&e,&k);
int a[1000] ;
for(int i = 0 ; i <= 3 , i++)
scanf("%d" , &a[i]) ;