I have a function that validates whether certain coordinates are within a matrix and returns true/false depending on the answer:
bool validateNextLocation(char robot, int proposed_row, int proposed_col, char map[7][7]){
auto const robot_location = World::getRobotLocation(robot);
int row = robot_location -> first;
int col = robot_location -> second;
if (map[proposed_row][col] != '1' || map[row][proposed_col] != '1'){return false;}
else{return true;}
}
I am trying to use the function in my switch cases:
switch (direction) {
case 'L': {
if (World::validateNextLocation(robot, ++robot_location->first, robot_location-> second, char a[7][7])){
++robot_location->first;
}
else{return -1;}
}
break;
case 'D': {
if (World::validateNextLocation(robot, robot_location->first, --robot_location->second, char a[7][7])){
--robot_location->second;
}
else{return -1;}
}
break;
case 'R': {
if (World::validateNextLocation(robot, --robot_location->first, robot_location->second, char a[7][7])){
--robot_location->first;
}
else{return -1;}
}
break;
default: {
if (World::validateNextLocation(robot, robot_location->first, ++robot_location->second, char a[7][7])){
++robot_location->second;
}
else{return -1;}
}
break;
}
But the char a[7][7] has a red underline where the error reads:
Expected '(' for function style cast or type construction
I know I'm not missing a bracket but where am I going wrong?
Just change
if (World::validateNextLocation(robot, robot_location->first,
++robot_location->second, char a[7][7])){
to
if (World::validateNextLocation(robot, robot_location->first,
++robot_location->second, a)){
Declaring an array, and using an array are two different things, you don't use the same syntax for both. I am assuming that somewhere in your code you do have a proper declaration for a.
That said passing a 7x7 matrix from one function to another seems unlikely to be the right thing to do, but no doubt that will sort itself out in time.
if (World::validateNextLocation(robot, ++robot_location->first, robot_location-> second, char a[7][7])){
The char a[7][7] would declare a new variable a as 7x7 matrix.
First you can't declare a variable inside of function arguments, second the variable would uninitialized and thrid expire at the end of the function call. So three reasons this syntax makes no sense.
You have to declare your array before the function call if you don't have it already and then just pass the variable a as argument.
Related
i am trying to add a developer mode in my program. since duty of car defers every month,i want give my user permission to change every single variables in my program alike duty lccost yen2taka freight
#include <iostream>
using namespace std;
class A
{
public:
int carbid,duty;
void Input()
{
cout<<"please insert the car price you want to bid for(in yen): ";
cin>>carbid;
cout<<"duty of the car: ";
cin>>duty;
}
int Exportcost()
{
int exportcost;
int servicechrg=10;
int freight=20;
exportcost=servicechrg+freight+carbid;
return exportcost;
}
int Yen2taka()
{
int yen2taka;
int taka2dollarrate=10;
int dollar2yen=1;
yen2taka=((Exportcost())/dollar2yen)*taka2dollarrate;
return yen2taka;
}
int Importcost()
{
int importcost;
int lccost=10;
int cnfcost=20;
importcost=lccost+cnfcost;
return importcost;
}
int Totalcosting()
{
int total;
int myprofit=10; //80000
total=myprofit+Importcost()+Yen2taka();
cout<<total;
return total;
}
void summary()
{
cout<<
}
};
int main()
{
x:
A ob;
ob.Input();
ob.Exportcost();
ob.Yen2taka();
ob.Importcost();
ob.Totalcosting();
int ch;
cout<<"press 1 to see the summery of the costing or 2 to restart costing again"<<endl;
cin>>ch;
switch(ch)
{
case 1:
ob.summary();
break;
case 2:
goto x;
}
}
At first, you should collect these parameters in a separate class:
class Configuration // maybe you find a better name...
{
int m_servicechrg = 10; // default
int m_freight = 20;
// ...
public:
int servicechrg() { return m_servicechrg; }
void servicechrg(int value); { /* check some limits? */ m_servicechrg = value; }
int freight() { return m_freight; }
void freight(int value); { /* check some limits? */ m_freight = value; }
// ...
};
// will allow you to do:
// C c; std::cout << c;
ostream& operator<<(ostream& s, Configuration const& c)
{
// which ever formatting is appropriate...
s << c.servicechrg() << ' ' << c.freight();
return s;
}
The setters could alternatively return bool to indicate invalid values.
Now you can use this class within main:
Configuration c;
A a;
int cost = a.exportCost(c); // you'd have to adjust signatures...
int value;
switch(ch)
{
case 4:
if(stc::cin >> freight) // catches invalid user input!
// one ALWAYS should do, otherwise you might end up in
// your program not working any more
{
c.freight(value);
// or, if you have:
if(!c.freight(value))
{
// some appropriate error message
// (it's better not to output in the setter, you are more flexible this
// way – maybe you want different messages at different occasions?)
}
}
else
{
// appropriate error handling
}
break;
default:
// handling invalid user input
// again, you always should; but stream state is not in error state,
// so you just can print appropriate error message
break;
}
See this answer for how to correctly handle stream errors.
If you wonder about the differences in error handling: First case is met if user enters non-numerical input, such as ss, second case, if input is numerical, but out of valid range (77).
Now if you don't want to pass the configuration as parameter all the time, you could make a global variable from (but careful, there are some dangers with global variables, use them as sparely as possible) or implement the singleton pattern.
Side notes: goto can be a fine tool sometimes, but it is a dangerous one (and the label's name x isn't a good one, prefer a name that clearly shows intention, such as REENTRY_POINT, LOOP_START, ...). If you can get along without unreasonable effort, prefer such variants:
bool isRunning = true;
do
{
// ...
case 2:
isRunning = false;
break;
}
while(isRunning);
Sure, an additional variable, an additional check; unfortunately, you cannot use break to exit a (pseudo-) endless loop (for(;;)) (but don't apply this pattern for nested loops, then it gets more and more unreadabla – and ineffcient: bool isExit = false; for(int i = 0; !isExit && i < n; ++i) { for(j = 0; j < n; ++j) { isExit = true; break; } } – see what I mean?). A variant might be:
for(;;)
{
switch(ch)
case 1:
// ...
//break; <- replace
continue;
case 2:
//
break;
} // end of switch
break; // break the surrounding for(;;) loop
}
But that's not really nice either.
A pretty nice variant allowing to exit the loop in the given case, as there isn't anyhting to do afterwards:
for(;;)
{
switch(ch)
{
case 2:
// maybe yet some cleaning up here
return 0;
default:
// ...
break;
}
}
Drawback: The function's exit point possibly is deeply nested inside the code.
There are yet other tricks to allow this pattern, like packing sub-sections of code in a lambda having a return inside and call that one directly. But that now really starts going beyond the scope...
Finally, if you insist on goto, my variant would rather be:
for(;;)
{
switch(ch)
{
case 2:
// ...
goto LOOP_EXIT;
default:
// ...
break;
}
}
LOOP_EXIT:
return 0; // e. g. main
(void)0; // if there isn't anything to do in the function any more
// (labels require an instruction afterwards!)
There won't be a hidden loop now and it is more obvious what you actually are doing. Currently, not really an issue, but if your code grows, the hidden loop gets more and more difficult to spot.
In such cases, I clearly mark the gotos so that another coder can immediately spot the critical code points:
///////////////////////////////////////////////////
// possibly some comment why applying this pattern
goto SOME_LABEL;
///////////////////////////////////////////////////
One could do the same with deeply nested function exit points (return).
I'm assuming the code should look something like this:
QString Boat::programming()
{
int rand = ???;
switch (rand) {
case 1:
return function1();
case 2:
return function2();
case 3:
return function3();
case 4:
return function4();
case 5:
return function5();
default:
return "";
}
}
Requirements:
When I call the function a second or third time, rand can not use the same value it had last time, or the time before.
Must use Qt's framework.
How should I go about this?
I assume '???' is the Qt way to get a random number in the range you want. Just wrap '???' in another function that stores the last 2 values it returned...
int myrand()
{
static int pp = -1, ppp = -1;
int p;
do { p = ???; } while (p == pp || p == ppp);
ppp = pp; pp = p;
return p;
}
QString Boat::programming()
{
int rand = myrand();
...
In a program there is a pointer to a class object WordList *TheList;. WordList has subclasses WordDataList and WordDataDLinkList, so in a case statement we interpret which subclass to use and how to print out the information in the list. From what I understand in the specs, each case is supposed to declare TheList as a pointer of that type and use that, and then reclaim the memory at the end of the case so that it can be used in the next iteration of the loop. When I try something like:
while (true)
{
displayMenu();
cin>>selection;
switch(selection)
{
case '1':
TheList = new WordDataList;
TheList->parseIntoList(inf);
TheList->printIteratively();
delete TheList;
break;
case '2':
TheList = new WordDataList;
TheList->parseIntoList(inf);
TheList->printRecursively();
delete TheList;
break;
case '3':
TheList = new WordDataList;
TheList->parseIntoList(inf);
TheList->printPtrRecursively();
delete TheList;
break;
case '6':
cout<<"Goodbye"<<endl;
return 0;
default:
cout<<"I cannot understand "<<selection<<". Try again."<<endl;
break;
} // switch
} // while
Deleting the pointer makes it so after the first run through no data appears (Menu still comes up) and option 2 ends up seg faulting. I'm modifying code my professor gave, and when he had no delete call, and new WordDataList and parseIntoList before the loop it ran fine. Any suggestions?
Added:
I'm reinitializing TheList in each case because I'll be adding 4 and 5 that will use WordDataDLinkList. If it's a pointer to WordDataList from outside the case statement, how would I change it to WordDataDLinkList inside when I need to? My professor wrote WordDataList for us:
#include <sstream>
#include <iostream>
#include "WordDataList.h"
using namespace std;
WordDataList::WordDataList()
{ numWords=0; }
bool WordDataList::incMatch(string temp)
{ for(int i=0; i<numWords; i++) {
if (temp==TheWords[i].getWord()) {
TheWords[i].incCount();
return true;
}
}
return false;
}
void WordDataList::parseIntoList(ifstream &inf)
{ string temp;
while (inf >> temp)
if (!incMatch(temp) && numWords < 10) {
TheWords[numWords].setWord(temp);
TheWords[numWords++].setCount(1);
}
}
// Print the data iteratively
void WordDataList::printIteratively()
// void printObjectArrayIterator(WordData TheWords[], int numWords)
{
cout<<"--------------------------"<<endl;
cout<<"|Object Array Iterative|"<<endl;
cout<<"|Word Occurences |"<<endl;
cout<<"--------------------------"<<endl;
for(int i=0; i<numWords; i++)
cout<<" "<<TheWords[i]<<endl;
}
// Print the data recursively
void WordDataList::printRecursivelyWorker(int numWords)
//void printObjectArrayRecursive(WordData TheWords[], int numWords)
{if (numWords==1) {
cout<<"--------------------------"<<endl;
cout<<"|Object Array Recursive|"<<endl;
cout<<"|Word Occurences |"<<endl;
cout<<"--------------------------"<<endl;
cout<<" "<<TheWords[numWords-1]<<endl;
return;
}
printRecursivelyWorker(numWords-1);
cout<<" "<<TheWords[numWords-1]<<endl;
}
// Call worker function to print the data recursively
void WordDataList::printRecursively()
{ printRecursivelyWorker(numWords); }
// Print the data recursively with a pointer
void WordDataList::printPtrRecursivelyWorker(int numWords)
//void printObjectArrayPointerRecursive(WordData* TheWords, int numWords)
{if (!numWords)
{ cout<<"--------------------------"<<endl;
cout<<"|Object Array Pointer |"<<endl;
cout<<"|Word Occurences |"<<endl;
cout<<"--------------------------"<<endl;
return;
}
printPtrRecursivelyWorker(numWords-1);
cout<<" "<<*(TheWords+(numWords-1))<<endl;
}
// Call worker function to print the data recursively
void WordDataList::printPtrRecursively()
{ printPtrRecursivelyWorker(numWords); }
I think you are confused about something else. Why do you need to keep recreating the 'WordDataList' are the parse and print methods modifiying it?
If not, just create it once, and simply use the select to choose which print function to use if any.
I would also suggest putting each of the case execution statement sets into a closure, and adding some print outs or stepping through the debugger to see what is going on. Main guesses are that your 'new' is returning NULL such that you can't call its members properly OR destructor is bad.
Okay, so from what I can kindof tell: Options 1-3 should select a different subclass for your pointer TheList to hold. TheList is a pointer to your base class so that's good. What I think you need to do is new the appropriate subclass in each switch statement. I.E.
case '1': TheList = new WordDataDLinkList();
EDIT: if you intend to call a different version of each member function based off the classtype you are currently using, as in WordDataDLinkList::parseIntoList(inf), instead of WordDataList::parseIntoList(inf), try reading up on polymorphism
This question already has answers here:
Case expression not constant
(5 answers)
Closed 5 years ago.
Here is my code:
bool Character::keyPress(char c)
{
switch(c)
{
case up_key:
move(0, -1);
break;
case down_key:
move(0, 1);
break;
case left_key:
move(-1, 0);
break;
case right_key:
move(1,0);
break;
default:
return false;
}
return true;
}
And the compiler complains:
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
In my header file I have:
protected:
char up_key;
char down_key;
char right_key;
char left_key;
I am using Visual C++ 2008.
As the error message states, the case expressions must be constant. The compiler builds this as a very fast look-up table at compile time and it can't do that if there is a possibility that the values could change as the program runs.
If you do need them to be variable, not constant, your best bet is to use if/else statements instead.
Replace this long clumsy code,
switch(c)
{
case up_key:
move(0, -1);
break;
case down_key:
move(0, 1);
break;
case left_key:
move(-1, 0);
break;
case right_key:
move(1,0);
break;
default:
return false;
}
with something like this:
move( (c==right_key) - (c==left_key) , (c==down_key) - (c==up_key) );
You can litterly replace that 17 lines long of code with that much more neat single line of code.
You can't because the language doesn't work that way. For example, what would happen if up_key, down_key, right_key, and left_key were all equal?
Because the switch statement can take only constants, you know when reading the code that the things you're comparing against are all constants. On the other hand, you would use if statements (or some other structure) to compare against variables:
if (c == up_key) {
move(0, -1);
} else if (c == down_key) {
move(0, 1);
} else ...
This provides a distinct difference in structure which can greatly aid those who come after you in reading your code. Imagine if you had to look up every case label to see whether it was a variable or not?
I believe it's because the compiler generates a jump table, with the values hardcoded in, although I may be wrong. The way the tables are generated just doesn't allow for it.
Since other answers have covered why you are getting an error, here is a way to move in one of the four directions in response to a key press: use lookup tables instead of the conditionals/switches.
Setup portion:
std::map<char,pair<int,int> > moves;
moves[up_key] = make_pair(0, -1);
moves[down_key] = make_pair(0, 1);
moves[left_key] = make_pair(-1, 0);
moves[right_key] = make_pair(1, 0);
The function:
bool Character::keyPress(char c) {
if (moves.count(c)) {
pair<int,int> dir = moves[c];
move(dir.first, dir.second);
return true;
} else {
return false;
}
}
//here is the full functional code snippet which can be compiled and run with most of C++
//compiler/link ...console app was demoed but you can apply the code/logic to win32 app...
//if you have any problem, send me email to Samuel_Ni#yahoo.com
#include <iostream.h>
#include <map>
#include <conio.h>
class CkbdHanler{
private:
map<char,pair<int,int> > moves;
protected:
char up_key;
char down_key;
char right_key;
char left_key;
public:
CkbdHanler(char a,char b,char c,char d):up_key(a),
down_key(b),
right_key(c),
left_key(d)
{
moves[up_key] = make_pair(0, -1);
moves[down_key] = make_pair(0, 1);
moves[left_key] = make_pair(-1, 0);
moves[right_key] = make_pair(1, 0);
}
bool keyPress(char c){
if (moves.count(c)) {
pair<int,int> dir = moves[c];
move(dir.first, dir.second);
return true;
} else return false;
}
void move(int i,int j){
cout<<"(i,j)=("<<i<<","<<j<<")"<<endl;
}
};
int main(int argc, char* argv[])
{
CkbdHanler CmyKbdH('u','d','l','r');
cout << "Hello C++... here is a demo of Map to replace switch-case" << endl;
CmyKbdH.keyPress('d');
cout << endl << "Press any key to continue...";
getch();
return 0;
}
I'm getting some weird behavior with a vector in C++ I was hoping someone could help me out. I have a vector like so:
vector<Instruction*> allInstrs;
the struct for Instruction is as follows:
struct Instruction : simple_instr
{
InstrType type;
Instruction(const simple_instr& simple) : simple_instr(simple)
{
type = Simple;
loopHeader = false;
loopTail = false;
}
int Id;
bool loopHeader;
bool loopTail;
};
the problem I'm having is this:
I need to iterate through each instruction and pull out specific fields and use those to do some analysis on the instructions in the vector. To do that, I was basically doing
VariableList Variables;
void GenerateVariableList()
{
for (int i = 0; i < allInstrs.size(); i++)
{
Variables.Add(allInstrs[i]);
}
Variables.RemoveDuplicates();
}
Variable List is defined as
struct VariableList
{
void Add(simple_instr* instr)
{
PrintOpcode(instr);
switch(instr->opcode)
{
case STR_OP:
case MCPY_OP:
Add(instr->u.base.src1);
Add(instr->u.base.src2);
break;
case LDC_OP:
Add(instr->u.ldc.dst);
break;
case BTRUE_OP:
case BFALSE_OP:
Add(instr->u.bj.src);
break;
case CALL_OP:
cout << "CALL OP" <<endl;
break;
case MBR_OP:
Add(instr->u.mbr.src);
break;
case RET_OP:
if (instr->u.base.src1 != NO_REGISTER)
Add(instr->u.base.src1);
break;
case CVT_OP:
case CPY_OP:
case NEG_OP:
case NOT_OP:
case LOAD_OP:
Add(instr->u.base.dst);
Add(instr->u.base.src1);
break;
case LABEL_OP:
case JMP_OP:
break;
default:
Add(instr->u.base.dst);
Add(instr->u.base.src1);
Add(instr->u.base.src2);
break;
}
}
void Add(Variable var)
{
variableList.push_back(var);
}
void RemoveDuplicates()
{
if (variableList.size() > 0)
{
variableList.erase(unique(variableList.begin(), variableList.end()), variableList.end());
currentID = variableList.size();
}
}
VariableList()
{
currentID = 0;
}
VariableList(VariableList& varList, bool setLiveness = false, bool LiveVal = false)
{
currentID = 0;
for (int i = 0; i < varList.size(); i++)
{
Variable var(varList[i]);
if (setLiveness)
{
var.isLive = LiveVal;
}
variableList.push_back(var);
}
}
Variable& operator[] (int i)
{
return variableList[i];
}
int size()
{
return variableList.size();
}
vector<Variable>::iterator begin()
{
return variableList.begin();
}
vector<Variable>::iterator end()
{
return variableList.end();
}
protected:
int currentID;
vector<Variable> variableList;
void Add(simple_reg* reg, bool checkForDuplicates = false)
{ cout << "Register Check" <<endl;
if (reg == null)
{
cout << "null detected" << endl;
return;
}
if (reg->kind == PSEUDO_REG)
{
if (!checkForDuplicates || (checkForDuplicates && find(variableList.begin(), variableList.end(), reg->num) != variableList.end()))
{
cout << "Adding... Reg " << reg->num << endl;
Variable var(reg->num, currentID);
variableList.push_back(var);
currentID++;
}
}
}
};
When I do this though, every instruction goes to the default case statement, even though I knwo for a fact some instructions shouldn't. If I change GenerateVariableList to
void GenerateVariableList()
{
for (int i = 0; i < allInstrs.size(); i++)
{
PrintOpcode(allInstrs[i]);
Variables.Add(allInstrs[i]);
}
Variables.RemoveDuplicates();
}
so that there is now a second PrintOpCode in addition to the one in Variables.Add, the program behaves correctly. I can't understand why adding a second PrintOpcode makes it work correctly. All print Opcode is is a function with a switch statement that just prints out a specific string depending on what the value of one of simple_instr's fields is.
VariableList Variables is contained inside of a separate struct called CFG
If you need more information/code i can provide it. If the answer is obvious I apologize, I don't program in C++ very often
EDIT:
One of the answers left, deleted now though, got me the fix.
Previously I was doing
static vector<Instruction*> ConvertLinkedListToVector(simple_instr* instructionList)
{
vector<Instruction*> convertedInstructions;
int count = 0;
for (simple_instr* current = instructionList; current; count++, current = current->next)
{
//Instruction* inst = new Instruction(*current);
Instruction inst = Instruction(*current);
inst.Id = count;
convertedInstructions.push_back(&inst);
}
return convertedInstructions;
}
to make the vector, but after reading that answer I changed it back to using "new" and it works correctly now. Thanks for the help, sorry for the dumb question heh
Most likely the const simple_instr& simple passed to your constructor goes out of scope, and you keep an invalid reference/pointer to a simple_instr.
Possibly not related your problem, but certainly a potential source of strange behaviour: Your Instruction(const simple_instr& simple) constructor may be getting called when you don't intend it. Mark it explicit...
explicit Instruction(const simple_instr& simple) ...
If that causes compiler errors, then that's progress :-) You might need to write a copy constructor to make them go away, and explicitly call the old constructor where you need to.
So, there are several suspicious observations:
In your definition of VariableList you use a type called Variable - how is that type defined?
Iterating over a container should be done using an iterator:
for (vector<Intruction *>::iterator it = allInstrs.begin();
it != allInstrs.end();
++it) {
Variables.Add(*it);
}
You should consider using a vector of boost::shared_ptr, or a boost::ptr_vector instead of a vector of pointers.
I can give you a huge general overview of "don'ts" relating to your code.
You are right in this case to use classes "deriving" from simple_instr but you are doing it wrong, given that later on you do a switch statement based on type. A switch-statement based on type (rather than state) is an anti-pattern. You should be calling some virtual method of your base class.
You almost certainly do not want your derived class to copy from the base class. You want to construct it with the parameters to construct its base-class.
You want a vector of the base class pointers? And to manage lifetime probably shared_ptr
const-correctness. Some of your methods like size() should certainly be const. For others you might want two overloads.