The book Im using. Pg 108 - 124
Objects Abstraction, Data Structures and Design using C++
Author: Elliot Koffman and Paul Wolfgang
I have a problem with this code.
All of these errors are occurring in the Phone_Directory.cpp.
It is in, getline, << , out. .......functions... I tried getting these from C++ standard Library but it still does not work. Can someone help me?
I copied all the classes from the book except for Directory_Entry (Book exercise).
My problems may lie in the Directory_Entry Class.
My Errors: The List is too long to......
phone_directory.cpp(11): error C2079: 'in' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
phone_directory.cpp(11): error C2440: 'initializing' : cannot convert from 'const char *' to 'int'
There is no context in which this conversion is possible
phone_directory.cpp(15): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
1> c:\program files\microsoft visual studio 10.0\vc\include\string(479) : see declaration of 'std::getline'
phone_directory.cpp(15): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
// Directory_Entry
#pragma once
#include<string>
#include<iostream>
using namespace std;
class Directory_Entry
{
private:
//Data fields
string name;
string number;
public:
// Constructors
Directory_Entry(string n, string num):
name(n), number(num)
{}
// Default Constructor
Directory_Entry(): name(""), number("") {}
//Modifiers
void set_number(const string& new_number)
{
number = new_number;
}
//Accessor
string get_name() const {
return name;
}
string get_number() const {
return number;
}
inline friend ostream& operator<<(ostream& os ,const Directory_Entry& per)
{
os << "Given Name: " << per.name << '\n'
<< "Number: " << per.number << '\n' ;
return os;
}
};
// Directory_Entry.pp
#include "StdAfx.h"
#include "Directory_Entry.h"
//Phone_Directory
#pragma once
#include<string>
#include"Directory_Entry.h"
using namespace std;
class Phone_Directory
{
private:
// Directory Entry Definition req
int find(const string& name) const;
void add(const string& name, const string& number);
void remove_entry(int index);
void reallocate();
int size;
int capacity;
Directory_Entry* the_directory;
string source_name;
bool modified;
public:
Phone_Directory(void);
~Phone_Directory(void);
void load_data(const string& source_name);
string lookup_entry(const string& name) const;
string add_or_change_entry(const string& name, const string& number);
string remove_entry(const string& name);
void save();
};
I got error messages from this part only.
//Phone_Directory.cpp
#include "StdAfx.h"
#include "Phone_Directory.h"
#include "Directory_Entry.h"
using namespace std;
void Phone_Directory :: load_data(const string& source_name)
{
this->source_name = source_name;
ifstream in(source_name.c_str());
if(in) {
string name;
string number;
while(getline(in,name)) {
if(getline(in, number))
{
add(name,number);
}
}
in.close();
}
}
void Phone_Directory :: save() {
if(modified){
ofstream out(source_name.c_str());
for(int i = 0; i < size i++)
{
out << the_directory[i].get_name() << endl;
out << the_directory[i].get_number() << endl;
}
out.close();
modified = false;
}
}
string Phone_Directory:: add_or_change_entry(const string& name, const string& number)
{
string old_number = "" ;
int index = find(name);
if(index != -1) {
old_number = the_directory[index].get_number();
the_directory[index].set_number(number);
}
else{
add(name,number);
}
modified = true;
return old_number;
}
string Phone_Directory :: lookup_entry(const string& name) const {
int index = find(name);
if(index != -1) {
return the_directory[index].get_number();
}
else{
return "";
}
}
int Phone_Directory :: find(const string& name) const {
for(int i = 0; i < size; i++) {
if(the_directory[i].get_name() == name)
return i;
}
return -1;
}
void Phone_Directory :: add(const string& name, const string& number) {
if(size == capacity)
reallocate();
the_directory[size] = Directory_Entry(name,number);
size++;
}
void Phone_Directory::reallocate() {
capacity *=2;
Directory_Entry* new_directory = new Directory_Entry[capacity];
for(int i = 0; i< size; i++)
{
new_directory[i] = the_directory[i];
}
delete[] the_directory;
the_directory = new_directory;
}
// PD_Application.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Phone_Directory.h"
#include "Directory_Entry.h"
#include<iostream>
#include<istream>
#include<ostream>
#include<limits>
using namespace std;
void process_commands(Phone_Directory&);
void do_add_change_entry(Phone_Directory&);
void do_lookup_entry(Phone_Directory&);
void do_remove_entry(Phone_Directory&);
void do_save(Phone_Directory&);
int _tmain(int argc, char* argv[])
{
if(argc < 2) {
cerr << "Must specify the name of the data file"
<< " that contains the directory|n" ;
return 1;
}
Phone_Directory the_directory;
the_directory.load_data(argv[1]);
process_commands(the_directory);
}
void process_commands(Phone_Directory& the_directory)
{
string commands[] = {
"Add/Change Entry",
"Look Up Entry",
"Remove Entry",
"Save Directory",
"Exit" };
const int NUM_COMMANDS = 5;
int choice = NUM_COMMANDS -1;
do
{
for(int i = 0; i< NUM_COMMANDS; i++)
{
cout << "Select: " << i << " " << commands[i] << "\n";
}
cin >> choice;
cin.ignore(numeric_limits<int>::max(), '\n');
switch(choice) {
case 0: do_add_change_entry(the_directory); break;
case 1: do_lookup_entry(the_directory); break;
case 2: do_remove_entry(the_directory); break;
case 3: do_save(the_directory); break;
}
}while(choice < NUM_COMMANDS - 1);
}
void do_add_change_entry(Phone_Directory& the_directory) {
string name;
string number;
cout << "Enter name: " ;
getline(cin, number);
string old_number = the_directory.add_or_change_entry(name,number);
if(old_number != " ") {
cout << name << "has been changed in the directory \n" ;
cout << "Old number was " << old_number << "\n";
}
else{
cout << name << "has been added to the directory\n";
}
}
void do_look_up_entry(Phone_Directory& the_directory) {
string name;
cout << "Enter name: " ;
getline(cin, name);
string number = the_directory.lookup_entry(name);
if(number != " ")
{
cout << "The number for " << name << " is" << number << "\n";
}else {
cout << name << " is not in the directory\n";
}
}
void do_remove_entry(Phone_Directory& the_directory)
{
// Excercise
}
void do_save(Phone_Directory& the_directory){
the_directory.save();
}
//return 0;
//};