Thread: getline()
View Single Post
Old 08-30-2005, 01:48 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
Than how would i replace the videoNum with videoNum2? videoName with videoName2?
You don't, you create an array, and read into that so you'll loop through that instead:
Code:
int i = 0;
long video_array[SIZE];
while(i < SIZE)
{
    cout << "What video number? ";
    cin >> video_array[i++];
}
But are you looking for something that will act as some kind of database ?
Then the std::vector might be a better storage facility, somethign like this:
Code:
#include <string>
#include <iostream>
#include <vector>

class video{
private:
  std::string name;
  long number;
  video(){};
public:
  video(long n):number(n){};
  video(std::string str):name(str){};
  video(long n, std::string str){
    number = n;
    name = str;
  };
  void set_num(long n){number = n;};
  void set_name(std::string str){name=str;};
  long get_num(void){return number;};
  std::string get_name(void){return name;};
};


void reset_istream(void);

int main(){
  std::vector <video> videos;
  std::string name;
  long number;
  int choice;
  while(true)
    {
      std::cout << "\t.------------------------." << std::endl;
      std::cout << "\t|  Welcome to Videos DB  |" << std::endl;
      std::cout << "\t|------------------------|" << std::endl;
      std::cout << "\t| 1) Add video           |" << std::endl;
      std::cout << "\t| 2) Find video          |" << std::endl;
      std::cout << "\t| 3) Delete video        |" << std::endl;
      std::cout << "\t| 4) Show all videos     |" << std::endl;
      std::cout << "\t| 5) Show specific video |" << std::endl;
      std::cout << "\t| 6) quit                |" << std::endl;
      std::cout << "\t'------------------------'" << std::endl;
      std::cout << "\t  Your choice: ";
      std::cout.flush();
      std::cin >> choice;
      switch(choice)
	{
	case 1:
	  while(true)
	    {
	      std::cout << "  -- Add a video --    " << std::endl;
	      std::cout << "  What is the Video Number? (0 for exit): ";
	      std::cout.flush();
	      std::cin >> number;
	      reset_istream();
	      if(!number)
		break;
	      std::cout << "  What is the Video Name?: ";
	      std::cout.flush();
	      std::getline(std::cin, name, '\n');
	      video temp(number, name);
	      videos.push_back(temp);
	    }
	  break;
	case 2:
	  /* left as an exercise for the student */
	  break;
	case 3:
	  /* left as an exercise for the student */
	  break;
	case 4:
	  for(int i=0; i < videos.size(); ++i)
	    {
	      std::cout << "\t------------------------" << std::endl;
	      std::cout << videos[i].get_num() << std::endl;
	      std::cout << videos[i].get_name() << std::endl;
	    }
	  break;
	case 5:
	  /* left as an exercise for the student */
	  break;
	case 6:
	  return 0;
	}
    }
  /* should we ever get here it will be an error */
  return -1;
}

void reset_istream(void)
{
  if(std::cin.eof())
    std::cin.clear();
  else
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}
I know it's a lot of code, which I've realy havn't taken my time to explain, I don't have the time right now, but if you want I'll be back later and will explain everything.

This kinda reminds me of what I came up with in this thread you might have a peek in that to see some more on the usage of vectors for storage.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote