How can I insert 2 other value in scanf one time - c

I have an incorrect number in numB.
How can I insert 2 other value in scanf one time?
Or it can't do it?
#include <stdio.h>
main()
{
char a,b;
int numA,numB;
printf("A : ");
scanf("%c%d",&a,&numA);
printf("B : ");
scanf("%c%d",&b,&numB);
printf("\n\n%d %d",numA,numB);
}
result
A : S 67
B : D 56
67 1684370524

The problem is that the value read into b is the newline character (Enter key) that you pressed after typing in 67. Then reading numB fails because it tries to interpret D as numB.
If you had typed S 67 D 56 <Enter> (i.e. without the Enter in the middle) then you would get the right output.
To fix this, one way is to change your format string to " %c%d". The space means that it will consume any whitespace before trying to read a character.

You get a newline from after the first number in b, and then an undefined value in numB. Use " %c%d" to avoid this problem; it skips white space.
#include <stdio.h>
int main(void)
{
char a, b;
int numA, numB;
printf("A : ");
scanf(" %c%d", &a, &numA);
printf("B : ");
scanf(" %c%d", &b, &numB);
printf("\n%d %d\n", numA, numB);
return 0;
}

Try this out and see if it works:
#include <stdio.h>
void main(){
char a,b,e;
int c,d;
printf("A: ");
scanf("%c %d",&a,&c);
e=getchar();
printf("B: ");
scanf("%c %d",&b,&d);
printf("%d %d",c,d);
}

Related

Input in c language

Why I am not able to take 3rd input of character in my c - program??
#include <stdio.h>
int main(){
int a;
scanf("%d",&a);
float f1;
scanf("%f",&f1);
char ch;
scanf("%c",&ch);
printf("Integer %d\nfloat %f\ncharacter %c",a,f1,ch);
}
scanf("%c",&ch);
After entering the float value and pressing enter, the scanf reads that extra newline character \n. To prevent that use this:
scanf(" %c",&ch);
Space before %c reads that extra \n and the required character will be stored in ch.
To demonstrate that please see the code below :
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
float f1;
scanf("%f", &f1);
char ch;
scanf("%c", &ch);
printf("Integer %d\nfloat %f\ncharacter %d", a, f1, ch); // changed to %d to print ASCII value
}
The output is :
12
12.3
Integer 12
float 12.300000
character 10
You can see the output here, it prints 10 (ASCII of \n).
Solution :
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
float f1;
scanf("%f", &f1);
char ch;
scanf(" %c", &ch);
printf("Integer %d\nfloat %f\ncharacter %c", a, f1, ch);
}
The output :
12
12.3
k
Integer 12
float 12.300000
character k

getchar following scanf (to read an integer) takes terminating "enter" key of scanf as character

When I have a scanf followed by a getchar, why does the getchar always keep getting the last delimiting character of scanf? How can I stop that? I tried looking into "format specifiers" for scanf, read quite a few things but none solves this.
The code is shown below -
#include <stdio.h>
#include <conio.h>
int main()
{
int a;
char b;
printf ("Enter an integer \n");
scanf_s(" %d", &a);
printf("Enter a character \n");
b = getchar();
printf("The integer you entered is %d \n", a);
printf("The character you entered is %c \n", b);
_getch();
return 0;
}
The output is as below -
Enter an integer
4563
Enter a character
The integer you entered is 4563
The character you entered is
The enter key I press at the end of integer entry is being returned by getchar. The screen does not even stop after printing "Enter a character". What is the correct way to do this ?
Use the scanf(" %c", &b) instead of getchar()
When you put the space befor the %c you clean the buffer
Or you can clean the buffer using this too:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
Complete example:
int main(void)
{
printf("Enter an integer \n");
int a;
scanf(" %d", &a);
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
}
printf("Enter a character \n");
char b = getchar();
printf("The integer you entered is %d \n", a);
printf("The character you entered is %c \n", b);
_getch();
}
But I think the scanf()

Why does this scanf in a while loop work?

I can't understand why this does exactly what I want. The part where I used two scanf's in the loop confuses me. I compiled it using devcpp.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int dend, dsor, q, r;
char c;
while(c!='n')
{
printf("enter dividend: ");
scanf("%d", &dend);
printf("enter divisor: ");
scanf("%d", &dsor);
q=dend/dsor;
r=dend%dsor;
printf("quotient is %d\n", q);
printf("remainder is %d\n", r);
scanf("%c", &c);
printf("continue? (y/n)\n");
scanf("%c", &c);
}
system("PAUSE");
return 0;
}
FWIW, your code invokes undefined behavior. In the part
char c;
while(c!='n')
c is an uninitialized local variable with automatic storage and you're trying to use the value of c while it is indeterminate.
That said, first scanf("%c", &c); is used to eat up the newline present in the input buffer due to the press of enter key after previous input. You can read about it in details in another post.

c - char variable proceeds without taking input

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

write a program that prompt user to enter a telephone number

I am writing a program that promts the user for a telephone number in the form (xxx) xxx-xxxx and then displays the number in form xxx.xxx.xxxx in C language.
#include <stdio.h>
int main(void) {
int d1, s2, d3;
printf("enter phone number[(xxx) xxx-xxxx]:"); //phone number to be entered
sscanf("%d %d-%d", d1, s2, d3); //to read input in above format
printf("you entered %d.%d.%d", d1, s2, d3);
return 0;
}
My problem is that scanf is unable to read data entered with () round brackets.
You meant to use scanf instead of sscanf. Also, you should memory address of the variable to be written into to scanf. You should change the format string of scanf to "". scanf returns the number of input items assigned successfully. Check this value for 3 to find if the input was entered in the required format.
#include <stdio.h>
int main(void) {
int d1, s2, d3;
int val; // to check if scanf was successful
// newline causes the string to be immediately
// written to stdout
printf("enter phone number[(xxx) xxx-xxxx]:\n");
val = scanf("(%d)%d-%d", &d1, &s2, &d3);
// check if scanf was successful
if(val == 3)
printf("you entered %d.%d.%d", d1, s2, d3);
else
printf("input not in the correct format.\n");
return 0;
}
You can get the input into a string. e.g.
#include <stdio.h>
#define NUMBER_LEN 14 //the number of characters in the string (the phone number)
int main()
{
char phone[NUMBER_LEN];
printf("enter phone number[(xxx) xxx-xxxx]: ");
gets(phone);
printf("You entered %s", phone);
return 0;
}
Further, you can play with your string and format it.
Use scanf instead sscanf(" %c", &char);
This is a simple program which reads the entered numbers as a array of characters that is string and simply prints these numbers : -
#include<stdio.h>
#include<conio.h>
void main(){
char phone[16];
printf("Enter mobile number: ");
scanf("%s",&phone);
printf("Your Mobile Number is: %s",phone);
getch();
}
int main(void)
{
int a, b, c;
printf("Enter phone number: [(xxx) xxx-xxxx]: "); scanf("(%d)%d-%d", &a, &b, &c);
printf("You entered: %d.%d.%d", a, b, c);
return 0;
}

Resources