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, 04:35 PM   #1 (permalink)
bradleyc
Registered User
 
Join Date: Aug 2005
Posts: 17
bradleyc is on a distinguished road
getline()

With a getline ... can you do something like ...eof?

Code:
long videoNum; char videoName[1];

cout << "  -- Current Record --    " << endl;
cout << "  What is the Video Number?: ";
cin.getline (videoNum, EOF);
cout << "  What is the Video Name?: ";
cin.getline (videoName, EOF);



cout << "  -- New Record --    " << endl;
cout << "  What is the Video Number?: ";
cin >> videoNum2;
cout << "  What is the Video Name?: ";
cin >> videoName2;
Than how would i replace the videoNum with videoNum2? videoName with videoName2?
bradleyc is offline   Reply With Quote
Old 08-29-2005, 08:36 PM   #2 (permalink)
bradleyc
Registered User
 
Join Date: Aug 2005
Posts: 17
bradleyc is on a distinguished road
Code:
clrscr();
cout << "                      " << endl;
cout << "  -- Current Record --    " << endl;
cout << "  What is the Video Number?: ";
cin.ignore();
cin.getline(temp, '\n');
videoNum = atol(temp);
cout << "  What is the Video Name?: ";
cin.ignore();
cin.getline(videoName, '\n');
But now im not sure how i get the second values, and replace them with the previous values found by the getline...

Code:
cout << "                      " << endl;
cout << "  -- New Record --    " << endl;
cout << "  What is the Video Number?: ";
cin >> videoNum2;


cout << "  What is the Video Name?: ";
cin >> videoName2;



Well i have this array ..

Code:
videoRec videoLib[SIZE]= { {123456, "Alexander",1, {"Joe Bloggs",23215}},
{24687, "Terminator 2", 1, {"Fred Nurks", 23456}},
{54321, "Titanic", 0,{"", 0}}};
For example.. if i enter 123456 and Alexander

-- Current Record --
What is the Video Number?: 123456
What is the Video Name?: Alexander

*Find in array ... where videoNum = 123456 & videoName = Alexander

-- New Record --
What is the Video Number?: 987654
What is the Video Name?: Ben Hur

Code:
videoRec videoLib[SIZE]= { {987654, "Ben Hur",1, {"Joe Bloggs",23215}},
{24687, "Terminator 2", 1, {"Fred Nurks", 23456}},
{54321, "Titanic", 0,{"", 0}}};
*Replace in array ...
bradleyc is offline   Reply With Quote
Old 08-30-2005, 12:48 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
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
Old 08-30-2005, 07:02 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Is this thread related to this thread:
Struct Help...

If so, how? What's up?
__________________
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



All times are GMT -8. The time now is 08:25 PM.


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