hi, i'm having problems with my homework. I have to create a Base classes from which 2 classes have to be derived, then i have to write a program to test their function. The problem that i'm having is that i'm taking the input from the keyboard and passing it to a function in the base class. I dont get any error/warning messages but when i run the debug, it stops when it takes the first keyboard input. Programs stops when the input from line 33 (when B/b is selected) or 47 (when R/r is selected), of the Library.cpp file, is copied on the Holding.cpp file line 28 "strcpy_s(title, strlen(name), name)." Any suggestion on what could it be????

[LIST=1]
[*]//holding.h - base class

[*]#ifndef _HOLDING_H
[*]#define _HOLDING_H

[*]class Holding
[*]{
[*]    protected:
[*]	    char *title;
[*]		int call;
[*]    public:
[*]		Holding();// {call = 0;}
[*]		Holding(const Holding& hold);// : title(hold.title), call(hold.call) { }
[*]		~Holding();
[*]		virtual void print() = 0;
[*]		void setCall(int num);
[*]		void setTitle(const char *name);
[*]};

[*]#endif
[/LIST]
[LIST=1]
[*]#include <iostream>
[*]#include <string.h>
[*]#include "holding.h"
[*]#include <stdio.h>

[*]using namespace std;

[*]Holding::Holding()
[*]{
[*]	call = 0;
[*]}
[*]Holding::Holding(const Holding &hold)
[*]{
[*]	title = new char [strlen(hold.title) + 1];
[*]	strcpy_s(title, strlen(hold.title), hold.title);
[*]	call = hold.call;
[*]}
[*]Holding::~Holding()
[*]{
[*]	delete [] title;
[*]	call = 0;
[*]}
[*]void Holding::setCall(int num)
[*]{
[*]	call = num;
[*]}
[*]void Holding::setTitle(const char *name)
[*]{
[*]	title = new char [strlen(name) + 1];
[*]	strcpy_s(title, strlen(name), name);
[*]}
[/LIST]
[LIST=1]
[*]//book.h - derived class 1
[*]#ifndef _BOOK_H
[*]#define _BOOK_H
[*]#include "holding.h"

[*]class Book : public Holding
[*]{
[*]	private:
[*]        char *author;
[*]    public:
[*]		Book() : Holding() {author = new char;};
[*]		Book(const Book& bk) : Holding(bk) {author = new char;}
[*]		~Book();
[*]		virtual void print();
[*]		void setAuthor(const char *auth);
[*]};

[*]#endif
[/LIST]
[LIST=1]
[*]#include "book.h"
[*]#include "holding.h"
[*]#include <iostream>
[*]#include <string.h>
[*]#include <stdio.h>

[*]using namespace std;

[*]void Book::setAuthor(const char *auth)
[*]{
[*]	author = new char[strlen(auth) + 1];
[*]	strcpy_s(author, strlen(auth), auth);
[*]}
[*]Book::~Book()
[*]{
[*]	delete [] author;
[*]}
[*]void Book::print()
[*]{
[*]	cout<< "BOOK: " << author << " \"" << title << "\" " << call << endl;
[*]}
[/LIST]
[LIST=1]
[*]//record.h - derived class 2
[*]#ifndef _RECORD_H
[*]#define _RECORD_H
[*]#include "holding.h"

[*]class Record : public Holding
[*]{
[*]	public:
[*]		enum format {L, C, R, D};
[*]	private:
[*]        char *performer;
[*]		format frm;
[*]        static char *snames[4];
[*]    public:
[*]		Record() : Holding() {performer = new char;}
[*]		Record(const Record& rec) : Holding(rec) {performer = new char;}
[*]		~Record();
[*]		virtual void print();
[*]		void setPerformer(const char *auth);
[*]		void setFormat(char f);
[*]};

[*]#endif
[/LIST]
[LIST=1]
[*]//record.cpp
[*]#include "holding.h"
[*]#include "record.h"
[*]#include <iostream>
[*]#include <string.h>
[*]#include <stdio.h>

[*]using namespace std;

[*]char *Record::snames[4]= {"LP", "Cassette", "Reel-to-Reel", "CD" };

