I have 2 QCheckbox tables, each contains 11 elements.
I declare them as following in my class :
QCheckBox *sectionTable[10];
QCheckBox *postTable[10];
For each QCheckBox, I do this
QCheckBox* checkboxA = new QCheckBox("A");
sectionTable[0] = checkboxA;
Through my test method, I would like to return the content of each element of my QCheckbox tables.
To do so, I've done this test :
/** TEST() **/
void VGCCC::test()
{
sectionTable[0]->setText("A");
sectionTable[1]->setText("B");
sectionTable[2]->setText("C");
sectionTable[3]->setText("D");
postTable[0]->setText("E");
postTable[1]->setText("F");
postTable[2]->setText("G");
postTable[3]->setText("H");
int i=0;
do
{
m_testTextEdit->insertPlainText(sectionTable[i]->text());
std::cout << "SECTION TABLE " << sectionTable[i]->text().toStdString() << "\n" << std::endl;
i++;
}
while(!sectionTable[i]->text().isNull());
do
{
m_testTextEdit->insertPlainText(postTable[i]->text());
std::cout << "POST TABLE " << postTable[i]->text().toStdString() << "\n" << std::endl;
i++;
}
while(!postTable[i]->text().isEmpty());
}
My application is compiling, and also running. But when I call the test function, my application crash.
How can we explain this problem ?
I would like to notify that I get a result in my console. It seems my test is half working, but is crashing at the end of the 1st do/while loop, when I get out of my condition.
With regard to the 11 elements: QCheckBox *sectionTable[10]; defines only 10 slots (0 through 9) for elements.
int i=0;
do
{
m_testTextEdit->insertPlainText(sectionTable[i]->text());
std::cout << "SECTION TABLE " << sectionTable[i]->text().toStdString() << "\n" << std::endl;
i++;
}
while(!sectionTable[i]->text().isNull());
Has the potential to reach past ten or eleven elements. Unless the terminating condition is found earlier, there is nothing to stop sectionTable[i] from trying to read sectionTable[11] to call its text method. If it manages to survive the call to the out-of-range sectionTable[11]->text(), it will then try calling sectionTable[11]->text().isNull(). Possibly this will be survivable as well and not be NULL. In this case sectionTable[12] will be tested. This will continue until the program hits really bad memory and crashes, a null is found, or pigs become the terror of the airways we all know they truly wish to be.
Note that i is not set back to 0 after this loop, so the first postTable to be inspected in the next loop will be at the same index as the last sectionTable.
So if sectionTable[5]->text().isNull() was NULL, postTable[5] will be the first postTable indexed and inspected.
do
{
m_testTextEdit->insertPlainText(postTable[i]->text());
std::cout << "POST TABLE " << postTable[i]->text().toStdString() << "\n" << std::endl;
i++;
}
while(!postTable[i]->text().isEmpty());
This loop has the same error in the exit condition as the sectionTable loop.
I find out how to solve the problem. As said in the answer before (#user4581301), I didn't set back my iterator i to 0.
Also, to avoid the "out of range" crash, I put a second condition which is i<sizeof(sectionTable[i]);
This is my fonctional test function :
/** TEST() **/
void VGCCC::test()
{
int i = 0;
do
{
m_testTextEdit->insertPlainText(sectionTable[i]->text());
std::cout << "SECTION TABLE " << m_materialMap[sectionTable[i]].c_str() << "\n" << std::endl;
i++;
}
while(!sectionTable[i]->text().isNull() && i<sizeof(sectionTable[i]));
i = 0;
do
{
m_testTextEdit->insertPlainText(postTable[i]->text());
std::cout << "POST TABLE " << postTable[i]->text().toStdString() << "\n" << std::endl;
std::cout << "POST TABLE " << m_materialMap[postTable[i]].c_str() << "\n" << std::endl;
i++;
}
while(!postTable[i]->text().isEmpty() && i<sizeof(postTable[i]));
}
Related
I have a recursive display function, meant to go through the values of array b and print the details. My function is successful in looping the correct amount of times, but only prints out the value at index 0. For example, if I have 3 books, it prints out the first book 3 times and is not going to the other values. I am a beginner programmer and I believe I am missing something very simple or obvious, but any help is appreciated.
void displayBooks(int n)
{
// Write the code below
if (n >= currentCount) {
return;
}
else {
cout << "Name: " << b->getName() << "\n";
cout << "Name of Book Author: " << b->getAuthor() << "\n";
cout << "Publication Year: " << b->getYearOfPublication() << "\n";
cout << "ID: " << b->getID() << "\n";
displayBooks(n + 1);
}
}
This is the function itself, however I can't show the complete program since it is a lot of code with multiple files. When the function is first called as displayBooks(0) in a switch case.
I believe that you are not printing out each index of the "b" variable you need to access the index of each one. You need to have b as an array of pointers then access the index of that variable like b[n]->someProperty();
You can create the array like this:
Obj* b[HOWMANY];
I am building a 2d game and I am storing all my enemy objects in an array. Right now I am trying to implement a quadtree. Currently I am just trying to build the quadtree and am not concerned with collisions. The code that pushes items to the quadtree is the following :
for (std::vector<Enemy>::iterator i=m_enemies.begin(); i != m_enemies.end(); ++i) {
std::cout << &(*i) << "Address of the object" << std::endl;
m_quad.Insert(&(*i));
}
The code for the Insert is the following :
void Quad::Insert(sf::RectangleShape* l_gameObject){
std::cout << &l_gameObject << "dsa1" << std::endl;
std::cout << "called insert " << m_objects.size() << std::endl;
m_objects.push_back(l_gameObject);
if (m_level < m_maxLevel) {
if (m_objects.size() > 3) {
std::cout<< "creating subregions " << m_objects.size() << std::endl;
m_subRegions.push_back(Quad(m_x,m_y,m_width/2.f, m_height/2, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x+m_width/2.f,m_y,m_width/2.f,m_height/2.f, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x+m_width/2.f, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
std::vector<int> temp;
for (int i=0; i < m_objects.size(); i++){
for (int j=0; j< m_subRegions.size(); j++) {
if (m_subRegions[j].Contains(m_objects[i])) {
m_subRegions[j].Insert(m_objects[i]);
temp.push_back(i);
break;
}
}
}
for (int i = temp.size(); i > -1; i--){
m_objects.erase(m_objects.begin() + temp[i]);
}
}
}
}
When I print the address that I am passing to the Insert function and the one I have in the function I see that they are different. In fact the on in is always the same and the one I pass is always different as it should be. Could anyone clarify why that is the case ?
EDIT : Thanks to gsamaras for pointing out that I was printing the address of the parameter.
Followup question
When I use the methods of the object I am addressing in the first for loop I get the correct results, but when I do the same thing in the Insert function I get 0. Why is that ?
You are printing the address of the address.
Change this:
std::cout << &l_gameObject << "dsa1" << std::endl;
to this:
std::cout << l_gameObject << "dsa1" << std::endl;
in order to print the same thing as outside your of your function.
Inside Insert, you're printing the address of the parameter.
Outside Insert, you're printing the parameter's value.
You want
std::cout << l_gameObject << "dsa1" << std::endl;
since l_gameObject is the address you're passing in.
Please do not try to modify the code. This code works fine.I took it from a book.I just need some help to understand the behavior of the code.
#include <iostream>
#include <ctime>
using namespace std;
void suffle2(int[][13]);
int main(){
int deck[4][13] = { 0 };
srand(time(0));
suffle2(deck);
return 0;
}
void suffle2(int d[4][13])
{
int row, column;
for (int card = 1; card <= 20; card++)
{
do{
row = rand() % 4;
column = rand() % 13;
cout << "d[" << row << "]" << "[" << column << "]" << "=" << d[row][column]<<endl;
} while (d[row][column] != 0);
d[row][column] = card;
cout << "d[" << row << "]" << "[" << column << "]" << "=" << d[row][column] <<endl;
}
}
Both cout << "d[" << row << "]" << "[" << column << "]" << "=" << d[row][column] <<endl;
I code to understand the behavior.My question is:
deck[4][13] has initialized to 0.So the while loop should not work cause it should always
find d[row][column] == 0 but it is not..it is working fine.
second question is,suppose the output is following:
d[0][4]=0
d[0][4]=1
d[2][4]=0
d[2][4]=2
d[1][12]=0
d[1][12]=3--------duplicate
d[3][11]=0
d[3][11]=4
d[0][6]=0
d[0][6]=5
d[1][12]=3--------duplicate
d[1][11]=0
d[1][11]=6
d[0][7]=0
d[0][7]=7<--------------------duplicate
d[1][6]=0
d[1][6]=8
d[0][6]=5
d[0][11]=0
d[0][11]=9
d[2][11]=0
d[2][11]=10
d[3][11]=4
d[3][2]=0
d[3][2]=11
d[2][11]=10
d[1][10]=0
d[1][10]=12
d[1][6]=8
d[0][9]=0
d[0][9]=13
d[0][1]=0
d[0][1]=14
d[3][4]=0
d[3][4]=15
d[0][7]=7 <---------------------duplicate
d[0][2]=0
d[0][2]=16
d[0][3]=0
d[0][3]=17
d[2][11]=10
d[0][5]=0
d[0][5]=18
d[0][9]=13
d[0][10]=0
d[0][10]=19
d[1][6]=8
d[1][7]=0
d[1][7]=20
Press any key to continue . . .
In the for loop the card value is incremental but when the duplicate card value is found for the same index of array, it is taking the very first value for that index.Why it is not changing the increment value?Please help me to understand the code.
You are right, all values of deck are 0 at the beginning and the loop works because the do{}while(condition); is executing at least once before checking condition. The loop is looking here for the first uninitialized card.
Duplicate output is just what the loop is currently checking, it finds an initialized card so it keeps looking for an uninitialized one (this is the first cout).
Below is the section I am having trouble with
if(transition == *(vec.end()-1)) { //vec contains the ASCI code
vec.pop_back();
--vec_index;
result.append(vec[vec_index]);
} else if(colors.find(modified.substr(1)) == colors.end()) {
cout << result << endl;
cout << "\033[0;37m";
cerr << "**ERROR: INVALID CLOSING COLOR TAG ON LINE: "
//cerr << "**ERROR: OVERLAPPING CLOSING COLOR TAG ON LINE: "
<< nlines << endl;
exit(0);
} else {
cout << result << endl;
cout << "\033[0;37m";
cerr << "**ERROR: INVALID CLOSING COLOR TAG ON LINE: "
//cerr << "**ERROR: OVERLAPPING CLOSING COLOR TAG ON LINE: "
<< nlines << endl;
exit(0);
}
In the above code, my else-if statement does not seem to execute at all. What I am trying to do is when modified.substr(1) is not found in the map is print the first error otherwise (the color string is in the map but it's not the same one as the last one in the vector) print the "else" error. However, my else case is always printing. The reason why it's modified.substr(1) is because modified (in this case) starts with a /.
transition = colors[modified.substr(1)];
will create an entry in map<> color; if it's not existing and value initialize it. That's why your else if condition will never be true. Use map::find instead of operator [] to find the entry. Something like,
map<string,string>::const_iterator it = colors.find(modified.substr(1)];
And use it instead of transition. You can de-reference it (as it->first for key and it->second for string-value) when needed to get the underlying string.
In your code vec is empty, so
if(transition == *(vec.end()-1))
leads to undefined behavior.
There is some problem during the run time of my program and i am unable to get what the problem is.
what happens basically is , my program automatically closes and displays the following in Microsoft visual c++ 2010 express window
What could be the reasons for this ? I have no idea why this is happening.
Let me tell that in my program i have used pointers too often and have used character arrays which i write to the disc
The program is too large to display
This is the function called after which my program stops :
void display_databases()
{
struct info_of_trains
{
int train_no;
char train_name[25];
char boarding_pt[25];
char destination[25];
int first_seats;
int fare_first;
int second_seats;
int fare_second;
char date[20];
};
info_of_trains e;
cout<<"TRno. TRname B.pt D.pt F.seats F.fare S.seats F.second Date\n";
FILE *fp;
fp=fopen("database","r");
if(fp==NULL)
{
cout<<"failure";
}
else
{
while(fread(&e,sizeof(e),1,fp)==1)
{
printf(e.train_no,e.train_name,e.boarding_pt,e.destination,e.first_seats,e.fare_first,e.second_seats,e.fare_second,e.date);
cout<<"-------------------------------------------------------------------------------\n";
}
fclose(fp);
}
}
This is where execution stops :!
You seem to have hit a breakpoint, or your program had an access violation (reading an illegal pointer). You also seem to have maximized/detached the debugging panels. You can reattach the panel by dragging the yellow bar at the top to the lower part of the screen.
Did you recieve a warning message before it happened? Otherwise, did you define a breakpoint (clicking in the left margin of the code editor, so a red circle appears there)
EDIT: As pointed out in the comments, the error occurs because you use printf the wrong way. Use cout instead, as you did above:
cout << e.train_no <<" " << e.train_name << " " << e.boarding_pt << " " << e.destination << " " << e.first_seats << " " << e.fare_first << " " << e.second_seats << " " << e.fare_second << " " << e.date << endl;