1

Following is the code snippet i'm working, although I need 4 values for my input, until the user types correct value, it should keep asking, any inputs on the same Thanks in advance

#include<stdio.h>
#include<math.h>

int
main(int argc, char *argv[])
{
        int days;
        float peny;

        do{
                printf("Enter Number of days: ");
                scanf("%d", &days);
        }
        while ((days != 28) || (days != 29) ||(days != 30) ||(days != 31));
        printf("Number of days are %d", days);


}
0

4 Answers 4

5

Think about what your loop condition is saying. It will keep looping whilst any of those individual conditions is still true, or to put it another way, looping until all of them are false simultaneously. Is that possible?

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly, I did not think it that way Oli, thanks for picking that up, as Jonathan said in the snippet. I did not think it that way, since the loop is waiting for all conditions to be false, and the culprit was the OR condition instead of the AND condition. Thanks a lot
2

while((days<28)||(days>31)); // ie. while days are illegal because to low OR too high

Comments

2

The condition

(((days != 28) || (days != 29) ||(days != 30) ||(days != 31)))

in your do-while loop will always be true because the value of days can not be 28, 29,30, 31 at the same time. The or operator could be changed to and:

while ((days != 28) && (days != 29) && (days != 30) && (days != 31));

1 Comment

Yes, I did not think of this in a way, since it will keep on looping if I put the conditional OR operator. The conditional AND works perfectly fine, this is really helpful. Thanks Jonathan
1

Always test the return status from scanf(); if it doesn't tell you 1, it is failing, perhaps because the user typed a instead of 31.

Consider counting the number of consecutive failures (and bailing out if the count gets too high) so the user doesn't get frustrated.

Consider using fgets() to read the input and sscanf() to parse it; it is generally easier to manage.

Consider using C++ for C++ and C for C; what you show is a C program rather than a C++ program (though it can indeed be compiled with C++, unless my eyes are deceiving me). In general, the languages are very different.

Include a newline at the end of the final printf().

3 Comments

"what you show is a C program rather than a C++ program" - I think that may need some clarification for someone just starting out (as evidence by the question itself). (Also, don't C programs need "return 0;" - unlike C++? ;-P)
Umesh - in C++, following Jonathan's advice, you get something like std::string s; int num_failures = 0; while (true) { std::cout << "Enter days in month: "; if (getline(std::cin, s)) { std::istringstream iss(s); if (iss >> days) if (days >= 28 && days <= 31) break; else std::cerr << days << " is never a valid number of days in a month.\n"; else std::cerr << "you need to enter a number\n"; if (++num_failures == 3) { std::cerr << "Giving up - goodbye\n"; exit(EXIT_FAILURE); } } - it'll look a little better when indented properly... :-)
@TonyDelroy: In C89 (eg with Microsoft Visual C), you do indeed need a return 0; at the end of main(). Sadly, C99 allows you to omit that from main() -- but only main() -- for conformity with C++98 (I assume). So, it is a valid program in the old (C99 -- as opposed to very old C89 or new C11) standards. I take it to be a C program precisely because it is using functions such as scanf() for which C++ has arguably better alternatives (or, at least, notationally distinctively different alternatives; I'm referring to the >> and << input and output operators, of course).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.