Vector output is strange ☺ ☻ ♥ ♦ ♠ ect

I'm a new comp sci and C++ student and I'm writing a program on my own for fun and to teach myself some basic C++ coding skills. Anyways I'm trying to write a program that will solve sudoku puzzles. I've written three functions so far. One to display the 9x9 grid, another for the user to input the initial values and another to add values to the puzzle (just the first row, I have to walk before I can run). The programs compiles fine but when it reaches the point where the computer inputs number into the puzzle board I run into a problem. Instead of entering 1 to 9 it enters ☺ ☻ ♥ ♦ ♠.

Any help is greatly appreciated.


My code:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// global constants
const char EMPTY = ' ';

// function prototypes
void displayBoard(const vector<char>& tempVector);
void addNumber(vector<char>& tempVector);
void rowAlgorithium(vector<char>& tempVector);

int main(int argc, char *argv[])
{
int numbers;
string more;
const int NUM_SQUARES = 81;
vector<char> board(NUM_SQUARES, EMPTY);

cout << "How many numbers do you wish to enter: ";
cin >> numbers;

do
{
for (int i = 1; i <= numbers; i++)
{
displayBoard(board);
addNumber(board);
displayBoard(board);
}
cout << "Do you want to enter any more numbers?: ";
cin >> more;
}while (more == "y");

// algorithium the easy part.

rowAlgorithium(board);
displayBoard(board);

system("PAUSE");
return EXIT_SUCCESS;


}


void displayBoard(const vector<char>& tempVector)
{



cout << "\n";
for(int j = 0; j < 9; j++)
{
for(int i = 0;i < 9; i++)
{cout << tempVector[i+(j*9)] << " | ";
}
cout << "\n" << "------------------------------------\n";
}
}

void addNumber(vector<char>& tempVector)
{
int where;
char number;
// int move = askNumber("Which square has a number", (board.size() - 1));
// while (!isLegal(move, board)) add later
//{
// cout << "\nThat square is already occupied, foolish human.\n";
// move = askNumber("Where will you move?", (board.size() - 1));
// }
// cout << "Fine...\n";
cout << "\nWhere is the number you wish to enter: ";
cin >> where;
cout << "\nWhat is the number: ";
cin >> number;
tempVector[where] = number;
}

// row algorithium

void rowAlgorithium(vector<char>& tempVector)
{



//row 1

char j = 0;
for(int i = 0; i < 9; i++)
{
if (tempVector[i] == ' ')
{
tempVector[i] = j;
}
j++;
}
}
Last edited on
The problem lies in the rowAlgorithium function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void rowAlgorithium(vector<char>& tempVector)
{
    //row 1

    char j = 0;
    for(int i = 0; i < 9; i++)
    {
        if (tempVector[i] == ' ')
        {
            tempVector[i] = j; //you are putting a decimal value (such as 0) rather than  it's ascii value.
        }

        j++; 
    }
}


try this instead:
Remember that the ascii char values for numeric digits start at hex 30 (48 decimal)

tempVector[i] = j+48

That might solve the funny characters problem (but that algorithym need a fair bit of work).
One of the annoying features of streams is that they treat char/signed char/unsigned char types as characters rather than as integers (and there is no way to change that). guestgulkan's solution definitely works, though I don't know if it is more convenient to the rest of the program to have the vector store ASCII digits as opposed to their integer equivalents (I have not looked at the code).

As an alternative, the following would also work:

1
2
3
4
for(int i = 0;i < 9; i++)
{
    cout << static_cast<unsigned>( tempVector[i+(j*9)] ) << " | ";
}


(note the only change I made was the static_cast.)
Thanks for the help.

I tried out jsmith's idea which fixed the output for the rowAlgorithium function but now it's displaying ascii values for the empty spaces and the numbers the user inputs.

first two row output when user enter 5 into the 5th ( first row, 6th column) vector space

1 | 2 | 3 | 4 | 5 | 53 | 7 | 8 | 9 |
------------------------------------
32 | 32 | 32 | 32 | 32 | 32 | 32 | 32 | 32 |

What I don't get is why "tempVector[i] = j;" inputs a decimal and "tempVector[where] = number" inputs an ascii value?

tempVector[i] = j; //you are putting a decimal value (such as 0) rather than it's ascii value.

tempVector[where] = number;
Basically what happens is this:

1
2
char ch = ' ';
ch = 22; //this will set it to ASCII character 22 


When you have tempVector[i] = j, and since your j is always = to 0, your are putting ASCII character 0.
Topic archived. No new replies allowed.