I'm having trouble making this exercise for C-programming. I need to use the getchar()-method instead of the scanf(). When I use the scanf, everything works perfect when I type for instance 7. However when I use the getchar() and type 7, I will get the ASCII-code of 7, not the int 7. How do I fix this?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i;
printf("Voer een getal in:\n");
fflush(stdout);
i = getchar();
//scanf("%d", &i);
if (i > -1000 && i < +1000) {
printf("het ingevoerde getal is: %d\n", i);
} else {
printf("foutieve invoer\n");
}
return EXIT_SUCCESS;
}
This is the correct behavior of getchar. While scanf's %d format specifier converts a sequence of digits to a decimal number, with getchar you need to do it yourself.
In order to do that, you need to know three things:
When the sequence of digits ends,
How to convert an ASCII code of a digit to a number, and
How to combine multiple digits into a single number.
Here are the answers:
You can decide to end the character input when the value returned by getchar is not a digit. You can use the isdigit function for that (include <ctype.h> header to use it).
You can convert a single digit character to its corresponding numeric value by subtracting the code of zero (i.e. '0') from the value returned by getchar
You can combine multiple digits into a number by starting the partial result at zero, and then multiplying it by ten, and adding the value of the next digit to it.
int num = 0;
for (;;) {
int ch = getchar();
if (!isdigit(ch)) break;
num = 10 * num + (ch - '0');
}
Related
I'm trying to write a short script to take in 6 inputs that will be in decimal, hex or octal and output their decimal versions. For example, if I input 1, 1 should be output. Input 010, should get 8, input 0x20, should get 32. Do I have to test each value to see if scanf reads it as its type or can I cast them after scanf reads them all as is? Also do I need functions to convert the octal and hex values to decimal or can I cast them? I'm very new to C and don't understand it well yet, but here's what I have so far (it outputs -200 as 32765 for whatever reason):
int num[6];
printf("Enter six integers:\n");
int i = 0;
int j;
while (i < 6){
if(scanf("0x%x", &j) == 1){
scanf("0x%x", &num[i]);
//hexToDec(num[i]);
} else if (scanf("0%o", &j) == 1){
scanf("0%o", &num[i]);
//octalToDec(num[i]);
} else {
scanf("%i", &num[i]);
}
i+=1;
}
for(int p = 0; p < 6; p+=1){
printf("%i\n", num[p]);
}
Answer for future reference: simply scanning in with "%i" does the conversions automatically. Revised code below:
int main(){
int num[6];
printf("Enter six integers:\n");
int i = 0;
while (i < 6){
scanf("%i", &num[i]);
i+=1;
}
for(int p = 0; p < 6; p+=1){
printf("%i\n", num[p]);
}
}
You cannot use scanf("0x%x", &j) to test the input and then use scanf("0x%x", &num[i]) to put the value in num[i]. scanf consumes the characters it accepts, so they are no longer in the input stream. When you do the second scanf, the characters are gone.
Instead, just attempt to scan the desired thing. If it works, you are done. If it does not work, go on to an else:
if (scanf("0x%x", &num[i]) == 1)
; // Worked, nothing to do.
else if (scanf("0%o", &num[i]) == 1)
;
else if (scanf("%i", &num[i]) == 1)
;
else
{
// Nothing worked, should put error-handling code here.
}
That is actually not great code for real applications, because scanf will consumes some of the input even if it ultimately fails. For example, with scanf("0x%x", &num[i]), if the input contains “0x” but then contains a non-hexadecimal character, scanf will consume the “0x” but leave the next character. However, it suffices for a learning exercise.
Once you have values in the num array, they are just int values. They are mathematical values, not numerals that are in octal, decimal, or hexadecimal. You can print them with %o for octal, or %d or %i for decimal. The original numeral is irrelevant. When printing, the mathematical value will be used to format a string in the requested base.
You should not use %x for printing an int, as %x is for unsigned int. However, you could convert an int to unsigned int and then print it with %x.
Note that you should not scan to int objects with %x. %x is for scanning to unsigned int objects. You can actually scan hexadecimal, octal, and decimal using:
if (scanf("%i", &num[i]) == 1)
;
else
…
The %i specification will recognize hexadecimal numerals beginning with “0x”, octal numerals beginning with “0”, and decimal numerals.
If you did not want to use %i for all three, because you want direct control for some reason, you will need to write more code. Standard C does not provide a direct way to scan only a hexadecimal numeral to an int. You would need to get the characters from the input and calculate the value from them or scan to an unsigned int and then convert the result to an int.
You can use function strtol with a base of 0 to do auto-detection of the integral value's base according to the input strings format (cf, for example, strtol documentation at cppreference.com):
long strtol( const char *str, char **str_end, int base )
Interprets an integer value in a byte string pointed to by str. ...
The valid integer value consists of the following parts:
(optional) plus or minus sign
(optional) prefix (0) indicating octal base (applies only when the base is 8 or 0)
(optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is 16 or 0)
If the value of base is 0, the numeric base is auto-detected.
So a call like long val = strtol("0x10",NULL,0); will yield a decimal value of 16.
Note that you can also use scanf in conjunction with format specifier %i, since this is defined to behave just as a call to strtol:
int scanf( const char *format, ... )
%i matches an integer. The format of the number is the same as expected by strtol() with the value 0 for the base argument (base is
determined by the first characters parsed)
The quick answer is use only
if(scanf("%i", &j) == 1){
...
}
what I have so far (it outputs -200 as 32765 for whatever reason):
Yet error handing of scanf() is troublesome. Far better to read the line of user input with fgets() and then parse the line - perhaps with strtol().
Novice C student here.
Could someone please explain why isdigit() is returning true for values 10+?
I'm doing a pretty basic assignment regarding a guessing game and must use isdigit() to inform user if he has entered a number 1-10 or not.
The program seems to be running fine otherwise, I just would like to know the reasoning behind isdigit() returning true for values 10+.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main()
{
int iRandomNum = 0;
char cResponse = '0';
srand(time(NULL));
iRandomNum = (rand() % 10) + 1;
printf("\nGuess a number between 1 and 10: ");
scanf("%c", &cResponse);
if (!isdigit(cResponse) || cResponse<'0'+1)
printf("\nYou did not enter a number 1-10");
else if ((cResponse - '0') == iRandomNum)
printf("\nCorrect!");
else
{
printf("\nSorry, you guessed wrong\n");
printf("The correct guess was %d\n", iRandomNum);
}
return 0;
}
If you add a printf to log the value of cResponse, the problem will become apparent very quickly:
printf("\nGuess a number between 1 and 10: ");
scanf("%c", &cResponse);
printf("cResponse is %c\n", cResponse);
outputs:
Guess a number between 1 and 10: 10
cResponse is 1
As you can see, only the first character is stored in cResponse (which makes sense, as it's just a single character), and since that first character is a digit, your isdigit() call returns true.
If you want to read numbers greater than 10, you can read to an int instead:
int cResponse = 0;
printf("\nGuess a number between 1 and 10: ");
scanf("%d", &cResponse);
printf("cResponse is %d\n", cResponse); // prints '10' if I type '10'
Note that you cannot use isdigit() in this case, although you can still easily check your bounds using if (cResponse >= 0 && cResponse <= 10).
You are passing a char to isdigit, which can only hold a single character. Thus, while you may be typing in 10, only the first character (which is a digit) is getting into cResponse.
Instead of reading the users input as a char, read it in as an int. This way you can just use if(guess >= 1 && guess <= 10). Since you need 10 as part of the range of inputs a char (which is a single character) wont do. You will either need to use a string (which will make things more complicated) or just use an int.
I have been trying to add some experience in C to my experience with Python and started with a basic addition program. One thing that I'm trying to do is check if the input is a number or a character as seen here:
#include <stdio.h>
#include <ctype.h>
int main()
{
int n, sum=0,c,value;
printf("Enter the Number of Integers You Want to Add\n");
scanf("%d", &n);
if(isdigit(n))
{
printf("Enter %d Integers\n", n);
for(c=1; c<=n; c++)
{
scanf("%d", &value);
if(isalpha(value))
{
printf("ENTER INTEGER NOT CHARACTER\n");
break;
}
else
{
sum = sum + value;
}
}
printf("Sum of Entered Integers = %d\n",sum);
}
else
{
printf("ENTER INTEGER NOT CHARACTER\n");
break;
}
return 0;
}
Initially I had tried this using isalpha(), and the program worked fine when adding numbers but interpreted characters as zeros instead of printing the "not an integer" statement. However, now that I reworked it to use isdigit(), it does not recognize ANY input as an integer, whether or not it is. Is there something that I'm just doing wrong?
When you use scanf to read an integer, you get just that, an integer. (To read a single character, you need %c and a pointer-to-char).
When you use isdigit(), you need to supply the representation of that character (e.g. in ASCII, the character '0' has the representation 48, which is indeed its value as an integer). To recap:
isdigit(0) is false
isdigit(48) is true (for ASCII, ISO8859, UTF-8)
isdigit('0') is true (no matter the character set)
isdigit('0' + n) is true for integers n = 0 ... 9
PS: Not testing the return value from scanf is asking for trouble...
Neither isdigit nor isalpha work as you think they do. The intent of those library functions is to check whether a given code point, represented as an int, is within a subset of points defined by the standard to be digit characters or alpha characters.
You should be checking the results of your scanf calls rather than assuming they just work, and acting on those results accordingly. If you request an integer and one is successfully scanned, then it will tell you so. If that fails, your course of action is probably to consume the rest of the line (through newline or EOF) and possibly try again:
#include <stdio.h>
int main()
{
int n,value,sum=0;
printf("Enter the Number of Integers You Want to Add\n");
if (scanf("%d", &n) == 1 && n > 0)
{
printf("Enter %d Integers\n", n);
while (n--)
{
if (scanf("%d", &value) == 1)
{
sum = sum + value;
}
else
{
// consume the rest of the line. if not EOF, we
// loop around and try again, otherwise break.
while ((value = fgetc(stdin)) != EOF && value != '\n');
if (value == EOF)
break;
++n;
}
}
printf("Sum of Entered Integers = %d\n", sum);
}
return 0;
}
Properly done you should be able to enter valid integers beyond single digits (i.e. values > 10 or < 0), which the above allows.
The %d marker to scanf tells it to interpret the input as a number (more accurately, it indicates that the pointer in the arguments points to an integer type). It can't do anything but put an integer into that argument. If it can't interpret the input as a number, scanf stops scanning the input and returns immediately.
isdigit() evaluates its argument as a character code, as Jens points out above. However, scanf already turned the character code into a pure number.
From the scanf man page:
On success, the function returns the number of items of the argument list
successfully filled.
In your program, you are trying to read just one item from stdin, so scanf should return 1. So check for that and you'll know that it all worked out ok:
printf("Enter the Number of Integers You Want to Add\n");
while(scanf("%d", &n) != 1) {
printf("That's not a valid integer. Please try again.\n");
}
You cannot use isdigit() the way you are using it because you're already using scanf to convert the user input to an integer. If the user had not input an integer, scanf would have already failed.
Look at the man pages for all the C functions you are using, they will show you what the function expects and what the return values will be under different circumstances.
In the case of isdigit(), the input is expected to be an unsigned char representing an ASCII character. This can be a bit confusing because ASCII characters are in fact represented as a type of integer, and a string is an array of those. Unlike languages like Python which hide all that from you. But there is a big difference between the STRING of a number (array of characters that contain the characters of the digits of the number) and the INTEGER itself which is in a form the processor actually uses to do math with... (simplified explanation, but you get the idea).
For my first task I am using fgets() to read a string from stdin. So I input 1234567890 on the terminal and it stores the string into a variable called str. Now I want to split the digits up and perform addition. In this case the sum would be 55 since 1+2+3+4+5+6+7+8+9+0 = 55.
How can I do that?
My code so far
#include <stdio.h>
#include <string.h>
int main(void){
char str[100];
printf("Please enter the 10 digit number \n");
fgets(str, 10, stdin);
//Separate the digits
//calculate their sum
int sum =..............
//print out the sum
printf("the sum of the digits is: %s", sum);
return(0);
}
Approach 1:
If you know for sure that you're counting for single digit integer, you can use
array indexing to read the digit-by-digit value from the input string.
convert the char value to corresponding int as per the required encoding (mostly, ASCII).
define one int variable , say res to hold the sum. perform the addition and store the result to sum.
loop untill terminating null is reached.
print out the sum using %d format specifier.
Approach 2:
Alternatively, you can do something like (again assuming,single digit integer)
Convert the whole string to integer using strtol(). You must check for the errors in that case.
define one int variable , say res to hold the sum.
perform modulo 10 (%0) on the converted interger value to take out the last digit. add and store that inres`
divide converted interger value by 10 (p /= 10).
continue to step 2 untill the result is 0.
when the converted interger value becomes 0, print the res.
P.S - Just FYI, usual way of splitting a string based on some delimiter is to use strtok().
Since you are sure the string will contain digits, subtract each character from '0' to get their numeric value.
int sum = 0;
fgets(str, sizeof str, stdin);
char *s = &str;
while(*s != '\0') {
sum += *(s++) - '0';
}
printf("the sum of the digits is: %d", sum);
First, make sure that you read only digits by using scanf, like this:
char str[11]; // 10 digits plus \0 terminator
printf("Please enter the 10 digit number \n");
scanf("%10[0-9]", str);
Now you can go through the digits one by one by indexing into str, and subtracting the code of digit zero, like this:
int nextDigit = str[i] - '0';
This should be enough information for you to complete the solution by adding a for loop around it. Make sure that you print sum using %d, not %s like you did in your code sample.
Let's say that we have a very simple code that begins like:
#include <stdio.h>
int main() {
char c;
int x;
printf("Num here: ");
c = getchar();
x = 0;
while (c!=EOF) {
x = c - 48; //to convert x from ASCII
In this program, I'm trying to get the user to type a number (ex. 42) and am trying to add the ones digit to the tens (and to the hundreds, etc.); I'm having a lot of trouble understanding how you can get the while loop to go back to the loop until the end of the number.
So, I need a lot of help understanding how I can get the loop to read until the end of the character, read the char input the user puts in as a number (42), and then, treat the numbers individually using just getchar().
Normally, you'd use:
int c; // Because getchar() returns an int, not a char
int x = 0;
while ((c = getchar()) != EOF)
{
if (isdigit(c))
x = x * 10 + (c - '0');
else
...
}
This reads a character each time it reaches the top of the loop. You get the loop to go back by running into the brace at the end of the loop (or, occasionally, by using a continue statement). You might exit the loop with a break, for example if you read some character that can't be part of a number.
If the user types 42 (followed by Enter), then you first read c == '4' and then c == '2' and then you read newline '\n'. For every digit from '0' to '9', digit - '0' yields the number corresponding to the digit. The newline can't be part of the number, so you either put it back with ungetc(c, stdin) or break the loop when you've read it.
Beware of overflow if the user types 43219876543 where you expected just 42 (and int is a 32-bit quantity).
You could write the loop condition as:
while ((c = getchar()) != EOF && isdigit(c))
or even:
while (isdigit(c = getchar()))
I'd be extremely reluctant to actually put the latter into production code but it is, in theory, safe.
How could I treat each number individually so that I can use the entirety of the numbers later on? So that if the user types 10 20 30, I can multiply 10 by 20, then (10*20) by 30?
Wheels within wheels — or loops within loops. You'll need to specify your criteria a bit. If the user types 1 you want the answer 1; if they type 1 2, you want 2; if they type 1 2 3, you want 6; and so on (where these are all the numbers on a single line of input). You'll need an outer loop that skips over blanks and tabs, then uses the inner loop to read a number, and then multiplies the current product (initial value 1) by the new number, and after the outer loop, you'll print the product. This will print 1 for an empty line; maybe that doesn't matter (and maybe it does).
Here's some code that approximates what is appropriate:
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF && c != '\n')
{
int product = 1;
while (c != EOF && c != '\n')
{
while (isspace(c))
c = getchar();
int number = 0;
while (isdigit(c))
{
number = number * 10 + (c - '0');
c = getchar();
}
printf("Number: %d\n", number);
product *= number;
}
printf("Product: %d\n", product);
}
return 0;
}
I also tried a version with a slightly different 'skip' loop:
while (c != EOF && c != '\n' && !isdigit(c))
c = getchar();
Both work OK on sane inputs. Empty lines are treated as end of input; lines containing blanks are not. If you input 1a2b3c with the second condition, you will get the output 0; with the first, you get an infinite loop. There is no overflow protection; don't try doing factorial 20 and expect the correct answer (with 32-bit int). Tweak to your heart's content.
Your code :
#include <stdio.h>
#include<ctype.h>
int main() {
int c;//getchar() returns integer
int x;
printf("Num here: ");
x=0;
//As #Jonathan Leffler suggested ,
//usage of while loop like this is very helpful the moment you press Enter loop breaks.
while (isdigit(c = getchar())) //isdigit is a function from ctype.h checks for Entered character is digit or not
x = x*10 + c - 48; //here '0'==48
printf("%d",x);
}
when you enter 42
loop rotates two times for c==4 and c==2
c==4
x=0*10+'4'-48 //here '4'==52 ==> x=0+52-48 ==>x=4
c==2
x=4*10+'2'-48 //here '2'==50 ==> x=40+50-48 ==>x=42
to add ones digits to tens and then hundreds ... if you want to add digits in input number use this below while loop
int sum=0,num;
//read num
while(num>0)
{
sum=sum+num%10; //get the last digit and add to sum
num=num/10; //reduce last digit of num
}
Read in character by character and convert that to a numeral using the while loop in Jonathan's answer. Every time you read a numeral, simply multiple your current sum by 10, and add the number. That way by the time you read the last numeral and add it in, you'll have the correct number.
Sometimes the way we think a problem should be solved, can be solved in a different method when all of the languages capabilities are considered.
#include <stdio.h>
int main() {
int x;
printf("Num here: ");
scanf("%d", x);
}
implements the same functionality as your program.