int input;
printf("Type in an odd number less than or equal to 9: \n");
int correctInput = 0;
do {
scanf("%d", &input);
if((input % 2) == 0) {
printf("You have not entered an odd number. Please try again. \n");
}
else if(input > 9 || input < 1) {
printf("Your input is not from 1 to 9. Please try again. \n");
}
else {
correctInput = 1;
}
} while(correctInput == 0);
printf("Input: %f. \n", input);
All I want to do is get an odd integer from 1-9 into the input variable. However when I run this code and enter in something like 7, I get
Type in an odd number less than or equal to 9:
3
Input: -1.#QNAN0.
printf("Input: %f. \n", input);
use this instead:
printf("Input: %d. \n", input);
f conversion specifier is to print double values, use d conversion specifier to print int values.
input is of integer type. Please try with printf("Input: %d. \n", input);
This line is not correct:
printf("Input: %f. \n", input);
The variable input is of int type so you should use the %d formatting sequence:
printf("Input: %d. \n", input);
Following is the correct syntax,
printf("Input: %d. \n", input);
format specifier %d stands for integers.
Related
I want this program to determine if given integers are positive/negative & odd/even then increment the counters. But it doesn't work properly.
include <stdio.h>
int main() {
int evenCount=0, oddCount=0, posCount=0, negCount=0, zeroCount=0, m;
char x='x';
while(x!='y') {
printf("enter an integer\n");
scanf("%d", &m);
if((m>0)&&(m%2==0)){
posCount+=1;
evenCount+=1;
}
else if((m>0)&&(m%2!=0)){
posCount+=1;
oddCount+=1;
}
else if((m<0)&&(m%2==0)){
negCount+=1;
evenCount+=1;
}
else if((m<0)&&(m%2!=0)){
oddCount+=1;
negCount+=1;
}
else {
zeroCount+=1;
}
printf("if you want to end the loop write 'y'\n");
scanf("%c", &x);
}
printf("odd %d \n", &oddCount);
printf("even %d \n", &evenCount);
printf("positive %d \n", &posCount);
printf("negative %d \n", &negCount);
return(0);
}
When I run it and give some numbers counts are at millions.
enter an integer
123
if you want to end the loop write 'y'
enter an integer
2
if you want to end the loop write 'y'
enter an integer
5
if you want to end the loop write 'y'
enter an integer
y
if you want to end the loop write 'y'
odd 6487572
even 6487576
positive 6487568
negative 6487564
The second example.
enter an integer
12
if you want to end the loop write 'y'
enter an integer
y
if you want to end the loop write 'y'
odd 6487572
even 6487576
positive 6487568
negative 6487564
I'm new to coding and this is my first post on this site also english is not my main language. I'm sorry if made any mistakes.
For starters you need to change this call
scanf("%c", &x);
to
scanf(" %c", &x);
Pay attention to the space before the conversion specifier.
And instead of trying to output addresses
printf("odd %d \n", &oddCount);
printf("even %d \n", &evenCount);
printf("positive %d \n", &posCount);
printf("negative %d \n", &negCount);
you have to write
printf("odd %d \n", oddCount);
printf("even %d \n", evenCount);
printf("positive %d \n", posCount);
printf("negative %d \n", negCount);
And it is better to substitute the while loop
while(x!='y') {
//...
printf("if you want to end the loop write 'y'\n");
scanf("%c", &x);
}
for do-while loop
do {
//...
x = '\0';
printf("if you want to end the loop write 'y'\n");
scanf(" %c", &x);
} while ( x != 'y' && x != 'Y' );
I am having some issues with the scanf function.
I am trying to printf a scanf result, but it only returns 0.00000
Can someone help me? Here is my code done so far:
#include <stdio.h>
int main() {
float grade;
char choise;
do {
printf ("Type your grade: ");
scanf("%f", &grade);
printf("Want to continue? s/n: ");
scanf(" %c", &choise);
printf("%s \n", &choise);
printf("%f", &grade);
}while(choise != 'n');
}
printf("%f", &grade); is wrong
try
printf("%f", grade);
But there are many other issues. At the very least, you must check the values returned by scanf, and you should use int main(void) or int main(int argc, char **argv). eg:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
float grade;
char choise;
do {
printf("Type your grade: ");
fflush(stdout);
if( scanf("%f", &grade) != 1 ){
fprintf(stderr, "Invalid input\n");
exit(1);
}
printf("Want to continue? s/n: ");
fflush(stdout);
if( scanf(" %c", &choise) != 1 ){
fprintf(stderr, "Invalid input\n");
exit(1);
}
printf("%c\n", choise);
printf("%f\n", grade);
} while( choise != 'n' );
return 0;
}
If you don't check the value returned by scanf, you don't know if any data was written into the variable. Since neither choise nor grade is initialized, attempting to read those values if scanf did not assign to them is undefined behavior. The behavior is also undefined if the input stream contains a value that cannot be represented as a float (eg, if it is a value greater than FLT_MAX), but that's really just an argument for avoiding scanf rather than a suggestion to try to make scanf usable. You can try to use scanf to make a user friendly interface, but it's really not worth the effort. Much better to simply abort on bad input. (If you want a user friendly interface, I would recommend you are using the wrong language.) See http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html for more details on why you really ought to just avoid scanf completely.
These calls of printf
printf("%s \n", &choise);
printf("%f", &grade);
are incorrect.
In the first call you are trying to output a single character as a string. In the second call you are trying to output a pointer of the type float * as an object of the type float.
Instead you have to write
printf("%c\n", choise);
printf("%f\n", grade);
The lines
printf("%s \n", &choise);
printf("%f", &grade);
are wrong.
They should be
printf("%c \n", choise);
printf("%f", grade);
The %s format specifier should only be used for strings (null-terminated sequences of characters), not individual characters. The expression &choice does not point to a string, as the character sequence is not null-terminated.
Also, the %c and %f format specifiers require values, not addresses, when using them with printf. Only when using them with scanf should you pass an address.
This is a snippet of my code. I'm confused why is digit can't function. It should if we input character/alphabet. The line is digit print "Please enter in numeric " but it doesn't print it. I need your opinion about this.
This my code:
printf("\nenter the amount of food to be purchased : ");
scanf("%d", &b);
printf("\n");
if (b >= 0) {
for (a=1; a<=b; a++){
printf("the price of food of- %d \t : ",a);
scanf("%d", &c);
printf("\n");
if (isdigit(c)) {
printf("Please enter in numeric !!\n");
while ((getchar()) != '\n');
system("PAUSE");
goto cashier;
}
printf("the amount ordered \t : ");
scanf("%d", &d);
printf("\n");
if (isdigit(d)) {
printf("Please enter in numeric !!\n");
while ((getchar()) != '\n');
system("PAUSE");
goto cashier;
}
scanf("%d", &c);. Reads an integer to c. When you call isdigit(c), you are not checking whether the input string is a number, you are checking whether the number inputted corresponds to an ascii character that represents a digit. This is not the intended behavior. What you want is this:
while (scanf("%d", &c) != 1) // Repeatedly get input until scanf reads 1 integer.
{
while (getchar()!='\n'); // Clear stdin.
puts("Please enter a number!");
}
// The resulting number is now stored in c.
This will try to read a number (not a string) into c. If the user does not enter 1 number, scanf() will not return 1 and the loop will try again. make sure that c is declared as an int and not a char, else numbers above 128 will overflow.
I'm currently learning C and I'm trying to create a program where the program checks whether a user inputs a letter or an integer:
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[256];
printf("\n Please enter a number or a letter: ");
scanf("%s", &a);
if (isdigit(a[256]) == 1)
{
printf("\n %c is a number.", a);
printf("\n The return value is %d.", isdigit(a[256]));
}
else if (isdigit(a[256]) == 0)
{
printf("\n %c is a letter.", a);
printf("\n The return value is %d.", isdigit(a[256]));
}
getch();
return 0;
}
However, when I run the program, this is what I get:
Please enter a number or a letter: 15
15 is a letter.
The return value is 0.
Or this:
Please enter a number or a letter: X
X is a letter.
The return value is 0.
Any input I type always return the same output. I wanted to pass an array of char so that the program will take in the entire input (e.g. If I input "230", the program will not just check "2" but "230").
Mis-match type (save time, enable all warnings), no width limit, wrong index, wrong compare (is...() return values: Only 0, not 0 important.), wrong function (use isalpha() to test for letter), wrong range passed. (is...() expect EOF,0-255, not char, which may be -128-127)
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[256];
printf("\n Please enter a number or a letter: ");
// scanf("%s", &a);
scanf("%255s", a); // limit input, use matching type
// if (isdigit(a[256]) == 1)
if (isdigit((unsigned char) a[0]))
{
// printf("\n %c is a number.", a);
printf("\n First character %c is a digit.", a[0]);
// printf("\n The return value is %d.", isdigit(a[256]));
printf("\n The return value is %d.", isdigit((unsigned char)a[0]));
}
// else if (isdigit(a[256]) == 0)
else if (isalpha((unsigned char)a[0]))
{
// printf("\n %c is a letter.", a);
printf("\n First character %c is a letter.", a[0]);
// printf("\n The return value is %d.", isdigit(a[256]));
printf("\n The return value is %d.", isdigit((unsigned char)a[0]));
}
else
{
printf("\n Neither digit nor letter.");
}
getch();
return 0;
}
printf("please enter a value for side a: ");
check1 = scanf(" %d*%c", &a);
while(check1 != 1)
{
printf("You have entered an invalid value");
scanf(" %d*%c", &a);
}
printf("The value for A is: %d\n", a);
I am trying to make sure that the entered value is an integer only with (check1 != 1). if I enter anything other than an integer and the while loop engages, it infinitely prints "You have entered an invalid value" but ignores the scanf to re enter the value of A.
I wrote a piece of code prior to this that didn't have the (check != 1) part, but had a scanf inside the while loop that worked.
printf("\nEnter the denominator number: ");
scanf("%d%*c", &num2);
while ( num2 <= 0 )
{
printf("The denominator can not be 0 or less, re enter the number: ");
scanf("%d%*c", &num2);
}
NOTE this second block of code works.
How can I fix this? Or can somebody suggest a simple alternative to making sure that 'a' is an integer? I am very new to programming and scanf is the only input prompt I am aware of.
Help would be much appreciated :)
Inside the while loop you need to change the value of 'check1' after using the 'scanf' function.
int a; char* w;
printf("please enter a value for side a: ");
int check1 = scanf(" %d*%c", &a);
while (check1 != 1)
{
scanf("%s", &w);
printf("You have entered an invalid value\nplease enter a value for side a: ");
check1 = scanf(" %d*%c", &a);
}
printf("The value for A is: %d\n", a);