I am attempting to build a Tic Tac Toe game in C++, however, I am very beginner in C++ but can code reasonably confidently in Python.
This is my code so far:
#include <iostream>
using namespace std;
char matrix[3][3] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
void Draw()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main()
{
Draw();
return 0;
}
There is a problem when the programme gets to the 4th item in the list at line 3.
E0146 - too many initializer values
C2078 - too many initializers
As such, the programme obviously won't run. Any help would be greatly appreciated as I have googled around and not been able to find an answer.
Thanks
Have tried:
char *matrix[3][3] etc:
matrix[3][3] etc:
Okay, so I didn't know that there was a difference between " " and ' '. This has solved the problem in case anyone else runs into something like this.
Related
The basic problem is that we (in pairs) have been tasked with creating a program to - amongst other things - read in a txt file and display it as a sudoku board as part of our introduction to coding at Uni.
We have completed most of the code and have got it working sufficiently well. However, we are struggling with what I would consider to be a basic aspect of this. The code reads in the file and saves it to the classes private char 9*9 matrix. We have run a debugging print-out to check that the values are being saved to the matrix and this is indeed the case. The issue is with the getMatrixVal(int x, int y) function which we think is returning a pointer rather than the character. We have tried returning a pointer from the function and have tried saving to a pointer and then to a char variable. Unfortunately, we am not excellent on pointer's.
With the recent suspensions to University timetables, we are temporarily unable to get the advice and support we require and, hence, we are applying here for advice.
Also, we are using the qt programming software on a raspberry pi - could this have anything to do with our problems?
#include "grid.h"
#include <QDebug>
#include <QChar>
#include <QFile>
grid::grid(char matrix_value, const int x, const int y) {
matrix[x][y] = matrix_value;
//char c = matrix[x][y];
//qDebug() << QChar(c);
}
void grid::load_file() {
QFile file("/home/pi/Documents/ELEC1204/P6/SUDOKU.txt");
char character, c;
int i, j;
if(!file.open(QFile::ReadOnly)) {
qDebug() << "Error in opening file";
return;
} //Print error if message if unable to open file
for(i = 0; i < 9; i++) {
for(j = 0; j < 9; j++) {
SKIP: //Related to 'goto'
file.read(&character, sizeof(char)); //Read ini individual characters
if(character == '\xd' || character == '\xa')
goto SKIP;
//Skip character related to new lines
//Goes to 'SKIP' point in program
else if(character == 'X')
grid('_', i, j);
//Save 'X's as '_'
else
grid(character, i, j);
//Save numbers as they are
c = getMatrixVal(i, j);
qDebug() << "i = " << i << "| j = " << j << "| Character is: " << QChar(character) << "| Matrix character is: " << QChar(c);
//Print-out for debugging
}
}
file.close();
//Close file
}
// Possibly useful source:
// https://doc.qt.io/qt-5/qfile.html
// https://forum.qt.io/topic/60240/reading-file-byte-by-byte/5
char grid::getMatrixVal(const int x, const int y) {
char c = matrix[x][y];
//qDebug() << "Matrix[" << x << "][" << y << "] is: " << QChar(c);
return c;
}
void grid::printMatrix() {
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
qDebug() << matrix[i][j];
}
}
}
//Prints out matrix
//Debugging function
There is nothing wrong with getMatrixVal: it returns a character from the right place in matrix.
Your call grid(character, i, j) constructs a new grid instance that is unrelated to the current one.
Instead, just assign directly to matrix: (or use a setter function)
matrix[i][j] = character;
For some unknown reason this simple code runs, does what it's expected to do and then crashes the run. I am using NetBeans IDE, which overlapped my arrays before (tends to be buggy), so I was wondering if someone gets the same error - that would mean I certainly have to change the IDE environment.
#include <iostream>
using namespace std;
int main ()
{
int first[4][4];
for (int a = 0; a < 5; a++)
{
for (int b = 0; b < 5;b++)
{
cout << a << " " << b << " ";
if (first [a][b] != 0)
{
first[a][b] = 0;
}
cout << first[a][b] << " ";
}
cout << endl << endl << endl;
}
return 0;
};
here you are declearing a array with 4 indexes.In c/c++ index number starts at 0.
In your code you are saying :
int first[4][4];
that means indexs are : 0 1 2 3.Array length or total index are 4.
But in for loop you are saying
for (int a = 0; a < 5; a++) {
....
}
so you are trying to access index number 0 1 2 3 4 respectively.But remember you don't have index number 4.That is why it should give array index out of bound error.
Also at the end of main function you are using a semicolon.remove that
main () {
....
};
Hope this solves the problem.From next time Please try to provide details about the errors your IDE is giving you as it will be easier for the people who are giving answer.
My apologies if this question looks simple. I'm still learning about threads. I already tried searching for a solution to this on here but didn't find any.
I'm trying to get my program to create a number of threads based on user input (ex: "cin >> 5" will create 5 threads) but it says the "i" in "threads myThreads[ i ]" needs to be a constant value. The code is below:
void exec(int n)
{
cout << "Thread " << n << endl;
}
int main()
{
int numThreads = 0;
// create threads
cin >> numThreads;
thread myThreads[numThreads]; // this part says myThreads "must be a constant value"
for (int i = 0; i < numThreads; i++)
{
myThreads[i] = thread(exec, i);
}
for (int i = 0; i < numThreads; i++)
{
myThreads[i].join();
}
cout << "Done!" << endl;
}
Any ideas as to how that section can be fixed? I've tried a few different ways but they haven't worked so far. Thank you very much.
There's no problem with multithreading. The problem is static array that you using as dynamic array.
Try something like this:
thread* myThreads = new thread[numThreads];
More about dynamic memory in C++:
http://www.cplusplus.com/doc/tutorial/dynamic/
UPD By James Adkison:
Do not forget to delete[] your array to avoid memory leaking.
You may have seen in many places "Loading..." where the 3 dots(or more), viz. "...", first appear one by one, then after displaying all the dots, they disapperar and once again appear one by one (so, total 2 times).
To elaborate:-
Stage 1:
Loading.
Stage 2:
Loading..
Stage 3:
Loading...
Then, it repeats second time! and then terminate!
So, for this I prepared a C++ program and its source code is:
#include <iostream.h>
#include <time.h>
#include<dos.h>
int main()
{
cout << "Loading";
cout.flush();
for (;;) {
for (int i = 0; i < 3; i++) {
cout << ".";
cout.flush();
sleep(1);
}
cout << "\b\b\b \b\b\b";
}
return 0;
}
This program is not terminating. It doesn't stops! How can I edit this to make this terminate?
Please post codes supported by Turbo C++ Compiler, as I am not too much aware of the ANSI C++!! :P
Gguys, please help me out? :)
Thanks, in advance! :)
Why not delete those points using a backspace?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
cout << "Loading";
cout.flush();
for (;;) {
for (int i = 0; i < 3; i++) {
cout << ".";
cout.flush();
sleep(1);
}
cout << "\b\b\b \b\b\b";
}
return 0;
}
I'd go for the following solution using "carriage return":
for (int j = 0; j < 3; j++) {
cout << "\rLoading \rLoading";
for (int i = 0; i < 3; i++) {
cout << ".";
sleep(300);
}
}
This overwrites the current line (with all dots) and repostions cursor at the end of "Loading".
BTW, the sleep value here is in milliseconds. You may have to change that if yours is in seconds.
Also, in your program you have nested loops both with running i - I doubt that this will work as intended...
I'm trying to call a function with no return type but it doesn't seem to be getting called.
The code looks something like this(summarized):
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int ItemsInQuestion[4];
void GetQuestions(int NumQuests);
int main()
{
int NumberOfQuestions = 0;
srand((unsigned)time(NULL));
cout << "How many questions would you like?" << endl;
cin >> NumberOfQuestions;
cout << NumberOfQuestions << " questions will be asked.";
GetQuestions(NumberOfQuestions);
system ("PAUSE");
return 0;
}
void GetQuestions(int NumQuests)
{
for(int Questions=NumQuests; Questions>NumQuests; Questions++)
{
ItemsInQuestion[0]=(rand()%(263))+1;
ItemsInQuestion[1]=(rand()%(263))+1;
ItemsInQuestion[2]=(rand()%(263))+1;
ItemsInQuestion[3]=(rand()%(263))+1;
cout << ItemsInQuestion[0] << ' ' << ItemsInQuestion[1] << ' ' <<ItemsInQuestion[2] << ' ' << ItemsInQuestion[3];
}
}
The line that outputs the values in the array never comes up. What is causing this?
Because
int Questions=NumQuests;
and
Questions>NumQuests;
don't like each other.
You set Questions to NumQuests and then tell the loop to keep going as long as Questions is strictly greater than NumQuests, which it isn't to start off with.
Even if it was, you'd soon run into an overflow and undefined behavior.
This is not the way of using for-loops :
for ( __Initialization, __Condition, __Increment)
As Grigore pointed out in his answer, your initialization is wrong. As Questions=NumQuest, the statement Questions>NumQuests is false from the beginning, and your code is equivalent to
for ( ; 1<1 ; ) // I'm a lazy loop, I'm ugly and useless
There is an infinite number of way to do what you want :
// Direct : 0, 1, 2, .. NumQuest-1.
for (int Questions=0 ; Questions < NumQuests ; Questions++)
{ ... }
// Reverse : NumQuest, ..., 2, 1.
for (int Questions=NumQuests ; Questions > 0 ; Questions--)
{ ... }