[*]void Record::setPerformer(const char *auth)
[*]{
[*]	performer = new char [strlen(auth) + 1];
[*]	strcpy_s(performer, strlen(auth), auth);
[*]}
[*]Record::~Record()
[*]{
[*]	delete [] performer;
[*]}
[*]void Record::setFormat(char f)
[*]{
[*]	if (f == 'l' || f == 'L')
[*]		frm = L;
[*]	else if (f == 'c' || f == 'C')
[*]		frm = C;
[*]	else if (f == 'r' || f == 'R')
[*]		frm = R;
[*]	else if (f == 'd' || f == 'D')
[*]		frm = D;
[*]}
[*]void Record::print()
[*]{
[*]	cout << "\"" << title << "\" " << performer << " (" << snames[frm] << ") " << call << endl;
[*]}
[/LIST]
[LIST=1]
[*]//record.h 
[*]#ifndef _LIBRARY_H
[*]#define _LIBRARY_H
[*]#include "book.h"
[*]#include "record.h"
[*]#include "holding.h"

[*]void main();

[*]#endif
[/LIST]
[LIST=1]
[*]#include <iostream>
[*]#include <string.h>
[*]#include <stdio.h>
[*]#include "library.h"
[*]#include "book.h"
[*]#include "record.h"
[*]#include "holding.h"

[*]using namespace std;

[*]Holding *holdLib[5];
[*]Book *bk;
[*]Record *rec;
[*]char select;
[*]const char *title;
[*]const char *name;
[*]int call;
[*]char format;

[*]void main()
[*]{
[*]	//count = 5;
[*]	//holdLib = new Holding [5];

[*]	cout << "Enter holdings to be stored in a list: " << endl;
[*]	cout << endl;

[*]	for (int c = 0; c < 5; c++)
[*]	{
[*]		title = new char[100];
[*]		name = new char[100];
[*]		
[*]		cout << "Enter B for book, R for recording: ";
[*]		cin >> select;

[*]		if (select == 'B' || select == 'b')
[*]		{
[*]			bk = new Book;
[*]			
[*]			cout << "Enter book title: ";
[*]			cin >> title;
[*]			bk->setTitle(title);

[*]			cout << "Enter book author: ";
[*]			cin >> name;
[*]			bk->setAuthor(name);

[*]			cout << "Enter call number: ";
[*]			cin >> call;
[*]			bk->setCall(call);

[*]			holdLib[c] = bk;
[*]		}
[*]		else if (select == 'R' || select == 'r')
[*]		{
[*]			rec = new Record;

[*]			cout << "Enter record title: ";
[*]			cin >> title;
[*]			rec->setTitle(title);

[*]			cout << "Enter performer: ";
[*]			cin >> name;
[*]			rec->setPerformer(name);

[*]			cout << "Enter format: (L)P, (C)assette, (R)eel-to-Reel, (D)isk: ";
[*]			cin >> format;
[*]			rec->setFormat(format);

[*]			cout << "Enter call number: ";
[*]			cin >> call;
[*]			rec->setCall(call);
[*]			
[*]			holdLib[c] = rec;
[*]		}
[*]		else
[*]		{
[*]			cout << "You have enter the wrong selection: " << endl;
[*]			c--;
[*]		}
[*]	}

[*]	cout << endl;
[*]	cout << "Here are the holdings: " << endl;
[*]	cout << endl;

[*]	for (int n = 0; n < 5; n++)
[*]	{
[*]		holdLib[n]->print();
[*]	}
[*]}
[/LIST]
  1. Is it only when you start your program in debugging mode that you're encountering this problem?
  2. Is your program working correctly outside the debug-mode?

[psychic debugging]
This describes your problem and shows ways to get around it without changing your input logic.
[/psychic debugging]

commented: What does "psychic debugging" mean? +4

> Cin Keyboard inpout to const char*
You want to "INPUT" to something already declared as const - doesn't that strike you as being in the least bit odd?

You say "no changes allowed", then try and change it?


Lesson number 1

#include <iostream>
#include <string.h>
#include "holding.h"
#include <stdio.h>

Decide whether you're a C programmer or a C++ programmer.
C/C++ is like walking down the median line of two lines of traffic moving in opposite directions. One small slip-up, and you're road-kill.
Life is much easier if you're in the flow of one or the other.

Lesson number 2
Having decided that C++ is the way to go, FORGET all about things like "char *" and use a std::string instead.
When you absolutely must have a "char*" for some historic interface, then use the c_str() method of a proper C++ string to get it.

Lesson 3
Lose all those global variables.

Lesson 4
Lose the void main, make it int

commented: Right, nice posting :) +4

This is what i've changed so far:

  1. In the Library.cpp the #include <string.h> has been removed as this was from a previous exercise
  2. "const char *" for just "char *"
  3. No global variables
  4. Void change to Int for the main()

I apologize for not specifying before but we are not allow to use the string lib (why? i don't know because it will make live easier), we have to use a char array.
Whether i run on debug mode or not, it still breaks on the same line

i deserve someone to slap me on the face because of this....where i use strcpy_s, the length that i'm declaring is different to the length that i assigned to the char*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.