CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2017
    Posts
    1

    Is a struct declaration using numbers possible?

    I want to write a program that helps me improve my English. I want it to have a database of words and their translations. For this I used struct.
    Code:
    struct vocable
      {
      int number;	
      int level;
      char word[99];
      char translation[99];
      char definition[999];
      };
      
    
    vocable 1 {1, 1, "hello", "Hallo", "Wird genutzt um jemand zu grueßen."}; 
    vocable 2 {2, 1, "goodbye", "Tschuess", "Wird genutzt um sich zu verabschieden."};
    You see I want the declaration to be useable as a number, so I can do this:
    Code:
    int i;
    time_t t;
    int random;
    int j;
    
    time(&t);
    srand((unsigned int)t);   
    random = rand() % 65 + 1;  /* To get a random number */
    
    
    for ( j=0; j<3; j++) 
    {	
      if (random == j.number)
      { 
      cout << 1.word << endl;
      }
    }
    Sadly, this does not work. You, for whatever reason, can't use numbers to declare a new struct variable.
    I have been thinking (and googling) about alternative ways but I really want to stick to the usage of struct.
    If anyone knows how to do this than I would be very pleased. Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Is a struct declaration using numbers possible?

    The identifier naming rules say that identifier names cannot start with digits, so these lines would already contain errors:
    Code:
    vocable 1 {1, 1, "hello", "Hallo", "Wird genutzt um jemand zu grueßen."}; 
    vocable 2 {2, 1, "goodbye", "Tschuess", "Wird genutzt um sich zu verabschieden."};
    What you can do is to create an array or vector of these vocable objects, e.g.,
    Code:
    auto vocables = std::array<vocable, 2>{
        vocable{1, 1, "hello", "Hallo", "Wird genutzt um jemand zu grueßen."},
        vocable{2, 1, "goodbye", "Tschuess", "Wird genutzt um sich zu verabschieden."},
    };
    Now your loop can be something like:
    Code:
    for (size_t j = 0; j < vocables.size(); ++j)
    {
        if (random == vocables[j].number)
        {
            cout << vocables[j].word << endl;
        }
    }
    although you could also consider a range-based for loop, e.g.,
    Code:
    for (const auto& vocable_obj : vocables)
    {
        if (random == vocable_obj.number)
        {
            cout << vocable_obj.word << endl;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is a struct declaration using numbers possible?

    Why do you want to hold number as part of struct vocable? Why not have random index directly the array holding the data? Consider
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    struct vocable
    {
    	int level;
    	char word[99];
    	char translation[99];
    	char definition[999];
    };
    
    const vocable vocs[] = {
    	{1, "hello", "Hallo", "Wird genutzt um jemand zu grueßen." },
    	{1, "goodbye", "Tschuess", "Wird genutzt um sich zu verabschieden." }
    };
    
    int main()
    {
    	const size_t No_vocs = sizeof(vocs) / sizeof(vocs[0]);
    	srand((unsigned int)time(NULL));
    
    	cout << vocs[rand() % No_vocs].word;
    }
    In this, extra elements can be added to the vocs array without knowing how many elements there are as that is calculated at run-time.

    It also might be worth considering having the data stored in a file that is read into an array/vector as part of the program. In this way the data can be changed/added just be editing the file rather than having to change and recompile the program every time.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Is a struct declaration using numbers possible?

    Quote Originally Posted by 2kaud View Post
    In this, extra elements can be added to the vocs array without knowing how many elements there are as that is calculated at run-time.

    It also might be worth considering having the data stored in a file that is read into an array/vector as part of the program. In this way the data can be changed/added just be editing the file rather than having to change and recompile the program every time.
    I don't want to be nit-picky but the size of the vocs array will be calculated and fixed at compile-time. Neither a C style array nor an std::array can ever change size at runtime. For that a dynamic array such as std::vector is needed. That would be the better option if the OP wants to store the vocable data on file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured