input value 123 -- this value is integer, and valid
input value 1b23a -- this value is invalid
How do I detect which values are valid and not?
Here is my code:
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
Please see this answer regarding use of strtol(). It is a safe way to convert arbitrary input that should be a string representation of an integer, while also saving 'garbage' bytes for additional analysis.
Using it, your code would look something like this:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef LINUX_VERSION
#include <curses.h>
#else
#include <conio.h>
#endif
#define BUFF_SIZE 1024
int main(void)
{
char str1[BUFF_SIZE], *garbage = NULL;
long num = 0;
printf("Enter the Number ");
scanf("%s",str1);
errno = 0;
num = strtol(str1, &garbage, 0);
if (errno) {
printf("The number is invalid\n");
return 1;
}
printf("You entered the number %ld\n", num);
if (garbage != NULL) {
printf("Additional garbage that was ignored is '%s'\n", garbage);
}
getch();
return 0;
}
This doesn't fix everything that is questionable about what you posted, but it should help you get off to a better start.
Output is:
tpost#tpost-desktop:~$ ./t
Enter the Number 1234abdc
You entered the number 1234
Additional garbage that was ignored is 'abdc'
Compiled via:
gcc -Wall -DLINUX_VERSION -o t t.c -lcurses
I'm not sure what platform you are using, so additional fixes to the code may be needed.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
One way is to use sscanf and check that there are no characters following the number. This is done most easily by adding a %c on the end and testing the return code, like this:
const char *yourString = ...;
int theValue, dummy;
if (sscanf(yourString, "%d%c", &theValue, &dummy) == 1) {
// Was a pure number, parsed into 'theValue'
} else {
// Either no number or had junk after it
}
Related
It's just a code to receive user inputs in C program, but fails to do so and accepts null space as input. I have tried fgets() as well and the same thing keeps happening. Please advice on how to fix.
#include <math.h>
#include <stdio.h>
//#include <string.h>
#define len 16
int main(void)
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,i=0,j=0;
printf("enter the number of cards:");
n = getchar();
//scanf("%d",&n);
int c1[len][n],card[len][n];
char buf[len];
printf("Enter card number:");
gets(buf);
system("Pause");
return (0);
}
"...code to receive user inputs in c program, but fails to do so and accepts null space as input..."
The reasons your existing code has problems is covered well in the comments under your post.
Consider a different approach: Define the following:
char inBuf[80] = {0};//
int numCards = 0;//Pick variable names that are descriptive (n is not)
int cardNum = 0;
bool isnum;
Then use it in conjunction with printf() etc.
printf("enter the number of cards:");
if(fgets(inBuf, sizeof(inBuf), stdin))//will read more than just a single char, eg. "12345"
{
int len = strlen(inBuf);
isnum = true;
for(int i=0;i<len;i++)
{
if(!isdigit(inBuf[i]))
{
isnum = false;
break;
}
}
if(isnum)
{
numCards = atoi(inBuf);
}
else
{
printf("input is not a number\n"
}
}
printf("Enter card number:");
if(fgets(inBuf, sizeof(inBuf), stdin))
{
...
Repeat variations of these lines as needed to read input from stdin, with modifications to accommodate assignment statements based on user input i.e. an integer (this example is covered), a floating point number, a string (eg. a persons name)
Although there is more that you can do to improve this, it is conceptually viable for your stated purpose...
I'm trying to scan the input from terminal and I'm trying to scan the initial white space but the program just skip it. I tried using this method before in a different program but it doesn't work in my new one. Plz help!!!
#include <stdio.h>
#include <string.h>
#define ADMIN_PASS "ABC123"
#define MAX_ARR_LEN 20
#define debug
void getinput(char inp[], int n);
void password(char passUser[]);
int main(void)
{
char passUser[MAX_ARR_LEN+1];
int i=1;
while (i==1)
{
password(passUser);
printf("Try again?(1/0)>");
scanf("%d",&i);
if (i == 1)
printf("\n");
}
return 0;
}
void getinput(char inp[], int n)
{
scanf("%[^\n]c", &inp[n-1]);
#ifdef debug
printf("\nThe entered code in function>%s\n",inp);
printf("The 1st character of entered code in function>%c\n",inp[0]);
#endif
}
void password(char passUser[])
{
char admin[MAX_ARR_LEN+1] = ADMIN_PASS;
do
{
printf("\nPlease enter the Administrator password to Login:\n");
getchar();
getinput(passUser);
#ifdef debug
printf("\nThe input password in main is>%s\n", passUser);
printf("The 1st character in main is>%c\n", passUser[0]);
#endif
if (strcmp(passUser, admin) != 0)
{
printf("The password entered is incorrect, try again\n");
}
} while (!(strcmp(passUser, admin) == 0));
}
You should pass the string with fgets(inp, sizeof(ADMIN_PASS), stdin) like this:
#include <stdio.h>
#include <string.h>
#define ADMIN_PASS "ABC123"
#define MAX_ARR_LEN 20
#define debug
void getinput(char * inp);
void password(char * passUser);
int main(void)
{
char passUser[MAX_ARR_LEN+1];
int i=1;
while (i==1)
{
password(passUser);
printf("Try again?(1/0)>");
scanf("%d",&i);
if (i == 1)
printf("\n");
}
return 0;
}
void getinput(char * inp)
{
fgets(inp, sizeof(ADMIN_PASS), stdin);
#ifdef debug
printf("\nThe entered code in function>%s\n",inp);
printf("The 1st character of entered code in function>%c\n",inp[0]);
#endif
}
void password(char * passUser)
{
char admin[MAX_ARR_LEN+1] = ADMIN_PASS;
do
{
printf("\nPlease enter the Administrator password to Login:\n");
getinput(passUser);
#ifdef debug
printf("\nThe input password in main is>%s\n", passUser);
printf("The 1st character in main is>%c\n", passUser[0]);
#endif
if (strcmp(passUser, admin) != 0)
{
printf("The password entered is incorrect, try again\n");
}
} while (!(strcmp(passUser, admin) == 0));
}
I removed getchar() and the second parameter of the function getinput() cause they were useless.
printf("Enter number of patients:");
int numberOfInputs = scanf("%d", &patients);
if (numberOfInputs != 1) {
printf("ERROR: Wrong number of arguments. Please enter one argument d.\n");
}
I am asking the user to input one number as an argument, but would like to print out a statement if the user does not input anything or puts in more than one input. For example, once prompted with "Enter number of patients:", if the user hits enter without entering anything, I would like to print out a statement. The code above is what I have been specifically tinkering around with it for the past couple hours as a few previous posts on this site have suggested but when I run it in terminal, it does not work. Any suggestions? Thank you in advance, and all advice is greatly appreciated!
If I understand your question right, you want to print an error when the input is anything other than an integer and this includes newline as well. You can do that using a char array and the %[] specifier.
Example:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int patients;
char str[10];
printf("Enter number of patients:");
int numberOfInputs = scanf("%[0-9]", str);
if (numberOfInputs != 1) {
printf("ERROR: Wrong number of arguments. Please enter one argument.\n");
}
patients = atoi(str); //This is needed to convert the `str` back to an integer
}
This will print the error when the user just hits ENTER as well.
This looks super over-complicated, but it basically splits the input, checks it to be exactly one and than checks it to be an integer (and converts it). It works fine in loop as well and handles empty input.
I'm sure there are more elegant solutions to this problem, it's just a suggestion.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
int getNumberOfInput(char* str);
bool isNumber(char* str);
int main()
{
char str[512];
while(1)
{
printf("Enter text: ");
fgets(str, 512, stdin);
int numberOfInput = getNumberOfInput(str);
if ( numberOfInput == 0 )
printf("You must give an input\n");
else if ( numberOfInput > 1 )
printf("You have to give exactly one input\n");
else
{
if (!isNumber(str))
printf("The input is not an integer\n");
else
{
int input = atoi(str);
printf("input: %d\n", input);
}
}
}
return 0;
}
int getNumberOfInput(char* str)
{
char* word = strtok(str, " \t\n\v\f\r");
int counter = 0;
while(word != NULL)
{
++counter;
word = strtok(NULL, " \t\n\v\f\r");
}
return counter;
}
bool isNumber(char* str)
{
int i, len = strlen(str);
for (i=0; i<len; ++i)
if (!isdigit(str[i]))
return false;
return true;
}
How do I put isalpha and isdigit in a while(1) loop?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i;
char type[256];
printf("You can type a number or a word. Type exit to exit! \n");
printf("Type: ");
fgets (type, 256, stdin);
if (isalpha(type[i]))
{
printf("Typed text: %s\n", type);
if((strcmp(type,"exit\n") == 0))
{
printf("Exiting...\n");
exit(1);
}
}
else if (isdigit(type[i]))
{
printf("Typed number: %s\n", type);
}
else
{
printf("Typed: %s\n", type);
printf("Its not a letter or number...?!\n");
}
}
I tried adding while(1) at the start at the code and close it at the end of code, but as soon as I enter number or letter the console crashes... Could someone please help me with this?
Your problem is not a loop problem, you need to give a value to i , as it is undefined and you get a nice crash. Please replace
int i;
with
int i=0;
I seek for an expert in c programming. Thanks in advance.
Example:
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
char name[100][52],selection[2]="Y";
int x,nname=1;
float sales;
do
{
printf("Enter name: ");
fflush(stdin);
fgets(name[nname],51,stdin); // i need put a wrapper in here
printf("Enter sales: ");
scanf("%f",&sales);
if (sales<1000)
printf("%s\tgood\n",name[nname++]);
else
printf("%s\tvry good\n",name[nname++]);
printf("Enter another name?(Y/N)");
fflush(stdin);
fgets(selection,2,stdin);
*selection=toupper(*selection);
}while(nname<=100 && *selection=='Y');
for(x=1;x<nname;x++)
printf("%s\n",name[x]); // want print the result without(newline) /n
printf("END\n");
system("pause");
}
How do I print the names without being separated by new lines?
I compiled it with GCC 4.4.1 - MinGW and it works fine.
It launched only a warning. This is the result:
warning: return type of 'main' is not 'int'|
||=== Build finished: 0 errors, 1 warnings ===|
Now it works as you expect.
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <string.h> // strlen()
void main() {
char name[100][52],selection[2]="Y";
int x,nname=1;
float sales;
do {
printf("Enter name: ");
fflush(stdin);
fgets(name[nname],51,stdin); // i need put a wrapper in here
for (x=0; x<strlen(name[nname]); x++){ // this will discarge the \n
if (name[nname][x] == '\n')
name[nname][x] = '\0';
}
printf("Enter sales: ");
scanf("%f",&sales);
if (sales<1000)
printf("%s\tgood\n",name[nname++]);
else
printf("%s\tvry good\n",name[nname++]);
printf("Enter another name?(Y/N)");
fflush(stdin);
fgets(selection,2,stdin);
*selection=toupper(*selection);
} while(nname<=100 && *selection=='Y');
for(x=1; x<nname; x++)
printf("%s ",name[x]); // want print the result without(newline) /n
printf("\nEND\n"); // inserted \n before END
system("pause");
}
Just use
printf("%s ", name[x]);
instead of
printf("%s\n", name[x]);
The \n character is creating the new lines.
Edit
fgets apparently reads newlines into the buffer - you can strip the newline with
name[nname][strlen(name[nname])-2] = '\0';