0

I'm pretty new to coding and especially to C, so I decided to take the CS50 course as an introduction to the language. I just finished watching the first lecture on C and, as a means to test my knowledge on the subject, I attempted to write a short little program. Also I am using the course's library for the get_int() function.

The goal is to test the user's input and check if it's less or equal to ten. If it matches the parameters, the program should print the "Success!" message and exit; otherwise, it should ask for input again. If the input value is over 10, the program responds just as expected, but if you input a value of 10 or less, it ends up asking you for input one more time before actually exiting. I think it's probably something with the "for" loop, but I just can't figure it out.

My code:

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>

int check_for_value();

int main()
{
    for(check_for_value(); check_for_value() != 1;  check_for_value())
    {
        printf("Failed!\n");
    }
    exit(0);
}

int check_for_value()
{
    int i = get_int("Your value: \n");

    if(i <= 10)
    {
        printf("Success!\n");
        return 1;
    }    
    else
    {
        printf("Try again!\n");
        return 0;
    }
}
Simson
  • 2,905
  • 2
  • 18
  • 30

2 Answers2

0

The for loop can look very simply

for ( ; !check_for_value(); )
{
    printf("Failed!\n");
}

In such a case it is better to use the while loop

while ( !check_for_value() )
{
    printf("Failed!\n");
}

As for your for loop

for(check_for_value(); check_for_value() != 1;  check_for_value())
    ^^^^^^^^^^^^^^^^^                           ^^^^^^^^^^^^^^^^^

then the underlined calls of the function are not tested.

Also bear in mind that such a definition of a for loop

for(int ret = check_for_value(); ret != 1; ret = check_for_value()) {
    printf("Failed\n");
} 

is a very bad style of programming. There is redundant records of the function calls. The intermediate variable ret is not used in the body of the loop. So its declaration is also redundant. Never use such a style of programming.

Pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )

and the statement

exit( 0 );

is redundant.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • Note the C11 standard [§6.5.3.4 ¶8](http://port70.net/~nsz/c/c11/n1570.html#6.5.3.4p8). It illustrates `int main()` in an example — technically non-normative, but strongly indicative that it is 'OK' to use `int main()` as long as you aren't going to call `main()` (because it doesn't provide a prototype for `main()`). It is preferable to use `int main(void)` — but 'according to the C Standard' is overstating your case. – Jonathan Leffler Feb 05 '20 at 22:46
  • @JonathanLeffler I am sure that it is a standard defect because it contradicts the Standard itself. – Vlad from Moscow Feb 05 '20 at 22:57
  • C11 [§5.1.2.2.1](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1) says: "or equivalent", and the empty parentheses in `int main()` is basically equivalent to `int main(void)` except that the latter gives a prototype to `main()` and the former does not. See also: [§6.7.6.3 ¶20](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p20) — if it is an error, it occurs twice (both times in non-normative examples). – Jonathan Leffler Feb 05 '20 at 22:59
  • @JonathanLeffler There is a footnote "10)Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on" – Vlad from Moscow Feb 05 '20 at 23:01
  • Yes, and the 'and so on' allows for `int main()`. At least, that's a plausible interpretation. – Jonathan Leffler Feb 05 '20 at 23:02
  • @JonathanLeffler It is difficult to say that "so on" means int main(). – Vlad from Moscow Feb 05 '20 at 23:03
  • The combination of "or equivalent" and "and so on" allows for that. (The [Foreword ¶8](http://port70.net/~nsz/c/c11/n1570.html#Forewordp8) states that notes, footnotes, and examples are non-normative.) – Jonathan Leffler Feb 05 '20 at 23:07
  • @JonathanLeffler I can not agree that "so on" allows this because the provided examples of main suppose that main has a prototype. – Vlad from Moscow Feb 05 '20 at 23:10
  • Then we'd best agree to disagree. Be aware that the standard includes examples of `int main()`. Be cautious about being dogmatic. By all means advocate for `int main(void)` — I agree that it is much better. But I cannot agree that the standard precludes `int main()`. See also [What should `main()` return in C and C++?](https://stackoverflow.com/a/18721336/15168) – Jonathan Leffler Feb 05 '20 at 23:11
  • @JonathanLeffler Yes I saw. It is a defect at least the Standard shall clarify the "so on".:) – Vlad from Moscow Feb 05 '20 at 23:15
0

That isn't doing exactly what you think it is. In your for loop, each time you write check_for_value(), it is going to call that function. So it will call it the first time and the return value will not matter. It will call it again for the middle statement and then the value will matter because you are comparing the output to not equal to 1. And then again it will call the function in the third statement, where again it won't matter. Usually for something like this, you would use a while loop instead. An example below:

int ret = check_for_value();
while(ret != 1) {
    printf("Failed\n");
    ret = check_for_value();
}

printf("Success\n");

Technically a for loop can work too as the following:

for(int ret = check_for_value(); ret != 1; ret = check_for_value()) {
    printf("Failed\n");
}
Warpstar22
  • 411
  • 3
  • 11
  • That replacement can be simplified to `while (check_for_value() != 1) printf("Failed\n");`, can't it? – Jonathan Leffler Feb 05 '20 at 22:15
  • Well yes but I think this is easier for a beginner to wrap their head around. Shorter code doesn't always mean it's more easily readable. Besides, if the compiler is smart, then it will do effectively the same. – Warpstar22 Feb 05 '20 at 23:33
  • Hmmm — I guess I think the shorter code _is_ easier to understand, but I've been coding in C for long enough (over 35 years) that any guesses I make on "easier for beginners" has to be treated with a pinch of salt. – Jonathan Leffler Feb 05 '20 at 23:37