I have 2 error for my code when i compile and i need some guidance =)
This is a small part of my program.
Here's my code.

I declare my function like below:

void working (Stack& deck, Stack& deckStack, List* column, Stack* stack, bool& loadGame, bool& exitGame, bool& newGame, char user[40], int& num, int& from, int& to, bool& next)
{
    int k =13,j;
    string file, x;
    char a;
    char userInput[2];

It shows error: invalid initialization of reference of type 'Stack&' from expression of type 'std::string'

x = strstr (user, " ");
file = x.erase(0,1);
save(file, deck, deckStack, column, stack); // error here
newGame = true;
break;

second error are as below:
error: expected primary-expression before '{' token
error: expected `;' before '{' token

userInput = {user[5], user[6]};   //error here
 num = atoi(userInput);
 userInput = {user[24], user[25]};  //error here
 from = atoi(userInput); from--;
 userInput = {user[36],user[37]};   //error here
 to = atoi(userInput);
 to--;
 move (3, num, from, to, deckStack, column, stack);

Kindly if anyone know my problem please guide me. Thanks =)

Dani AI

Generated

Nice progress — the second error is coming from two separate problems: using a brace initializer where it is not allowed, and treating a short char array as a C-string without a null terminator.

Arrays cannot be assigned with a brace list after they are declared (that form is for initialization). As hinted, that explains the compile error; as suggested, assigning elements by index avoids the brace-list issue, but you must also supply a terminating '\0' before passing the buffer to C-style parsers like atoi.

Two safer fixes:

  • Use std::string parsing (recommended). Convert the incoming C string to a std::string, check length, then extract and parse substrings with std::stoi so you avoid manual null-termination and get error reporting via exceptions.
std::string s(user);
if (s.size() >= 38) {
    int num  = std::stoi(s.substr(5,2));
    int from = std::stoi(s.substr(24,2)) - 1;
    int to   = std::stoi(s.substr(36,2)) - 1;
    ...
}
  • Or parse in-place with sscanf which reads directly from the C buffer and reports failures:
int num;
if (sscanf(user + 5, "%2d", &num) == 1) { ... }  // check return value

Extra cautions: always bounds-check strlen(user) before indexing, validate characters with isdigit if you expect digits, and prefer std::stoi/std::strtol (they provide error detection) over atoi (which silently returns 0 on failure).

Recommended Answers

All 5 Replies

Erm..i solved the 1st problem~
Now left the second one =) Any helps?

What is user input declared as? It looks as if you a re trying to use an initialize list. Make sure your compiler is able to use that feature if that is what you are doing.

My userInput is declared as array.

Extended initializer lists are only available with C++0x.Maybe you should assign each element by directly indexing it

userInput[0]=user[5]; 
 userInput[1]=user[6];

Thx alot =)
You guys were great~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.