initializing arrays

Pages: 12
Oct 21, 2008 at 4:03am
when ever i try to initialize this array this way it gives an error
char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

this is the error i get
error C2059: syntax error : '{'

i cant think for the life of me why i get this

can anyone please help?
Last edited on Oct 21, 2008 at 5:35am
Oct 21, 2008 at 5:57am
It run fine for me. But I think the problem is that your array size is small. Try make it 27?
Oct 21, 2008 at 6:09am
It runs fine by me to, with the size 26. The problem must be somewhere else, can you upload a bit more of the code?
Oct 21, 2008 at 6:12am
Perhaps the error lies on the line before this? Post your entire code, then we might be able to see what the problem is.
Oct 21, 2008 at 6:40am

#include "pTree.h"

class Main
{
public:

void createMine(int i);

string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };

int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}};


private:


};

it wont let me compile!
i hope someone can help
Last edited on Oct 21, 2008 at 6:41am
Oct 21, 2008 at 6:51am
Wy are you using type string? I would recommend to use type char[], however, both types works fine on my compiler. Try do compile this code:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
std::string alphabet[26]    //you can also use char, but then you have to
                                         // change all the " into '
 = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };

int main()
{
    std::cout<<alphabet[0];
    return 0;
}


If it does compile, the problem is somewhere else in your code. If it dont... Wich compiler do you use?
Oct 21, 2008 at 7:00am
ok
when i try to implement the class through Main.cpp
which consists of the following, i get that error

i am using Microsoft visual c++ 6.0

this is Main.cpp

#include "Main.h"

void Main::createMine(int i)
{/*
pNode("Mine " + alphabet[i], production_hours[0][i], production_hours[1][i]);

if(node.getName() == "Mine b")
{
node.setRight(createMine(i+3);
}
else if(node.getName() == "Mine e")
{
node.setLeft(createMine(i+3);
node.setRight(createMine(i+4);
}
else if(node.getName() == "Mine c" || node.getName() == "Mine h" ||node.getName() == "Mine g"|| node.getName() == "Mine f" || node.getName() == "Mine i")
{
//stop
}
else
{
node.setLeft(createMine(i+2));
node.setRight(createMine(i+3));
}
*/
}



Oct 21, 2008 at 8:40am
For your array, when you declare it with the values you want it to hold you don't need to declare the size of array; the computer counts the variables.

char alphabet[] = {'a','b','c','d'};
Oct 21, 2008 at 9:21am
yeah thats true, but ive tried to getting rid of the 26 and it still gives the same error

i think i might try to use cygwin instead of microsoft visual c++ 6
or is that a bad idea?
Last edited on Oct 21, 2008 at 9:22am
Oct 21, 2008 at 10:10am
Changing your compiler isn't the answer to the problem.
I suggest (as has already been suggested) that you post the whole code as it stands, not bits and peices of it which could be taken out of context.
Oct 21, 2008 at 10:36am
i did post it all. just not in one thing

ill do it again

This file is Main.h

#include "pTree.h"

class Main
{
public:

void createMine(int i);

string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };
int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}};


private:


};




this file is Main.cpp


#include "Main.h"

void Main::createMine(int i)
{/*
pNode("Mine " + alphabet[i], production_hours[0][i], production_hours[1][i]);

if(node.getName() == "Mine b")
{
node.setRight(createMine(i+3);
}
else if(node.getName() == "Mine e")
{
node.setLeft(createMine(i+3);
node.setRight(createMine(i+4);
}
else if(node.getName() == "Mine c" || node.getName() == "Mine h" ||node.getName() == "Mine g"|| node.getName() == "Mine f" || node.getName() == "Mine i")
{
//stop
}
else
{
node.setLeft(createMine(i+2));
node.setRight(createMine(i+3));
}*/
}

thats all my code for this class.
sorry its not indented and stuff
i dont know how to post it so it looks like code

any ideas as to why it i get the error: error C2059: syntax error : '{'
in regards to
string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };
Oct 21, 2008 at 11:04am
Like i have told you before, the alpabet[26] don't cause an error when i use it. However, the syntax
int var[x][y]={{...}{...}}
, as you use in the line after declaring alphabet, does cause an error when i try to compile it. It looks like the same error with another compiler: it says i cant use "{" (->the second "{") before "}" token. Could that be the problem?
Last edited on Oct 21, 2008 at 11:07am
Oct 21, 2008 at 11:12am
The string class operator[] doesn't do what you think, its not storing an array of strings like you hope; there is no operator for that.

You need to store an array of characters not strings:
The string operator[] searches through the current string object e.g.

1
2
string str ("Concurrent");
str[0]; // This is 'C' 


Go back to your original code using the char array and force that to work. If you cant make your original design work change it a bit

Ex:
1
2
3
4
5
6
7
8
char alpha[] = {'a','b','c'};
char alpha[] = "abcde";

char alpha[26];

for (loop through alpha) {
   // Add characters to array
}
Last edited on Oct 21, 2008 at 11:13am
Oct 21, 2008 at 11:19am
cheers, ill see if this works

and also, how would you create an Array of strings?
Last edited on Oct 21, 2008 at 11:19am
Oct 21, 2008 at 11:21am
@Umz
The following code, using a array of type string, works fine by me:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
std::string alphabet[26]
 = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };

int main()
{
    std::cout<<alphabet[7]<<alphabet[0]<<alphabet[11]<<alphabet[11]<<alphabet[14];
    return 0;
}
Oct 21, 2008 at 11:30am
ok, so the alphabet thing is sorted. I still get a problem with the same error for the other array though

ill show you

my Main.h file


#include "pTree.h"

#include <string>
using namespace std;

class Main
{
public:

Main();
void createMine(int i);

string alpha;

int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50},
{10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}};


private:


};

now my Main.cpp file

#include "Main.h"

Main::Main()
{
alpha = "abcdefghijklmnopqrstuvwxyz";
}
void Main::createMine(int i)
{

}

so yeah, i have the same issue with the following array as i did with the alphabet array

int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50},
{10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}};

cheers to any one that can help
Oct 21, 2008 at 11:50am
have you even read my comment:


Like i have told you before, the alpabet[26] don't cause an error when i use it. However, the syntax
int var[x][y]={{...}{...}}
, as you use in the line after declaring alphabet, does cause an error when i try to compile it. It looks like the same error with another compiler: it says i cant use "{" (->the second "{") before "}" token. Could that be the problem?
Oct 21, 2008 at 12:08pm
sorry i missed your comment about the alphabet array
i dont know why that doesnt work. its being a lame ass

any way
ill try this and see if it works
int production_hours[2][9] = {100, 200, 50, 25, 70, 120, 80, 60, 50},
{10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9};

Nope
i got the same error

is that what u meant for me to do?
Last edited on Oct 21, 2008 at 12:12pm
Oct 21, 2008 at 12:48pm
Try to use the #format if you use code in your message, thats easier to read.

I'm not very familiar with multideminsinal arrays. After trying i found the following code. This code does compile, and it works as it has to work.
However, i cant find the differnce between this and your first code... :)

1
2
3
4
5
6
7
8
9
#include <iostream>

int array[2][2]={{1,2},{3,4}};

int main()
{
        std::cout<<array[0][0];
        return 0;
}
Oct 21, 2008 at 1:00pm
man this is laaaame hahah
i wish mine would just work magically

umm
thanks for the help scipio

ill try to initialize the array as two different arrays and in a different way

if you somehow stumble accross my problem and find a way to fix it let me know

cheers
Pages: 12