0

I have a script here that I made and im having trouble with the do-while loop. I can either get it to run infinitely (continue) or only once (break). Maybe there's something wrong with the conditions I've set... I'm at a loss here, so any help would be greatly appreciated

#include <stdio.h>
#include <time.h>

int main ()
{
    int age;
    char string[50];
    char string2[4];
    int yearsleft;

    /*
    FILE *fp;


    fp = fopen( "RetireeFileForCo.txt", "a+" );

    */

    do {

        printf( "What is your name? \n");

        scanf( "%s", string );

        printf( "Hello %s\n", string );


        printf ("What is your age? \n");

        scanf ("%d", &age);

        printf ("You enter %d\n", age );


        if ( age  > 65 ) {

            printf (".... You should already be retired %s\n", string );

//  fputs ( ".... You should already be retired %s\n", fp, string );


        } else {
            yearsleft = 65 - age ;
            printf ("Your number of years left until retirement is  %d\n", yearsleft);
        }

        /*
        fputs ( "As of the date above.... You should already be retired %s\n",       fp );}}


        fclose (fp);
        */
        printf( "Do you want to check another person's status? (yes or no) \n");

        scanf( "%s", string2 );
        if("string2 = yes") {
            continue;
        }
        printf ("Thank you for your input\n");
    }
    while("string2 = yes");
    return 0;
}
mschuurmans
  • 1,088
  • 1
  • 12
  • 24
  • 5
    Please post your code here, rather than an image of your code. – John Bode Nov 19 '18 at 14:33
  • 6
    Post code as **text**, not as a picture of text and not a link to a picture of text. Also, C code is not a script but a compiled language. – dbush Nov 19 '18 at 14:33
  • 4
    What script? There are no scripts. Only a C program in an image. – DeiDei Nov 19 '18 at 14:34
  • 2
    Your condition is `while("string2 = yes")` which is equivalent to "repeat while the memory address of the string `string2 = yes` is not the null pointer", which is unlikely to be what you want, and always true. – Arkku Nov 19 '18 at 14:38
  • Possible duplicate of [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) – klutt Nov 19 '18 at 14:58

1 Answers1

6

The statements if ( "string2 = yes" ) and while ( "string2 = yes" ) are indeed the problems. "string2 = yes" is simply a string, and in a boolean context such as above evaluates to true (or, more accurately, not false).

To compare string values, you have to use the library function strcmp (neither the = assignment nor == comparison operators are defined for strings or any other array expressions):

if ( strcmp( string2, "yes" ) == 0 ) // strcmp returns 0 if the arguments are equal
{
  // do something if string2 is equal to yes
}

do
{
  // loop at least once, repeat if string2 is equal to "yes"
} while( strcmp( string2, "yes" ) == 0 );

You could also write those as if ( !strcmp( string2, "yes" ) ) and while ( !strcmp( string2, "yes" ) ).

John Bode
  • 119,563
  • 19
  • 122
  • 198