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;
}
Related
I'm new to C, and currently trying to practice on some simple codes, and I'm currently stuck with that next one.
After entering the first customer data, and repeat the code... View_customer function fails to show the saved data, and when I try to go to enter a second account data, it fails at the second entry.
#include<stdio.h>
#include<stdlib.h>
typedef struct cust{
char name[60];
int acc_no,age;
char address[60];
char citizenship[15];
double phone;
char acc_type[10];
} cust;
void new_account(int num);
void view_account(int num);
int main()
{
cust customers[10];
char answer;
int n;
int cutomer_number=1;
int cutomer_num2;
printf("Welcome To The program X: \n");
do{
printf("\n How can we serve you today? \n 1.Create a new account \n 2.Print an existing Account info \n ");
scanf("%d",&n);
if (n==1)
{
new_account(cutomer_number);
cutomer_number++;
}else {
printf("Please enter your Cust number: ");
scanf(" %d",&cutomer_num2);
view_account(cutomer_num2);
};
printf("\n Press Y to continue. Press any Key To Exit: ");
scanf(" %c",&answer);
}while (answer == 'Y' || answer == 'y');
return 0;
}
void view_account(int n)
{
cust customers[n];
printf("Your name is %s \n ", customers[n].name);
printf("Your age is %d \n", customers[n].age);
printf("Your address is %s \n", customers[n].address);
printf("Your citizenship is %s \n", customers[n].citizenship);
printf("Your phone number is %f \n", customers[n].phone);
printf("Your account type is %s \n", customers[n].acc_type);
};
void new_account(int n)
{
cust customers[n];
customers[n].acc_no = n;
printf("You are the customer number %d \n", customers[n].acc_no);
printf("Please, Enter your name: ");
scanf("%s", &customers[n].name);
printf("Please, Enter your age:");
scanf(" %d", &customers[n].age);
printf("Please, Enter your address: ");
scanf(" %s", &customers[n].address);
printf("Please, Enter your citizenship: ");
scanf(" %s", &customers[n].citizenship);
printf("Please, Enter your phone number: ");
scanf(" %f", &customers[n].phone);
printf("Please, Enter your account type: ");
scanf(" %s", &customers[n].acc_type);
}
>
Each of your three functions declares its own customers array variable, so each of them has their own memory for the customer data. Moreover, the array of new_account goes out of scope at the end of the function, so you can no longer safely access the data. Because of how C compilers typically work, the customer data is not immediately erased from memory, so your view_account function might still be able to read it, but that is what is called "undefined behavior". Which means it might work, or it might not.
Try to pass down the array from the main function to the other two functions in parameters. Or, to make things simpler at first, you could also turn the local customers variable of main into a global variable.
cust customers[10];
int main(int argc, char *argv[])
{
char answer;
int n;
int cutomer_number=1;
int cutomer_num2;
printf("Welcome To The program X: \n");
...
}
void view_account(int n) {
printf("Your name is %s \n ", customers[n].name);
...
}
void new_acccount(int n)
{
customers[n].acc_no = n;
...
}
Note that there are further issues in your code, like not checking the return value of scanf or overflowing the char arrays of the struct if you enter too many characters (missing bounds and length checking), or being able to enter more than 10 customers and accessing out of bounds of the customers array, or not using customers[0] (because your customer_number starts at 1, but array indices are 0-based). But I will not go into further details here to keep the answer focused.
I am developing a software in C and I need to read a whole number, but if the user presses the "enter" key, the scanf should ignore and not modify the current value of the variable. Here is an example:
#include <stdio.h>
#include <stdlib.h>
void main(void) {
int a = 5;
printf("\nenter an integer value for the variable 'a': ");
scanf("%d", &a);
printf("\n the value of variable 'a' is: %d", a);
//if the user presses the enter key the output is: 5
//if the user enters number 7 the output is: 7
}
thanks
Always check the result of scanf.
In your case:
if(scanf("%d", &a) != 1)
printf("\nThe scanf was not successful\nThe value of the `a` was not changed\n");
When scanf was not successful the variable is not changed.
https://godbolt.org/z/GcY51W
solution:
#include <stdio.h>
#include <stdlib.h>
void main(void) {
int a = 5;
char number[2];
printf("\nenter a value for the variable 'number': ");
fgets(number, 2, stdin);
if(sscanf(number, "%d")>0){
a = atoi(number);
}
printf("variable 'a' is: %d\n", a);
}
I wrote this short code to test my understanding of the isdigit function:
int inChar;
printf("enter input:");
scanf(" %d", &inChar);
if (isdigit(inChar))
printf("Your input was a number");
else
printf("Your input was not a number.\n");
When I test this program and I enter a number, C returns the else statement (Your input was not a number.). So regardless of if I enter a number or a letter, the program returns the else statement.
Why is this so?
isdigit() checks if a single character that was passed to it by converting the char value an unsigned char.
So, you can't directly pass any int value and expect it to work.
Man isdigit() says:
isdigit()
checks for a digit (0 through 9).
To check single digit, you can modify:
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit((unsigned char)inChar)) {
printf("Your input was a number");
}
else {
printf("Your input was not a number.\n");
}
If you have an array (a string containing a number) then you can use a loop.
The function's purpose is to classify characters (like '3'). Running it on something that's read using %d doesn't make sense.
You should read a single char using %c. Remember to check that reading succeeded.
The C library function void isdigit(int c) checks if the passed character is a decimal digit character.
If you badly wanna try it with an int you can init in this way
int inChar = '2';
The following code gave expected results.
int main()
{
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit(inChar))
printf("Your input was a number. \n");
else
printf("Your input was not a number.\n");
return 0;
}
Output:
vinay-1> ./a.out
enter input:1
Your input was a number
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);
}
In the program written below how i can ensure that only integer value is entered?
And if character is entered the program should convert it to its ASCII equivalent and then add them and show output as number.
Please help me......
#include<stdio.h>
int main(int argc, char **argv)
{
int a,b,c;
printf("enter two numbers:-");
scanf("%d \t %d",&a,&b);
c=a+b;
printf("addition of numbers= %d",c);
}
scanf returns the number of items that it successfully read, so you can check to make sure that it returns the same number that you expect. For example,
if (scanf("%d \t %d", &a, &b) != 2)
{
// handle error
}
Note that \t is a whitespace character, and whitespace is ignored by scanf.
Just to add to what James said.
Dont forget to flush stdin
#include<stdio.h>
int main(int argc, char **argv)
{
int a,b,c;
printf("enter two numbers:-");
if( scanf("%d \t %d",&a,&b) == 2 )
{
c=a+b;
printf("addition of numbers= %d",c);
}
else {
printf("please enter a valid input");
getchar(); // the getchar will clear the stdin otherwise next time you go do
// a scanf it might not work.
}
}