Thread: Struct Help...
View Single Post
Old 08-29-2005, 03:20 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I am not sure what you would like to do but here is a starter. See if this is usefull to you. If you don't know what a static variable is then check out the 12th reply in this thread:
return types

Anyway, here is the full compilable code:
Code:
#include <cstdlib> //size_t
#include <cstring> //strcpy
#include <iostream>

static const std::size_t SIZE = 10;
struct customerRec 
{
  char Name_[20];
  long Num_;
};
struct videoRec 
{
  long Num_;
  char Name_[30];
  bool bRented_;
  static std::size_t Amount_;
  customerRec customer_;
};

//As soon as a videoRec instance exists we start with 0 video's.
//Each time we add a video, we increase the total amount by 1.
std::size_t videoRec::Amount_ = 0;


int main()
{
  videoRec videoLib[SIZE];
  //Let's create a customerRec object for easy object managing.
  customerRec TheCustomer;
  
  //First we *could* enter the customer data.
  TheCustomer.Num_ = 123456;
  std::strcpy(TheCustomer.Name_ , "Joe Bloggs");
  
  //Then we *could* enter the video data.
  //NOTE: you could first add all the video data instead. 
  videoLib[0].Num_ = 1;
  std::strcpy(videoLib[0].Name_, "Alexander");
  videoLib[0].bRented_ = false; //Not rented yet.
  //Add then we increase the total video amount by 1.
  ++videoRec::Amount_;
  
  //The movie "Alexander" is rented so let's add the customer data.
  videoLib[0].customer_ = TheCustomer;
  //And don't forget to tag this movie as "rented":
  videoLib[0].bRented_ = true;
  
  //So how do we find out where to append the next movie?
  //Why easy: we kept track of how many video's are in the library:
  unsigned index = (unsigned) videoRec::Amount_;
  videoLib[index].Num_ = index+1;
  std::strcpy(videoLib[index].Name_, "Terminator 2");
  videoLib[index].bRented_ = false; //Not rented yet.
  ++videoRec::Amount_;
  
  /***** Let's see what we got so far *********/
  
  for(std::size_t i = 0;   i < videoRec::Amount_; ++i)
  {
    std::cout << "Video Number: " << videoLib[i].Num_ << std::endl;
    std::cout << "Video Titile: " << videoLib[i].Name_ << std::endl;
    std::cout << "Rented (1 =  true, 0 = false): " << videoLib[i].bRented_ << 
      std::endl << std::endl;
    if(videoLib[i].bRented_ == true)
    {
      std::cout << "!!! rented by !!!" << std::endl;
      std::cout << videoLib[i].customer_.Name_ << " [ID = " << 
        videoLib[i].customer_.Num_ << "]" << std::endl << std::endl;
    }
  }
  
  return 0;
}
- Note how I added the underscore ( _ ) character for members of a struct/class. This way we know it's a member of a struct of a class and not something else.
- Note that I use std::size_t to increase the portability of the code.
- Also observe that I changed your names a bit.
For example in struct customerRec you use member names like "customerName" and "customerNum". But that "customer" is redundant since that member refers to that specific struct implicitly .
- There is tons of info on this site about the usage of "std::" in case you need it.
- The header "cstdlib" is officially needed to use "std::size_t".
- The header "cstring" is officially needed to use "std::strcpy". Many compilers don't require the inclusion of this header but we do it nicely according to the ISO standards here if we can .
- Try to replace using "char*" types with "std::string" (in header <string>) as soon as possible.
- Observe how convenient a tracker like our static variable ( std::size_t videoRec::Amount_ ) can be like we implemented in our code. Later when you learn the ways of constructors you'll have even more neat options. But this one is nice already.
- Be clear in what you want. Try to use some decent grammar. It's very important. C++ is hard enough as it is.
__________________
Valmont is offline   Reply With Quote