-1

Ok, so this function is supposed to take results from a user and then give them an option to input more results or return to the main menu main() when they're done.

Everything works fine. the only problem is at the last scanf statement before the switch.

Instead of waiting for the users input in userChoice, its automatically starting the function over again. Its only supposed to do that if userChoice is 'a' or 'A' but it just completely ignores the scanf statement and the switch as well, it seems. What am i doing wrong? Scroll down to see the line im referring to.

void inputResults(int gameNumber, int i, int j)
{


    int Results[ROWS][COLS];
        char userChoice = ' ';
        if (j >= COLS)
        {
            j = 0;
            i = i + 1;
        }
        if (gameNumber > ROWS)
        {
            printf("\nJk, Max number of games reached\n");
            main();
        }

        gameNumber = gameNumber + 1;
        printf("\nHow many points did the home team score in game %d?\n", gameNumber);
        scanf_s("%d", &Results[i][j]);
        j = j + 1;
        printf("\nHow many points did the opponents score in game %d?\n", gameNumber);
        scanf_s("%d", &Results[i][j]);
        printf("\nHome: %d  Opponents: %d\n", Results[i][j-1], Results[i][j]);
        printf("\nA) Enter results for game %d\nB) Main menu  \n", (gameNumber + 1));

*************************PROBLEM***********************************

        scanf_s("%c", &userChoice); /*it ignores THIS it just loops the 
                                    function after it prints the above*/

        switch (userChoice) //it doesnt even get to this.
        {
        case 'A':
            inputResults(gameNumber, i, j);
        case 'a':
            inputResults(gameNumber, i, j);
        case 'b':
            main();
        case 'B':
            main();
        default:
            printf("\nThats still not  a valid choice dude\n");
        }

}

the values passed into gameNumber, i, and j are all all 0 if you're wondering. They were passed from the main function

  • 1
    you [can not call `main()`](https://stackoverflow.com/questions/4238179/calling-main-in-main-in-c) function in C++ code, apparently... Plus you are using `scanf_f()` [wrong](https://stackoverflow.com/questions/41199504/reading-a-character-with-scanf-s). – C.M. Sep 09 '18 at 00:16
  • Are you programming in C or C++? Your entire program is written in C, including the explicit call to `main()`, which is not allowed in the C++ standard. – eesiraed Sep 09 '18 at 00:18
  • @C.M. How does one use scanf_f right? cause when i type scanf_f instead of scanf_s it says unresolved external symbol scanf_f referenced in function – Mixtape Mania Sep 09 '18 at 00:20
  • *Everything works fine.* -- This is not legal code. You cannot call `main()` directly. – PaulMcKenzie Sep 09 '18 at 00:21
  • @MixtapeMania did you try reading links I've given you? – C.M. Sep 09 '18 at 00:21
  • @PaulMcKenzie My mistake, i am programming in C, because i am able to call main() functions. – Mixtape Mania Sep 09 '18 at 00:25
  • Also, you probably want `break`s in each of your `case`s. Otherwise, every subsequent `case` will be executed after the selected one. (Google `case switch fallthrough`.)And don't call `main()`. – 3Dave Sep 09 '18 at 00:26
  • @MixtapeMania -- Why even resort to calling `main()`? Just write a normal `while` loop. Calling `main` is an indication you're missing the point of proper looping structures such as `do`, `for`, or `while`. – PaulMcKenzie Sep 09 '18 at 00:27
  • @PaulMcKenzie I can try just using a for loop for this function. But will that solve the problem of the scanf char value? – Mixtape Mania Sep 09 '18 at 00:32
  • @MixtapeMania -- The comment section is for comments, so I commented on your code. The answer section is designated for answers. – PaulMcKenzie Sep 09 '18 at 00:33

1 Answers1

0

When you read a single character with scanf_s like

char ch;
scanf_s("%c", &ch, 1);

and enter for eg. the letter 'A' and press enter, the 'A' ends up in ch but the '\n' from you pressing enter remains in stdin. On the next call to scanf_s() this '\n' is read and stored in ch without giving you the chance of making any input.

Use

scanf_s("%c[^\n]", &ch, 1);

instead which will read and discard all extra characters still in stdin up to the next newline character.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • Thanks, this solution worked. It appears there are more issues but i'll go trial and erroring, it at least fixed the scan_f issue. – Mixtape Mania Sep 09 '18 at 00:37