Array of structures

Feb 8, 2012 at 9:21am
Hi there everyone,

I have tried searching everywhere for an answer to my question, but I cannot find someone with the same problem as me so I thought I would ask here.

I am trying to create an array of a structure type that I have created myself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//this is in a header file
struct QUESTIONS
{
	char questiontext[80];
	char questionanswer[80];
	char wrongans1[80];
	char wrongans2[80];
	char wrongans3[80];
	char refbook[20];
	int refchapter;
	float refpages;
};

//this is in a separate .cpp file

QUESTIONS* chapter1 = new QUESTIONS[3];
//QUESTIONS chapter1[3];

chapter1[0].questiontext = "How many engines does the Challenger 605 have?";
chapter1[0].answertext = '2';
chapter1[0].refchapter = 1;


I am using MS Visual C++ 2010 Express and the problem is that if I use either one of the declarations for chapter1, I get the red squiggly underline error thing underneath the 0 and the . (the dot) in all 3 of the chapter1[0].questiontext... assignments.

The error that is given to me when I hold the mouse over the 0 is:

"Error: the size of an array must be greater than zero"

And when I hold the mouse over the . I get the following error:

"Error: expected a declaration"


Can anybody see something obvious that I have done wrong? As far as I can see I have already created the array of QUESTIONS structures, so why can't I assign a value to any given value within the array?

Thanks for your help,


Flyer53
Feb 8, 2012 at 11:00am
I'll take a look at this on my lunch break in about an hour if you can wait.
Feb 8, 2012 at 11:43am
 
chapter1[0].questiontext = "How many engines does the Challenger 605 have?";

questiontext is an array of char representing a string. So you need
 
strncpy(chapter1[0].questiontext, 80, "How many engines does the Challenger 605 have?");

Feb 8, 2012 at 12:39pm
iHutch105 - That would be great, thank you

kbw - I tried this and I get an error:

Error: this declaration has no storage class or type specifier

Do I need to #include <string> or something? (I included it and it doesn't seem to change anything)

Also, I get the same errors as in my first post when attempting to assign an integer to the .refchapter part.

Thanks for the replies
Feb 8, 2012 at 12:43pm
No problem, a little delay until my break today. Won't be having it until a little later. I'll have a look as soon as I can.
Feb 8, 2012 at 12:57pm
Great, thanks - but don't spend your whole break on this problem!! Remember to leave some time for yourself!!
Feb 8, 2012 at 1:02pm
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std; 

struct QUESTIONS
{
	char questiontext[80];
	char questionanswer[80];
	char wrongans1[80];
	char wrongans2[80];
	char wrongans3[80];
	char refbook[20];
	int refchapter;
	float refpages;
};

int main ()
{
    QUESTIONS* chapter1 = new QUESTIONS[3];

    strncpy(chapter1[0].questiontext, "How many engines does the Challenger 605 have?", 80);
    chapter1[0].refchapter = 1;

    cout << chapter1[0].questiontext << endl;
    cout << chapter1[0].refchapter << endl;
    
    return 0;
}
Feb 8, 2012 at 1:19pm
Grey Wolf - I tested out your code in a program called Quincy 2010 and it worked out fine, but in my Visual Studio 2010 I still get the errors mentioned above :(
Feb 8, 2012 at 1:29pm
First step, and it could be a kick yourself moment, I would look at is:

 
chapter1[0].answertext = '2'


The struct doesn't have an 'answertext' element.
Last edited on Feb 8, 2012 at 1:30pm
Feb 8, 2012 at 1:33pm
closed account (z05DSL3A)
Interesting, I tested it in VS2010 ... lets see if I can make it fail ...
Feb 8, 2012 at 1:34pm
That is true and I have kicked myself in response, but the same errors as above are still present :(
Feb 8, 2012 at 1:40pm
Hmmm, all working fine on VS6 (oooooold work systems).

EDIT: Everything is being included, yes? The right headers in the right files.
Last edited on Feb 8, 2012 at 1:41pm
Feb 8, 2012 at 2:20pm
Sure you're compiling GreyWolf's code (as iHutch says, with the right headers - I had to add cstring)?

Your original code was broken (you were trying to assign a const char[47] to a const char[80]) but his is fine as it doesn't assign pointers around and instead copies data.
Last edited on Feb 8, 2012 at 2:20pm
Feb 8, 2012 at 2:31pm
Hi guys, I think part of the problem seems to have been that I had this in a separate .cpp file.

I have now moved it into its own .h file and it seems happier (at least with respect to the intellisense error I was getting in the first post).

I still have to check that I have the correct includes in there but I need to tidy up some of the other code before it will run at the moment.

Thanks for your help and I will continue to work away at it!
Feb 8, 2012 at 2:35pm
You can make things so much easier on yourself by using proper C++ strings instead of arrays of char.
Feb 8, 2012 at 2:42pm
I agree, but the SDK I will be using requires *char and I couldn't find a nice way to convert easily between the two in the past.
Feb 8, 2012 at 2:49pm
Convert a string to a char*:
char* theCharPointer = someString.c_str();

Convert a char* to a string
string someString(theCharpointer);
Last edited on Feb 8, 2012 at 2:49pm
Feb 8, 2012 at 3:38pm
But when using .c_str() doesn't that result in a const char? This is not allowed within the SDK I will be using...or is there a way to convert it to a non const? Or am I completely wrong!!
Feb 8, 2012 at 9:58pm
You need to include string.h
Feb 8, 2012 at 11:52pm
closed account (DSLq5Di1)
@flyer53
If you wish to use a std::string with the windows api,
1
2
3
4
5
string str(size + 1, 0) // create an empty string with the required size,
                        // +1 to include room for the null character.

func(&str[0], str.size(), ...) // &str[0] is a char pointer to the first character,
                               // and str.size() represents the maximum number of characters. 
Topic archived. No new replies allowed.