Alright guys,
I've been looking at this on google and I can't seem to find an answer. Any help is definitely appreciated!
Here is the code, then I will explain my problem.

 void printMenu()
 {
             cout<<"||============================||"<<endl;     
             cout<<"||=========Main Menu==========||"<<endl;
             cout<<"||===1.Begin DNA Creation";
             cout<<"||===2.Load the Files";
             if(!dna1[].strand1){
             cout<<"<0>";}
             else{
             cout<<"<"<<sizeof(dna1.strand1)<<">";}             
             cout<<endl; 
             cout<<"=========||"<<endl;
             cout<<"||===3.Save the Files=========||"<<endl; 
             cout<<"||===4.Exit===================||"<<endl;
             cout<<"||============================||"<<endl;
             cout<<"||===Enter a Choice <1-4>: ";
 }

Now I'm assuming that my variable dna1 in the structure I created "dna" would behave like any other array and I won't need to pass it over. If this is incorrect then thanks for the heads up.

Basically this function is supposed to print the menu and it will check if the user has ran the function to create the array. If they havent, meaning they just started it up, The desired output is this:

||==============================||     
||=========Main Menu============||
||===1.Begin DNA Creation=======||
||===2.Load the DNA Strand<0>===||
||===3.Save the DNA Strand======||
||===4.Exit=====================||
||==============================||
||===Enter a Choice <1-4>:           

And if the first function (option 1) has been run then I would like it to print out the size of it.

Is it not possible to get the size of an array or even check to see if it's initialized?
I know

sizeof(int);

would run fine and print out 4.
Is there a way to get this to work? If anymore info is needed than let me know; I tried to include everything I thought was needed.

Here's the structure data incase it's needed:

struct dna{     
       int location;
       char strand1;
       char strand2;};         

Thank you

Couldn't you just have a count set to = 0 at the beginning and each time a value get's inserted into the array, it increments by 1? Or, if you just need to know whether someone has entered something - Have a Boolean? Here's an example anyway:

#include <iostream>

using namespace std;
int main(int argc, char *argv[]) {

    int numbers[8];
    int count = 0;

    for(int i=0; (i < 3); i++)
    {
        numbers[i] = i+i+2*i;
        count++;
    }

    cout << count; // prints "3" only 3 elements added to an 8 integer array

}

Yes I could do that. However I want the actual number of bytes it takes up. And taking the number of items in an int array and multiplying by 4 seems tedious so I was hoping there was a legit way. Thank you for your reply, I'm probably going to end up doing it that way unless someone has a solution ;3

Hey,

I don't think it is possible with Arrays (Someone correct me?) I've only ever seen/used counting the elements as they have been inserted.

Can you not use STL vectors?

You have not posted the declaration of dna1, so it's hard to say exactly where you are going with this. Regardless, having the brackets operator without an array index seems like an error.

if(!dna1[].strand1){

Does the compiler let you do this? Then later you have this...

cout<<"<"<<sizeof(dna1.strand1)<<">";} 

Is dna1 an array or not? Seems like at least one of these lines can't make sense, even if you add an array index. Again, need to see the declaration of dna1 and whether it is an array.

sizeof(dna1.strand1)

seems like it would be 1, since the size of a character is 1 and strand1 is defined as a character.

I am guessing that dna1 is an array, and a dynamic array at that from your post description. Looking at sizeof(dna1) rather than sizeof(dna1.strand1) might be more in line with what you are looking for, but still won't work, I think. Read on.

If dna1 is a pointer to dynamic array, I don't see how you can use sizeof because you'd be returning the size of the pointer, which on a 32-bit machine would be 4. Not what you are looking for. If dna1 is a STATIC array, you might be in business, but I'm a little confused about what exactly you're doing as far as the counting.

#include <iostream>
#include <cstdlib>
using namespace std;


// total size = 6 on a 32 bit machine, but might be 8 due
// to optimization
struct dna{
       int location; // size = 4
       char strand1; // size = 1
       char strand2;}; // size = 1

int main()
{
    dna dna1[10];
    dna* dna2 = new dna[10];
    cout << sizeof(dna1) << endl; // prints 60 or 80
    cout << sizeof(dna) << endl; // prints 6 or 8
    cout << (sizeof(dna1) / sizeof(dna)) << endl; // prints 10
    cout << sizeof(dna2) << endl; // prints 4
    delete []dna2;
    return 0;
}
commented: Very informative, Thanks +0

Thanks everyone.
Vernon, I will definitely check out the pointers, that will probably help me alot. No,

if(!dna1[].strand1){ 

did not work. I apologize for leaving the code like that, I was just very unsure where to go with it.

Phorce, Thank you also! I shall do some research on the STL vectors also =]

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.