-
November 12th, 2013, 12:19 PM
#1
Keep getting strange results with char and scanf
Code:
#include <stdio.h>
int main(){
unsigned char x, y=10;
scanf ("%c", &x);
x = (x)*(y);
printf("%i", x);
if (x==100){
printf ("correct");
}
return 0;
}
If I take out scanf and assign x a value then it works, but if I enter, for example, 10 into scanf it comes up as 234 rather than 100.
-
November 12th, 2013, 12:42 PM
#2
Re: Keep getting strange results with char and scanf
It seems to me that you want x to be an int, not an unsigned char, upon which you should read in with say %d or %i instead of %c.
-
November 12th, 2013, 01:02 PM
#3
Re: Keep getting strange results with char and scanf
I want it to be a char, the program will only ever hold numbers from 1-12 so I'm using char to save memory.
-
November 12th, 2013, 01:09 PM
#4
Re: Keep getting strange results with char and scanf
Originally Posted by cassas00
I want it to be a char, the program will only ever hold numbers from 1-12 so I'm using char to save memory.
Unfortunately, then you need to read in the input as a string which you parse into a char (or unsigned char). You cannot use scanf since scanf with %c would read in just a char, e.g., your input of "10" was read in as '1', which in ASCII is 49. This 49 was multiplied by 10 to get 490. Presumably 490 is larger than UCHAR_MAX, so the result was wrapped around to get 234.
Frankly, it would be simpler to just use an int. Your concern for saving memory is misplaced here, especially since the char will be promoted to int for the calculation anyway.
-
November 12th, 2013, 01:13 PM
#5
Re: Keep getting strange results with char and scanf
I see your point.
How would I ensure scanf only accepts numbers?
while ((answer >='a' && answer <='z')||(answer >='A' && answer <='Z'))
{
printf ("Error, invalid input, please re-enter answer ");
scanf(" %i", &answer);
}
-
November 12th, 2013, 01:18 PM
#6
Re: Keep getting strange results with char and scanf
Originally Posted by cassas00
How would I ensure scanf only accepts numbers?
Approach #1: use scanf to read into an int, then check that scanf returns 1.
Approach #2: read into a string then parse it into an int with error checking. Unfortunately, atoi does not do proper error checking, so one way is to use a long instead of an int with strtol.
-
November 12th, 2013, 01:32 PM
#7
Re: Keep getting strange results with char and scanf
can you elaborate on approach #1 please? Having a little trouble implementing it.
-
November 12th, 2013, 01:38 PM
#8
Re: Keep getting strange results with char and scanf
I have this
#include <stdio.h>
int main()
{
int v;
while (scanf("%d", &v) != 1) {
printf("ERROR");
v = getchar();
scanf("%d", &v);
}
}
It works but I need to type in integer twice to end the loop?
-
November 12th, 2013, 01:39 PM
#9
Re: Keep getting strange results with char and scanf
Originally Posted by cassas00
can you elaborate on approach #1 please? Having a little trouble implementing it.
Hmm... okay. Here's an example derived from yours involving a controlled infinite loop:
Code:
#include <stdio.h>
int main(void) {
int x;
for (;;) {
printf("Enter an integer: ");
if (scanf("%i", &x) == 1) {
break;
}
}
x *= 10;
printf("%i\n", x);
if (x == 100) {
printf("correct\n");
}
return 0;
}
You probably want something slightly different to give an initial prompt for input and then an error message on invalid input, but I leave that as an exercise.
Notice that I indent my code properly so that it is readable.
-
November 12th, 2013, 01:40 PM
#10
Re: Keep getting strange results with char and scanf
Originally Posted by cassas00
can you elaborate on approach #1 please? Having a little trouble implementing it.
Show your implementation and describe the "trouble"
Victor Nijegorodov
-
November 12th, 2013, 01:45 PM
#11
Re: Keep getting strange results with char and scanf
Sorry, when I paste the code doesn't indent
#include <stdio.h>
int main()
{
int v;
while (scanf("%d", &v) != 1) {
printf("ERROR");
v = getchar();
scanf("%d", &v);
}
}
This gives me what I'm looking for, an error if a number isn't entered, but when a number is entered it doesn't end the loop, only when I enter a second number does it end the loop and accept that number
so it will do
enter number f //enter f
error f
error f
error f
error 4 // enter 4, loop doesn't end
5 // second number inputted ends loop and accept that number (if another letter is entered it carries on with Error)
-
November 12th, 2013, 01:52 PM
#12
Re: Keep getting strange results with char and scanf
Originally Posted by cassas00
Sorry, when I paste the code doesn't indent
It is because you didn't use Code tags!
Victor Nijegorodov
-
November 12th, 2013, 01:56 PM
#13
Re: Keep getting strange results with char and scanf
Code:
#include <stdio.h>
int main()
{
int v;
while (scanf("%d", &v) != 1) {
printf("ERROR");
v = getchar();
scanf("%d", &v);
}
}
Option wasn't there on quick reply so I just typed it in, any insight why I have to enter a number twice to end the loop?
-
November 12th, 2013, 02:03 PM
#14
Re: Keep getting strange results with char and scanf
Ah got it now, was because I had more than one scanf so it repeated the question, thanks for your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|