Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-29-2005, 12:45 AM   #1 (permalink)
bradleyc
Registered User
 
Join Date: Aug 2005
Posts: 17
bradleyc is on a distinguished road
Struct Help...

video.h

Code:
const int SIZE = 10;
struct customerRec {

  char customerName[20];
  long customerNum;
};
struct videoRec {
  long videoNum;
  char videoName[30];
  int borrowed;
  customerRec customer;
};
videoRec videoLib[SIZE]= { {123456, "Alexander",1, {"Joe Bloggs",23215}},
{24687, "Terminator 2", 1, {"Fred Nurks", 23456}},
{54321, "Titanic", 0,{"", 0}}};
main.cpp

Code:
cout << "  What is the Video Number?: ";
cin >> videoNum;
cout << endl <<"  What is the Video Name?: ";
cin >> videoName;
videoLib.videoNum = videoNum;
videoLib.videoName = videoName;
displayMenu()
How do i add new videoNum and videoName values to the end of that videoRec videoLib array ... in the video.h, isnt not like what im trying to do in the main.cpp is it?
bradleyc is offline   Reply With Quote
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
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
when to use a struct sde Standard C, C++ 7 09-10-2008 05:07 AM
really stuck on deallocation problem MealMan401 Standard C, C++ 6 04-11-2005 03:18 AM
File I/O and arrays JaySun Standard C, C++ 6 03-24-2005 11:43 PM
. vs -> sde Standard C, C++ 3 02-23-2004 02:21 AM
delete parts of an array Apodysophilia Java 4 05-07-2003 01:11 PM


All times are GMT -8. The time now is 06:02 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting