How to validate user input? - c

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.
}
}

Related

Unable scan character in while loop

i want infinite scan
this is my code:
int main() {
int i=1;
char score;
while(1)
{
printf("Please Input No %d of char",i);
scanf("%C",&score);
i++;
}
return 0;
}
my output:
Please Input No 1 of char:A
Please Input No 2 of char:B
Please Input No 3 of char:Please Input No 4 of char:C
Please Input No 5 of char:Please Input No 6 of char:
how do i fix my i variable in while loop normal.
thanks
Use getchar(); after scanf()
int main() {
int i=1;
char score;
while(1)
{
printf("Please Input No %d of char\n", i);
scanf("%c",&score);
getchar();
//or
scanf(" %c", &score);
// don't need getchar() here.
i++;
}
return 0;
}
Note that %C isn't standard.
And scanf("%c",&score); is taking a char but while We are pressing Enter this is an another input. So, we need getchar()

Catching the user entering a string when expecting an integer?

I am making a program where the user enters two binary numbers and an operation character then I print the output in decimal. I want the while loop to keep the program running until the user enters quit. How can I read if the user enters quit and not an integer value in the scanf? Is there a way to catch this?
#include <stdio.h>
#include <string.h>
int toDecimal(int num);
int main(){
// Define variables
int num1, num2, result = 0;
char op;
char run[4] = "go";
// While loop to rerun program until quit is entered
while(strcmp(run, "quit\n") != 0){
// Reads user input
scanf("%i %c %i", run, &num1, &op, &num2);
printf("\nnum1: %i num2: %i op: %c\n", num1, num2, op);
num1 = toDecimal(num1);
num2 = toDecimal(num2);
printf("\nnum1: %i num2: %i op: %c\n", num1, num2, op);
}
printf("\nGoodbye!\n");
return 0;
}
I believe that I may be able to read everything in as a string and then convert to an integer however I don't know how. Is this a solution I should look into?
How can I read if the user enters quit and not an integer value in the scanf?
There is no good way with scanf(). Instead get user input with fgets() and do not use scanf() until you know why it is bad.
// Read user input
char buf[80]; // Use adequate size input buffer,
while (fgets(buf, sizeof buf, stdin)) {
buf[strcspn(buf, "\n")] = '\0'; // Lop off potential trailing \n
if (sscanf(bufm "%i %c %i", &num1, &op, &num2) == 3) {
printf("\nnum1: %i num2: %i op: %c\n", num1, num2, op);
num1 = toDecimal(num1);
num2 = toDecimal(num2);
printf("\nnum1: %i num2: %i op: %c\n", num1, num2, op);
} else if (strcmp(buf, "quit")== 0) {
break;
} else {
printf("Bad input <%s> ignored\n", buf);
}
}
This is a pretty basic idea , I hope you get the point.You could also use dynamic memory allocation when reading the string.
Basically you read the whole input with fgets and you extract the variables using sscanf.
#include <stdio.h>
#include <string.h>
int main ()
{
int n1,n2;
char arr[1000],oper;
fgets(arr,sizeof(arr),stdin);
while (strcmp(arr,"quit\n")!=0){
if (sscanf(arr,"%d %c %d",&n1,&oper,&n2)==3); /* Scanning for each number and operator and checking input*/
else
printf("wrong input");
/* Code */
fgets(arr,sizeof(arr),stdin);
}
return 0;
}

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;
}

How to stop a program based on user's input in C

