0

the following program is not complete yet so don't judge on that.(I haven't made the win condition yet etc)

#include <stdio.h> //Date:17/10/2022, By Raunak Shahi
int main(void)
{
    int i=0;
    int coord=0;
    char c1,c2,c3,c4,c5,c6,c7,c8,c9='';
    char input='';
    double a;
    printf("Here's the number assigned to each cell:\n"); //Showing the user the coordinates of each cell through the demo table
    printf(" ___________ \n");
    printf("| 1 | 2 | 3 |\n");
    printf("|___|___|___|\n");
    printf("| 4 | 5 | 6 |\n");
    printf("|___|___|___|\n");
    printf("| 7 | 8 | 9 |\n");
    printf("|___|___|___|\n");
    for(i=1;i<=9;i++){ //Printing the table each turn until the game is finished
        if(i%2==0){
            input='O';
            printf("Nought's turn, enter a coordinate:");
        }else{
            input='X'; 
            printf("Cross's turn, enter a coorinate:");
        }
        scanf("%lf",a);
        coord=(int)a;
        switch(coord){
            case 1:
              c1=input;
              break;
            case 2:
              c2=input;
              break;
            case 3:
              c3=input;
              break;
            case 4:
              c4=input;
              break;
            case 5:
              c5=input;
              break;
            case 6:
              c6=input;
            case 7:
              c7=input;
              break;
            case 8:
              c8=input;
              break;
            case 9:
              c9=input;
              break;
             }
        printf(" ___________ \n");
        printf("| %s | %s | %s |\n", c1, c2, c3);
        printf("|___|___|___|\n");
        printf("| %s | %s | %s |\n", c4, c5, c6);
        printf("|___|___|___|\n");
        printf("| %s | %s | %s |\n", c7, c8, c9);
        printf("|___|___|___|\n");
    }
    return 0;
}

When I run it, it asks me the coordinate for cross, and then nothing happens, even though it should print the table and then ask nought player for their move and I do not understand why, I'm a beginner please help!

Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • 2
    Note that `char c1,c2,c3,c4,c5,c6,c7,c8,c9='';` will *only* initialize `c9`. All the other variables defined on the same line will stay uninitialized and with indeterminate values. – Some programmer dude Oct 26 '22 at 07:18
  • 2
    And it seems you should use an *array* instead of nine distinct variables. Like `char c[9];`. – Some programmer dude Oct 26 '22 at 07:19
  • Also, why do you read input as a *floating point* value? What is the reason for that? – Some programmer dude Oct 26 '22 at 07:19
  • @Someprogrammerdude I see, Imma initialize all of them and then see. I'm using floating point just so if someone enters invalid inputs, the program still works as I've had similar issues on other programs. – Raunak Shahi Oct 26 '22 at 07:27
  • Also note that `''` is not a valid character literal, did you mean `' '`? – Alan Birtles Oct 26 '22 at 07:38
  • @RaunakShahi *I'm using floating point just so if someone enters invalid inputs, the program still works* -- No, you use `std::string` for that, where your code validates the input. What if the user enters "abc"? The way input verification works is to allow anything, and *write the code* to check if the input is valid. To allow any data, the input is usually put into a `std::string` to be verified by the program itself. – PaulMcKenzie Oct 26 '22 at 07:45
  • Regarding the input as float, that won't be able to handle input of characters anyway. My recommendation when it comes to input and input-validation is to forget that `scanf` exists (it's not very C++ any way). Instead read lines using `std::getline` and then do conversion with validation using e.g. `std::stoi`. – Some programmer dude Oct 26 '22 at 07:50
  • And what source are you using to learn C++? Because the code you show is really plain C, not anything specific to C++ at all. Please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn C++ properly. – Some programmer dude Oct 26 '22 at 07:52
  • The most fundamental reason your switch statement doesn't work is that _you aren't passing a pointer to your scanf call_ so you aren't actually setting your `a` to **anything at all.** Should be `scanf("%lf",&a);` That said, casting a float or double to an int is a recipe for disaster ; ) – mmixLinus Oct 26 '22 at 11:57

1 Answers1

0

Without changing your structure too much, here's a version that works (tested with onlinegdb.com):

#include <stdio.h> //Date:17/10/2022, By Raunak Shahi
int main(void)
{
    int i=0;
    int coord=0;
    char c[9];
    for (int i = 0; i < 9; i++) c[i] = ' ';
    char input=' ';
    printf("Here's the number assigned to each cell:\n"); //Showing the user the coordinates of each cell through the demo table
    printf(" ___________ \n");
    printf("| 1 | 2 | 3 |\n");
    printf("|___|___|___|\n");
    printf("| 4 | 5 | 6 |\n");
    printf("|___|___|___|\n");
    printf("| 7 | 8 | 9 |\n");
    printf("|___|___|___|\n");
    for(i=1;i<=9;i++){ //Printing the table each turn until the game is finished
    
        if(i%2==0){
            input='O';
            printf("Nought's turn, enter a coordinate:");
        }else{
            input='X'; 
            printf("Cross's turn, enter a coorinate:");
        }
        scanf("%d",&coord);
        c[coord-1] = input;
        printf(" ___________ \n");
        printf("| %c | %c | %c |\n", c[0], c[1], c[2]);
        printf("|___|___|___|\n");
        printf("| %c | %c | %c |\n", c[3], c[4], c[5]);
        printf("|___|___|___|\n");
        printf("| %c | %c | %c |\n", c[6], c[7], c[8]);
        printf("|___|___|___|\n");
    }
    return 0;
}
mmixLinus
  • 1,646
  • 2
  • 11
  • 16