Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I am trying to write a program to check validity of month entered by user. Here are my codes:
int monthcheck(int m) {
int month = 0;
while (m <= 0 || m > 12) {
printf("Month must be between 1 and 12, re-enter month:");
scanf("%d", &m);
}
if (m == 2) {
month = 1; // February
}
if (m==1||m==3||m==5||m==7||m==8||m==10||m==12) month=2;
return month;
}
int main()
{
int m;
printf("Enter month");
scanf("%d",&m);
if (monthcheck(m)==0) printf("The month is valid");
else if (monthcheck(m)==1) printf("The month is february");
}
And here is my output:
Enter month-2
Month must be between 1 and 12, re-enter month:13
Month must be between 1 and 12, re-enter month:2
Month must be between 1 and 12, re-enter month:2
The month is february
My question is: why does the program still ask me to enter another input (at line 4) although it is not meet the conditions of while loop? Help is appreciated.
Many thanks!
The second if statement is called
else if (monthcheck(m)==1) printf("The month is february");
if the expression in the first if statement
if (monthcheck(m)==0) printf("The month is valid");
evaluates to false.
The both if statements call the function with the unchanged variable m declared in main.
So when you entered 2 the first time within the function then the function returned the value 1 due to this if statement
if (m == 2) {
month = 1; // February
}
And the body of the first if statement within main was skipped and the second if statement got the control that again called the function with the initial value of the variable m equal to -2 because the variable in main was not changed.
Now again the function returned the value 1 and the body of the second if statement got the control.
This output
Enter month-2
Month must be between 1 and 12, re-enter month:13
Month must be between 1 and 12, re-enter month:2
is generated by the first function call called in the expression of the first if statement.
And this output
Month must be between 1 and 12, re-enter month:2
The month is february
is generated by the second function call called in the expression of the second if statement when the function was called with the same value of the variable m equal to -2.
is generated
At least you should write the if statements like
if ( ( m = monthcheck(m) ) == 0 ) printf("The month is valid");
else if ( m == 1 ) printf("The month is february");
Though the function does not make a great sense because the value 1 returned by the function is also valid. That is it is unclear what you are trying to achieve with using the function.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
#include <stdio.h>
int main() {
/*function declaration*/
double invalidInput(double);
int loop_obeTask;
double obe_Task[3];
double checkInput ;
for(loop_obeTask = 1 ; loop_obeTask < 4 ; loop_obeTask++)
{
printf("OBE%d : ", loop_obeTask);
scanf("%lf",&obe_Task[loop_obeTask]);
checkInput = invalidInput(obe_Task[loop_obeTask]);
if(checkInput = 1)
{
printf("INVALID INPUT \n" );
break;
}
}
return 0;
}
double invalidInput(double inputGrade)
{
double indicator;
if(inputGrade > 100)
{
indicator = 1;
}
else{
indicator = 0;
}
return indicator;
}
Hello Guys, want to validate the input numbers using the function once the user enters > 100 IT IS INVALID INPUT but in my case, it always shows invalid input both greater and less than 100. I can't post image because my reputation less than 10.
enter image description here
enter image description here
Short answer
You're not checking the value, but assigning it: if(checkInput = 1)
You should use the == operator instead.
Long answer
Even if it does compile, your code presents numerous conceptual mistakes:
You're using double as return value of invalidInput(). Since that function is for validation only and it returns either 1 or 0, you should use int as return type instead.
Your for loop starts at index number 1, which is wrong and will lead to undefined behaviour. C array indexes starts at 0, so if you define an array with size 3, the available indexes are going to be 0, 1, 2, and the loop should look something like for (index = 0; index < MAX; index++) where MAX is 3 in this case.
Your if condition is not checking the equality of checkInput and 1. That's because you're using the assignment operator =, instead of the equality operator ==.
It's common practice to put your function declarations at the top of your file and before the main definition, both for readability and scope reasons.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to enter value to my array and between 0 and 101 and trying to put zero or 101 will return an error but putting anything from 1-100 will allow me to enter a second number.
but i am stuck when write code to add a second number the loop keeps going in 1 and i cant figure out my error. any help would be apreciated
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char letter;
int main()
{
int i;
int grades[12] ;
for ( i = 0; i < 12; i++)
do{//loops the following peice of code if values over 100 or less than 0 are entered
printf("Please Enter Grade 1:\n");
scanf("%f", & grades[i]);
if (grades > 100 || grades < 0)
printf("Invalid Entry please enter another between 0 and 100:\n");
}while(grades < 100 || grades > 0);
}
The mistake here is you're comparing the whole array to a value. What you want is one element, plus you need to test that you're outside of the acceptable bounds:
while(grades[i] > 100 || grades[i] < 0);
As Jonathan points out below, the logic in your code tests that it's within, which means only valid answers will loop forever, the opposite of what you wanted.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Im creating a program that asks the user for an integer and checks if it is between two values and and if not, asks the user to try again. Im currently having trouble with the condition for the while loop. I want the program to check if the input number from the user is between 28 and 31 but when i put the inequality in, it acts as if its completely skipping the while loop. statement on line 8
Thanks in advance for the help everyone, I've just started learning and want to learn as much as possible. here is the code in text form instead of image form:
printf("Enter days in the month (between 28 & 31): ");
int d = get_int();
while ( d<28 && d>31 )
{
printf("error");
}
printf("how many pennies on the first day? ");
float p = get_float();
You are having a wrong logical statement in your while loop condition.
It should be d<28 || d>31. You are falsely using the and operator instead of or operator.
I think what you are to do with the while loop is not what the code is actually doing. Based on your description, you want to keep prompting the user to enter number of days until they enter the correct value. However, what your code is currently doing is asking once and then throwing error within the while loop. Perhaps try something like this
int d;
do {
printf("Enter days in the month (between 28 & 31): ");
d = get_int();
}
while ( d<28 || d>31 );
First we declare the variable d.
Then we start a do while loop that will keep running the code within the loop (asking the user to input days in month). If user enters a value that is less than 28 OR greater than 31, the while loop resets and asks the user to enter the value again. If the user enters a valid value, the loop exists and moves to next question.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm having trouble with an exercise on my homework.
I have to make a function that tells me when the next leap year is given an n (or n if it's a leap year).
I already tackled the last part, but I'm having trouble with the "next leap year" part. I assume I have to do a cycle?
Here's what I have so far
int next_leapyear(int n)
{
if(((n%4==0)&&(n%100!=0))||(n%400==0)) return n;
else
while(?){
n++;
}
return n;
}
I'm just starting to learn this language, so if you guys could keep it simple I would appreciate it
The task could be split into two parts. First, one could check whether a given n is a leap year, which coule be done with the following function.
int IsLeapYear(int n)
{
return (((n%4==0)&&(n%100!=0))||(n%400==0));
}
Next, one could use a loop, based on the function above, to increase n until it is a leap year. This could be done as follows.
int GetNextLeapYear(int n)
{
int CurrentYear = n;
while(!IsLeapYear(CurrentYear))
{
CurrentYear++;
}
return CurrentYear;
}
Your idea of increasing the year until it's a leap year will work but it's not very efficient because you have to do a lot of modulos which is a very expensive operations.
In fact to find the next normal leap year you just need to round the year to the next multiple of 4, which can be achieved with either of these
year = (year + 3) & ~0x03;
year = (year | 0x03) + 1;
In case the after rounded it's a multiple of 100 then just add 4 to get the correct year. So the implementation may be like this
int next_leapyear(int n)
{
n = (n + 3) & ~3;
if (n % 100 == 0) n += 4;
return n;
}
Increase n until your if statement condition is not true so there are 2 ways either you can put condition in while braces, which is straight forward.
2nd is that, run loop infinite times and put breaking condition inside the loop
while(1){
n++;
if(((n%4==0)&&(n%100!=0))||(n%400==0)) break;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So basically I was creating a program to ask the user how many times they wanted to test the program.But I couldnt figure out the problem with my for loop. So:
If the user wants to test the program 3 times, it should ask for the value of a 3 times only and it should quit after.
This is my code as follows:
#include <stdio.h>
int main()
{
int test;
printf("How many times do you want to test the program?");
scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++) //I cant figure out whats going on with the for loop.
{
printf("Enter the value of a: \n");
scanf("%d", &test);
;
}
return 0;
}
The output should be:
"How many times do you want to test the program": 3
Enter the value of a : any numerical value
Enter the value of a: any numerical value
Enter the value of a: any numerical value
(exit)
In this section of your code:
scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++)
First, memory owned by test is populated with a value entered by user. (this is OK)
Next, you nullify that new value in memory by setting test to zero. (this is not OK)
finally the construction of your loop statement is incorrect.
In a correct version of your for loop, test should be a value used as a limit against which an index is tested as that index is incremented across a range of values, for example, from 0 to some positive value.
You probably intended:
scanf("%d", &test);
//test = 0; // Reinitializing the test from 0 (leave this out)
for(int i = 0; i <= test; i++)
{
...
Where a separate index value (i) is incremented and tested against the limit test.