Switch Case Problem

Hey everybody!

I've been working on a Sudoku program these past couple of weeks, and I almost have it complete. I'm just experiencing one last problem. In my program there is a menu that prompts the user if they'd like to display the game board, edit a square, show possible values and quit and save the game. The program works fine up until the quit and save case.

It comes out like this

> > E
> What are the coordinates of the square: B2
>
> What is the value at 'B2': 9
>
>
Q

When it should come out like this

> > E
> What are the coordinates of the square: B2
>
> What is the value at 'B2': 9
>
> > Q

I'm been going over my code for a few hours now, and just can't figure what's wrong. I was hoping to get a fresh pair of eyes to look at it.
Here is the function for this part of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void interact(int board[][COLS])
{
   char option;
   string filename;
   displayOptions();
   cout << endl;
   display(board);
      do
      {
         cout << "> ";
         cin >> option;
         switch (option)
         {
            case '?':
               displayOptions();
               cout << endl;
               break;
            case 'D':
            case 'd':
               display(board);
               break;
            case 'E':
            case 'e':
               editSquare(board);
               break;
            case 'S':
            case 's':
               showPossibleValues(board);
               break;
            case 'Q':
            case 'q':
               getSaveFilename(filename);
               break;
            default:
               cout << "ERROR: Invalid command\n";
               break;
         }
      } while (toupper(option) != 'Q');
}
Hello SJDub,

Welcome to the forum.

You have a good intention, but I can not see anything wrong with the function that you have posted.

What needs to be seen is any function that comes from choosing "Q" starting with the "getSaveFilename" function and any function that is called from there.

The problem is that you get focused on one part of the program thinking that the problem is there when it is three functions away and you did not consider that because your focus was in the wrong place. It is like compiler error messages, The message will point to a line number that is a problem, but it is actually the line above where the problem starts.

I realize you may not want to post your entire code yet and that is OK I understand, but you do need to show the functions that are involved in writing to a file.

Hope that helps,

Andy
Hello SJDub,

Sometimes the little things are the hardest to see. You want your screen to display this:
> > E
> What are the coordinates of the square: B2
>
> What is the value at 'B2': 9
>
> > Q

If this is what you are looking for than say so on line 10 cout << "> "; the prompt should be: cout << ">> ";. If that is not what you need than I need more to go on.

Hope that helps,

Andy
Last edited on
what's causing the leftmost sets of > ? Something got it to add another newline.

working stub at https://repl.it/repls/IdolizedGrossOutlier to show the part posted seems ok:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
using namespace std;

void display(int k) {   cout << "board display\n"; }
void displayOptions() { cout << "options\n"; }
void editSquare(int k){ cout << "edit square\n"; }
void showPossibleValues(int k){ cout << "possible values\n"; }
void getSaveFilename(string s){ cout << "get save filename\n"; }

int main() 
{
    int board = 42;
    string filename = "blah";
    
    char option;
    cout << endl;
    display(board);
    do
    {
        cout << "> ";
        cin >> option;
        switch (option)
        {
        case '?':
           displayOptions();
           cout << endl;
           break;
        case 'D':
        case 'd':
           display(board);
           break;
        case 'E':
        case 'e':
           editSquare(board);
           break;
        case 'S':
        case 's':
           showPossibleValues(board);
           break;
        case 'Q':
        case 'q':
           getSaveFilename(filename);
           break;
        default:
           cout << "ERROR: Invalid command\n";
           break;
        }
    } while (toupper(option) != 'Q');
}
Topic archived. No new replies allowed.