#include <stdio.h>

int main(int argc, const char* argv[])
{

   int x;

   loopstart:
   printf("enter a value : ");
   scanf("%d", &x);
   getchar(); 
   
   printf("value is %d\n", x);
   return 0;

}

can u help me, I want x return integer number (ASCII code) when enter a character...

my code is not working...

thanks...

Recommended Answers

All 9 Replies

#include <stdio.h>

int main(int argc, const char* argv[])
{

   int x;

   loopstart:
   printf("enter a value : ");
   scanf("%d", &x);
   getchar(); 
   
   printf("value is %d\n", x);
   return 0;

}

can u help me, I want x return integer number (ASCII code) when enter a character...

my code is not working...

thanks...

I'm not sure what your after...Is this it:

#include <stdio.h>

int main(void)
{
	int ch;

	ch = getchar();
	fprintf(stdout, "hex value->0x%x\n", ch);
	return 0;
}

thanx for reply... but its not a right thing i want...

I want:

enter a integer number or character, x will return real integer number (not decimal in ASCII)...

i want to filter only number, not character in x variable...

can u help me???

Accept input as a single character
Test the character to see if it '0' thru '9'
If it isn't, tell user it's not an integer.

Accept input as a single character
Test the character to see if it '0' thru '9'
If it isn't, tell user it's not an integer.

i want get a number (integer value), and drop out all character, and inform user enter wrong value.

i want get a number (integer value), and drop out all character, and inform user enter wrong value.

Then you should read what WaltP posted

I followed the WaltP posted and when I enter a character, the program inform wrong value but i can not get integer value from char c variable...

#include <stdio.h>

int main(void){

   char c;

   loopstart:
   printf("enter integer number : ");
   scanf("%c", &c);

   if (c>='0' && c<='9'){
   printf("value is : %c\n", c);
   }
   else{
   printf("wrong value!!!\n");
   }
   return 0;
}

Try something like this(Or what WaltP said)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char**argv)
{
	int ch;

	fputs("enter a value->", stdout);

	while ((ch = fgetc(stdin)) != '\n')
	{
		if (isdigit(ch))
		{
			fprintf(stdout, "ch is a digit->%c\n", ch);
		}
		else
		{	
			fprintf(stdout, "ch is not a digit->%c\n", ch);
		}
	}

	exit(EXIT_SUCCESS);
}

Now when prompted for a value enter 1q2w3e4r5t6y

Write the following program using functions (name it exercise1c.c). Make functions as follows

    * all numbers are asked with one function called ask_integer_number. This function will have two parameters (the range) and function will return the number which is in correct range. REMARK: function can return only one number at a time. Function shall meet the prototype

       int ask_integer_number(int min, int max);

    * function will print the numbers and results in a function called print_numbers.

       void print_numbers(int voltage, int resistance);

In main program use still the global variables, You have already defined
Test Your code by giving a character when asking for numbers. What did happen and why ?

Examine the functionality of scanf() - function ( read the man pages). Examine what are the scanf-functions return values also in error condition.

Give the answers to C-file as a comment.

Add a feature to You code that it will take notice of the rerurn value of scanf() and use this return value to get rid of that previouse error You did discover (about characters)

this is a requirement... ur code is good but this is not a thing I want...

im having problem with this :

Add a feature to You code that it will take notice of the rerurn value of scanf() and use this return value to get rid of that previouse error You did discover (about characters)

If you are having a problem with

Add a feature to You code that it will take notice of the rerurn value of scanf() and use this return value to get rid of that previouse error You did discover (about characters)

then you did not understand what happened when you followed these instructions:

Test Your code by giving a character when asking for numbers. What did happen and why ?

Examine the functionality of scanf() - function ( read the man pages). Examine what are the scanf-functions return values also in error condition.

Give the answers to C-file as a comment.

What did you discover doing these steps? What did happen? Did you figure out why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.