Home > Net >  How do I add an infinite loop in C?
How do I add an infinite loop in C?

Time:03-31

I have to make this an infinite loop and also make it go back to the start when a wrong character is an input. What to do?

This is my code so far:

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

int main ()
{
// Prompt user for input last choice contender
   char c;
   printf("Last choice: ");
   c = getchar();

      // If last choice is R, loser should play S
    if (c == 'R'|| c == 'r')
    {
        printf("Your next move: S");
    }
// Else if last choice is P, loser should play R
    else if (c == 'P' || c == 'p')
    {
        printf("Your next move: R");
    }
// Else if last choice is S, loser should play P
    else if (c == 'S' || c == 's')
    {
        printf("Your next move: P");
    }
// TO DO: If another character start over
    else if (c != 'S'|| c != 's' || c != 'P' || c != 'p' || c != 'R'|| c != 'r')
}
// TODO: make an infinite loop / start over

CodePudding user response:

I there are multiple ways of doing this, i recommend a while loop.

Add a while(){

} around all the code you want to reiterate, inside the while() parameters you can have a boolean like this boolean won = false, while(!won) and add the won = true when a player has won, or when you dont want the player to continue your game! But i also recommend reading about the while loop, google while loop in google

CodePudding user response:

If I do understand correctly your need, you can do like that :

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

int main ()
{
    while(true) // or while(1) if you don't want to include stddef or any other lib containing true and false declaration
    // Prompt user for input last choice contender
    {
        char c;
        printf("Last choice: ");
        c = getchar();

        // If last choice is R, loser should play S
        if (c == 'R'|| c == 'r')
        {
            printf("Your next move: S");
        }
        // Else if last choice is P, loser should play R
        else if (c == 'P' || c == 'p')
        {
            printf("Your next move: R");
        }
        // Else if last choice is S, loser should play P
        else if (c == 'S' || c == 's')
        {
            printf("Your next move: P");
        }
        // TO DO: If another character start over
        else if (c != 'S'|| c != 's' || c != 'P' || c != 'p' || c != 'R'|| c != 'r')
    }
}

Also, you don't need to do anything else for your code to restart at the beginning of the loop if no authorised character have been met. You don't even need to catch them with your latest else if clause, since, arriving at the end of code in the loop anyway, nothing more will be done once arrived there. (else if (c != 'S'|| c != 's' || c != 'P' || c != 'p' || c != 'R'|| c != 'r')) But, if you wish to keep your empty (no sub {}) else if clause, you should add a ; at the end of the clause, or else I don't know how you current code would compile.

I welcome you to stack overflow, but I advise you to follow some basic guide on c language, it looks like you are missing some serious basis that will keep blocking you on your current learning road. Overall, I encourage you to pursue your learning :) Good luck !

  • Related