I've a switch and a functions 'block the case that are after it' at the 4th case of the switch.
I tried to move the case and apparently it's the 'P_JEU' case that block but I don't understand why.
switch (partie) {
case P_CHOIX_ANIM:
allPlayers.chenillard(250,100);
partie = P_CHOIX;
case P_CHOIX:
temp = allPlayers.checkInterro();
if (temp == 0) break;
if (temp == 1) { //Only 1 press
partie = P_JEU;
DEBUG_PRINTLN("P_CHOIX 1 press");
break;
}
partie = P_CHOIX_ERREUR;
break;
case P_JEU:
// Bug
if (bConfig.isPressed()) {
if (bConfig.getPressDuration()) {
if (bConfig.getPressDuration() <= 2000) {
partie = P_CHOIX_RESET;
} else {
bConfig.reset();
}
}
}
allPlayers.filteredCall(A_CHECK, J_PLAYER);
bool passResponse = allPlayers.Pass();
if (passResponse) { partie = P_JEU_REPONSE; }
break;
case P_CHOIX_RESET:
allPlayers.reset();
bConfig.reset();
partie = P_CHOIX_ANIM;
break;
default:
DEBUG_PRINTLN("Default");
partie = P_CHOIX_RESET;
break;
Thanks
You cant declare variables inside a case:
https://complete-concrete-concise.com/programming/c/keyword-switch-case-default/
Can I declare variables inside an Objective-C switch statement?
bool passResponse = allPlayers.Pass();
is wrong - you have to enclose it:
case P_JEU:
// Bug
if (bConfig.isPressed()) {
if (bConfig.getPressDuration()) {
if (bConfig.getPressDuration() <= 2000) {
partie = P_CHOIX_RESET;
} else {
bConfig.reset();
}
}
}
allPlayers.filteredCall(A_CHECK, J_PLAYER);
{
bool passResponse = allPlayers.Pass();
if (passResponse) { partie = P_JEU_REPONSE; }
}
break;
Related
The first function, linkAndMove, is used for basic linking together and moving point process.
The Union function is used for finding all numbers in linked lists la and lb (without repeats)
My test example: la {1,3} lb{3,5}
But in the last when la point to NULL, and lb point to 5.
After first function linkAndMove, the list la changed to {1,3,5}
Why did la's end node change from NULL to lb's now node 5?
before first function
after first function
void linkAndMove(slink **pNode, slink **qNode, slink **finNode,
int linkFlag, int moveFlag) {
if (linkFlag == -1 || moveFlag == -1) {
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
switch (linkFlag) {
case 0:
if ((*finNode)->data != (*pNode)->data) {
(*finNode)->next = (slink *) malloc(sizeof(MemLEN));
(*finNode)->next = (*pNode);
(*finNode) = (*finNode)->next;
}
break;
case 1:
if ((*finNode)->data != (*qNode)->data) {
(*finNode)->next = (slink *) malloc(sizeof(MemLEN));
(*finNode)->next = (*qNode);
(*finNode) = (*finNode)->next;
}
break;
case 2:
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
switch (moveFlag) {
case 0:
(*pNode) = (*pNode)->next;
break;
case 1:
(*qNode) = (*qNode)->next;
break;
case 2:
(*pNode) = (*pNode)->next;
(*qNode) = (*qNode)->next;
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
}
void Union(slink *la, slink *lb, slink *lc) {
slink *pNode, *qNode;
pNode = la->next;
qNode = lb->next;
int linkFlag, moveFlag;
while (pNode != NULL || qNode != NULL) {
linkFlag = -1;
moveFlag = -1;
if (pNode == NULL) {
linkFlag = moveFlag = 1;
} else if (qNode == NULL) {
linkFlag = moveFlag = 0;
} else {
if (pNode->data > qNode->data) {
linkFlag = 1;
moveFlag = 1;
} else if (pNode->data < qNode->data) {
linkFlag = 0;
moveFlag = 0;
} else {
linkFlag = 0;
moveFlag = 2;
}
}
/*if (pNode == NULL) {
linkAndMove(NULL, &qNode, &lc, linkFlag, moveFlag);
} else*/
linkAndMove(&pNode, &qNode, &lc, linkFlag, moveFlag);
}
}
I found the reason.
Because in function linkAndMove, the pointer finNode is connected to the list la's node. In preivous codes, using node's next to connect pNode, so changed the la's end node from NULL to that node.
The solution I found is create new node for list lc, that cannot infect the orignal data list la. Codes here.
switch (linkFlag) {
case 0:
if ((*finNode)->data != (*pNode)->data) {
(*finNode)->next = initLinkNode();
(*finNode) = (*finNode)->next;
(*finNode)->data = (*pNode)->data;
}
break;
case 1:
if ((*finNode)->data != (*qNode)->data) {
(*finNode)->next = initLinkNode();
(*finNode) = (*finNode)->next;
(*finNode)->data = (*qNode)->data;
}
break;
case 2:
break;
default:
cout << "ERROR! No matched logical in basic link list process." << endl;
exit(1);
}
I need to group multiple if functions into one big function with a custom name.
if (NPC == NPC1)
{
Attack = NPCAttack1;
}
if (NPC == NPC2)
{
Attack = NPCAttack2;
}
if (NPC == NPC3)
{
Attack = NPCAttack3;
}
if (NPC == NPC4)
{
Attack = NPCAttack4;
}
if (NPC == NPC5)
{
Attack = NPCAttack5;
}
if (NPC == NPC6)
{
Attack = NPCAttack6;
}
if (NPC == NPC7)
{
Attack = NPCAttack7;
}
if (NPC == NPC8)
{
Attack = NPCAttack8;
}
if (NPC == NPC9)
{
Attack = NPCAttack9;
}
if (NPC == NPC10)
{
Attack = NPCAttack10;
}
I want all of that to be inside a function called AttackFunction. How do I do that?
First of all, let's replace all of those if statements with a single switch statement:
switch (NPC) {
case NPC1:
Attack = NPCAttack1;
break;
case NPC2:
Attack = NPCAttack2;
break;
case NPC3:
Attack = NPCAttack3;
break;
case NPC4:
Attack = NPCAttack4;
break;
case NPC5:
Attack = NPCAttack5;
break;
case NPC6:
Attack = NPCAttack6;
break;
case NPC7:
Attack = NPCAttack7;
break;
case NPC8:
Attack = NPCAttack8;
break;
case NPC9:
Attack = NPCAttack9;
break;
case NPC10:
Attack = NPCAttack10;
break;
}
Now we can inject this code into a function, which receive the NPC, and return the desired type of attack.
<attack_type> get_attack_type_from_npc(<npc_type> NPC) {
switch (NPC) {
case NPC1:
return NPCAttack1;
case NPC2:
return NPCAttack2;
case NPC3:
return NPCAttack3;
case NPC4:
return NPCAttack4;
case NPC5:
return NPCAttack5;
case NPC6:
return NPCAttack6;
case NPC7:
return NPCAttack7;
case NPC8:
return NPCAttack8;
case NPC9:
return NPCAttack9;
case NPC10:
return NPCAttack10;
}
throw std::runtime_error("No attack type found");
}
in your main:
int main() {
// ... Declare Attack & NPC ... Set NPC ...
Attack = get_attack_type_from_npc(NPC);
}
I have the next code, and when raiz is NULL is not entering the if statement
I already tried to do the other way like if(raiz != NULL) and in the else statement trying to run the code :
masAlto = 1;
raiz = Nuevo;
Here is my code:
void InsertaAVL(arbolAVL *raiz, arbolAVL *Nuevo, int masAlto)
{
arbolAVL *a = raiz;
int subarbolmasAlto = FALSO;
if (raiz == NULL)
{
masAlto = 1;
raiz = Nuevo;
}
else
{
if (raiz->llave > Nuevo->llave)
{
//Insertar a la Izquierda
InsertaAVL(raiz->Izq, Nuevo, subarbolmasAlto);
if (subarbolmasAlto == VERDADerO)
{
switch(raiz->FBalance)
{
case LH : BalanceIzquierdo(raiz,masAlto);
break;
case RH : raiz->FBalance = EH;
masAlto = FALSO;
break;
case EH : raiz->FBalance = LH;
masAlto = VERDADerO;
break;
}
}
else
masAlto = FALSO;
}
else
{
InsertaAVL(raiz->Der, Nuevo, subarbolmasAlto);
if (subarbolmasAlto == VERDADerO)
{
switch(raiz->FBalance)
{
case LH : raiz->FBalance = EH;
break;
case RH : BalanceDerecho(raiz,masAlto);
break;
case EH : raiz->FBalance = RH;
masAlto = VERDADerO;
break;
}
}
else
masAlto = FALSO;
}
}
}
I expect that when raiz is NULL to enter the code inside the if statement
It does enter the if statement true branch, although I'd prefer if (!raiz) as the conditional as this behaves well with the NULL of C and the nullptr of C++. Effectively though the true branch is a no-op since the assignments
masAlto = 1;
raiz = Nuevo;
have no effect on the quantities passed by the caller of the function. Did you want int* masAlto &c. as the parameters?
Hello everyone so this is my first question here.I will try to explain my problem as briefly as i can.So i am trying to sort a taken string into 26 different lists according to their initials.I don't know if this is a proper approach since i am only a student yet.When i run this code i get the following error which i could not find any solution for.
error C4700: uninitialized local variable 'item' used
Here is my node struct and my linked list is just as any other linked list class.
template
struct nodeType
{
int wCount;
Type info;
nodeType<Type> *link;
};
So my question is why do i get this error and is there any better approach for my problem.(By the way this is just a part of what i am trying to do normally i should be reading a processed text file and inserting every word into a list according to their initials and increase their count if they exist in the list.)
#include <iostream>
#include "unorderedLinkedListType.h"
#include <string>
using namespace std;
int main()
{
unorderedLinkedList<string> listA, listB, listC, listD, listE, listF, listG,
listH, listI, listJ, listK, listL, listM, listN, listO, listP, listQ, listR,
listS, listT, listU, listV, listW, listX, listY, listZ;
nodeType<string> *item; // Node Definition
item->info = "trying";
item->link = NULL;
item->wCount = 0;
char first; // Taking the initial of a string
first = item->info[0];
switch (first) // Switch case for insertion to lists
{
case 'a': if (listA.search(item->info)){ item->wCount++; }
else { listA.insertFirst(item->info); }
break;
case 'b': if (listB.search(item->info)){ item->wCount++; }
else { listB.insertFirst(item->info); }
break;
case 'c': if (listC.search(item->info)){ item->wCount++; }
else { listC.insertFirst(item->info); }
break;
case 'd': if (listD.search(item->info)){ item->wCount++; }
else { listD.insertFirst(item->info); }
break;
case 'e': if (listE.search(item->info)){ item->wCount++; }
else { listE.insertFirst(item->info); }
break;
case 'f': if (listF.search(item->info)){ item->wCount++; }
else { listF.insertFirst(item->info); }
break;
case 'g': if (listG.search(item->info)){ item->wCount++; }
else { listG.insertFirst(item->info); }
break;
case 'h': if (listH.search(item->info)){ item->wCount++; }
else { listH.insertFirst(item->info); }
break;
case 'i': if (listI.search(item->info)){ item->wCount++; }
else { listI.insertFirst(item->info); }
break;
case 'j': if (listJ.search(item->info)){ item->wCount++; }
else { listJ.insertFirst(item->info); }
break;
case 'k': if (listK.search(item->info)){ item->wCount++; }
else { listK.insertFirst(item->info); }
break;
case 'l': if (listL.search(item->info)){ item->wCount++; }
else { listL.insertFirst(item->info); }
break;
case 'm': if (listM.search(item->info)){ item->wCount++; }
else { listM.insertFirst(item->info); }
break;
case 'n': if (listN.search(item->info)){ item->wCount++; }
else { listN.insertFirst(item->info); }
break;
case 'o': if (listO.search(item->info)){ item->wCount++; }
else { listO.insertFirst(item->info); }
break;
case 'p': if (listP.search(item->info)){ item->wCount++; }
else { listP.insertFirst(item->info); }
break;
case 'q': if (listQ.search(item->info)){ item->wCount++; }
else { listQ.insertFirst(item->info); }
break;
case 'r': if (listR.search(item->info)){ item->wCount++; }
else { listR.insertFirst(item->info); }
break;
case 's': if (listS.search(item->info)){ item->wCount++; }
else { listS.insertFirst(item->info); }
break;
case 't': if (listT.search(item->info)){ item->wCount++; }
else { listT.insertFirst(item->info); }
break;
case 'u': if (listU.search(item->info)){ item->wCount++; }
else { listU.insertFirst(item->info); }
break;
case 'v': if (listV.search(item->info)){ item->wCount++; }
else { listV.insertFirst(item->info); }
break;
case 'w': if (listW.search(item->info)){ item->wCount++; }
else { listW.insertFirst(item->info); }
break;
case 'x': if (listX.search(item->info)){ item->wCount++; }
else { listX.insertFirst(item->info); }
break;
case 'y': if (listY.search(item->info)){ item->wCount++; }
else { listY.insertFirst(item->info); }
break;
case 'z': if (listZ.search(item->info)){ item->wCount++; }
else { listZ.insertFirst(item->info); }
break;
}
listT.print(); // Printing the listT to try out my code
return 0;
}
nodeType<string> *item; is a pointer to an item. But you never allocated the item and initialized the pointer. Please use a std::map instead of this creepy switch statement and 26 lists.
#include <map>
int main()
{
std::map< char, unorderedLinkedList<string> > listMap;
nodeType<string> *item = new nodeType<string>();
item->info = "trying";
item->link = NULL;
item->wCount = 0;
char first;
first = item->info[0];
if (listMap[first].search(item->info))
{
item->wCount++;
}
else
{
listMap[first].insertFirst(item->info);
}
// ...
}
I wrote a C++ program, actually it's a game.
I've received this error:
Windows has triggered a breakpoint in bla bla...
Can someone help me?
That's code, but error occur on line AAA:
void r_motions(char **map,int size)
{
int parameter_i,parameter_j,player_i,player_j;
int *r_location_i = new int[1],*r_location_j = new int[1];
player_finder(map,size,player_i,player_j);
int r_num = robots_finder(map,size,r_location_i,r_location_j);
for(int i=1;i<=r_num;i++)
{
parameter_i =0;
parameter_j =0;
if(r_location_i[i]>player_i) parameter_i = -1;
if(r_location_i[i]<player_i) parameter_i = 1;
if(r_location_j[i]>player_j) parameter_j = -1;
if(r_location_j[i]<player_j) parameter_j = 1;
map[r_location_i[i]][r_location_j[i]] = '.';
r_location_i[i] = r_location_i[i]+parameter_i;
r_location_j[i] = r_location_j[i]+parameter_j;
}
for(int i=1;i<=r_num;i++)
{
switch (map[r_location_i[i]][r_location_j[i]])
{
case '.':
map[r_location_i[i]][r_location_j[i]] = '+';
break;
case '#':
map[r_location_i[i]][r_location_j[i]] = '+';
print_map(map,size);
cout << "Robots win ." << endl;
sleep(1000);
exit(1);
break;
case '+':
map[r_location_i[i]][r_location_j[i]] = '*';
break;
case '*':
map[r_location_i[i]][r_location_j[i]] = '*';
break;
default: cout << "what r u doin' ??";
break;
}
}
}
All right, just for starters, look at this:
int *r_location_i = new int[1], ...;
...
for(int i=1;i<=r_num;i++)
{
parameter_i =0;
...
if(r_location_i[i]>player_i) parameter_i = -1; // reading outside the array
...
r_location_i[i] = r_location_i[i]+parameter_i; // writing outside the array
...
}
Go back and study arrays. Do not touch another pointer until you know exactly what's wrong with the code above.