I'm just starting to learn C and I'm having problem with stopping my program based on what the user inputted.
#include <stdio.h>
#include <stdbool.h>
int main()
{
int a;
int b;
char c[5];
printf("Enter the two values you like to compare, type stop to end.\n");
while (c != "stop")
{
scanf_s(" %d %d %s", &a, &b, &c);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}
printf("Thanks for playing.");
getchar();
return 0;
}
The problem that I'm having is having to put in another variable, c, in my scanf_s. How would I do it so that the user does not have to put in another word after the 2 numbers? Also how can I check if the user only input "stop" so that it will stop the program? Btw the way I have it right now also does not stop the program when I do "10 10 stop". Thanks.
remove & for c in scanf_s(" %d %d %s", &a, &b, &c);
use strcmp to compare strings.
if you need to ignore case while comparing use strcasecmp (for UNIX based systems) and stricmp (for windows).
Use do-while instead of while if you need to run the loop at least once.
Full Working Code:
#include <stdio.h>
#include <string.h>
int main()
{
int a;
int b;
char c[5] = {'\0'};
do {
printf("Enter the two values you like to compare, type stop to end.\n");
scanf("%d%d%s", &a, &b, c);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}
while (strcmp(c,"stop"));
printf("Thanks for playing.");
getchar();
return 0;
}
while (c != "stop")
You cannot compare strings in C like that, use memcmp() or strncmp() library functions available in string.h. Read about them to know how they can be implemented as condition in while loop.
Also, to get string input, use
scanf_s(" %d %d %s", &a, &b, c); // Remove that litle '&' before 'c'.
NOTE: The function scanf_s returns the number of inputs scanned correctly, you should check that before proceeding with input values.
To get the user to stop without explicitly entering "stop", many ways are there:
1) Use do-while loop and keep asking user if he wants to play more.
2) Use negative numbers input (say -1) to quit.
Use line below to make sure your 'c' scan your string entered.
scanf(" %d %d %s", &a, &b, c);
Edit:
Consider replacing your while with line below to make sure you stop works. Include "string.h"
while (strcmp(c,"stop"))
Here is the fixed version... I have add the comments in the code for understanding...
#include <stdio.h>
#include <string.h>
int main()
{
int a;
int b;
char c[5] = {'\0'};
printf("Enter the two values you like to compare, type stop to end.\n");
while (strcmp(c,"stop"))
{
scanf("%d%d%s", &a, &b, c);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}
printf("Thanks for playing.");
getchar();
return 0;
}
First, lets correct your program:
&c (third argument to scanf_s) is incorrect. It should be c (because it is already a pointer). According to docs, scanf_s requires sizes to be specified for all %s format strings; therefore, the way you are doing things now, you should have written scanf_s(" %d %d %4s", &a, &b, c);
The program would be much easier to use if you changed your logic to "enter a blank line to exit". You can test this looking at the return value of scanf_s, which will be the number of format strings correctly read from the input.
If you need to ask for strings, then allow either, and look at whatever the user wrote to see whether it was number or string:
#include <stdio.h>
#include <string.h>
int main() {
int a;
int b;
char c[32];
printf("Enter the two values to compare, or type stop to end.\n");
while (fgets(c, 31, stdin) != NULL && strncmp("stop\n", c)) != 0) {
// user did not request to exit and wrote something; maybe 2 numbers
if (sscanf(c, "%d %d", &a, &b) != 2) {
// but he did not write two numbers. Complain and repeat.
printf("please write two numbers to compare, or type stop to end.\n");
continue;
}
if (a == b) {
printf("both are equal\n");
} else {
printf("both are not equal\n");
}
}
printf("Thanks for playing.\n");
return 0;
}
fgets reads whole lines, and you can then try to parse them using sscanf. This has the advantage over common scanf that you can try to parse the same line in different ways, depending on what you find in it. Generally, you do not want to fill your code with getchar(). If you are debugging, your debugger will stop for you. If you are not debugging, you will want to test or use your program with input redirection, and getchar() is simply not needed.
Its because you are using &c instead of just c in scanf_s. Replace that and it should work. Also, c is a string, so, you have to use strcmp instead of !=.
An easier way to write this code would be::
int main()
{
int a;
int b;
char c;
do
{
printf("Would you like to play?\nPress 'Y' for 'Yes' or 'N' for 'No'\n");
scanf( "%c", &c ) ;
/*scanf_s( "%c", &c, 1 ) ; */
if( c != 'Y' && c != 'y' )
break ;
printf("Enter the two values you like to compare\n" ) ;
scanf(" %d %d", &a, &b);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}while(1) ;
printf("Thanks for playing.");
getchar();
return 0;
}

Switch small program does not let me see result

I am learning the switch statement of C. This is my small program and it runs and does the calculation but doesn't let me see the result of the operation. The black window shows up so that I input the numbers and the operator and then for a fraction of a second shows the result and disappears. Any help is appreciated.
#include <stdio.h>
int main(int argc, char *argv[])
{
int num1, num2, ans=0;
char ch, name;
printf("Enter a value: ");
scanf("%d", &num1);
printf("Enter a second value: ");
scanf("%d", &num2);
printf("Input * To multiply\
+ To add\
- To subtract: ");
scanf(" %c", &ch);
switch(ch)
{
case'*':
ans=num1 * num2;
printf("%d times %i equals: %i",num1,num2,ans);
break;
case'+':
ans=num1+num2;
printf("%i plus %i equals: %d",num1,num2,ans);
break;
case'-':
ans=num1-num2;
printf("%d minus %d equals: %d",num1,num2,ans);
break;
default:
printf("Range numbers");
}
getchar();
return ch;
}
Probably due to output buffering. Add newlines (\n) last in your formatting strings.
As a newbie, you should end all your printf format string with an escaped newline \n, i.e. printf("%i plus %i equals %d\n", num1, num2, ans); (or you should call fflush(stdout); just after the end of the switch before the getch and before all your scanf).

Resources