Newbie C++ question

A

Alex

Hi Guys,

The code presented below does not compile. Could you give me a few
suggestions to make it compile?

/*
This program creates a class called "card" that maintains a library
card
catalogue entry. The class stores a book title, author, and number of
copies
on hand. It stores the title and author as strings and the "the number
on hand"
as an integer. Using a public member function called "store()" to store
a book's
information. It uses a public member function called "show()" to
display the
information. It includes a short "main()" function to demonstrate the
class.
*/

#include <iostream>
using namespace std;

class card {
//private to class "card"
char a, b;
int c;

public:
void store_a(char title);
void store_b(char author);
void store_c(int no);

char show_a();
char show_b();
int show_c();
};

//define functions
void card::store_a(char title)
{
a = title;
cout << "Please enter the title of the book: " << "\n";
cin >> a;
return;
}

void card::store_b(char author)
{
b = author;
cout << "Please enter the author of the book: " << "\n";
cin >> b;
return;
}

void card::store_c(int no)
{
c = no;
cout << "Please enter the number of copies the library owns: " <<
"\n";
cin >> c;
return;
}

char card::show_a()
{
return a;
}

char card::show_b()
{
return b;
}

int card::show_c()
{
return c;
}

//Main function
int main()
{
do{

card s1, s2, no1;
s1.store_a();
s2.store_b();
no1.store_c();
s1.show_a();
s2.show_b();
no.show_c();

}while (a|b|c != 'exit');
return 0;
}

Thank you.
 
K

Kai-Uwe Bux

Alex said:
Hi Guys,

The code presented below does not compile. Could you give me a few
suggestions to make it compile?

/*
This program creates a class called "card" that maintains a library
card
catalogue entry. The class stores a book title, author, and number of
copies
on hand. It stores the title and author as strings and the "the number
on hand"
as an integer. Using a public member function called "store()" to store
a book's
information. It uses a public member function called "show()" to
display the
information. It includes a short "main()" function to demonstrate the
class.
*/

#include <iostream>
using namespace std;

class card {
//private to class "card"
char a, b;
int c;

public:
void store_a(char title);
void store_b(char author);
void store_c(int no);

char show_a();
char show_b();
int show_c();
};

//define functions
void card::store_a(char title)
{
a = title;
cout << "Please enter the title of the book: " << "\n";
cin >> a;
return;
}

void card::store_b(char author)
{
b = author;
cout << "Please enter the author of the book: " << "\n";
cin >> b;
return;
}

void card::store_c(int no)
{
c = no;
cout << "Please enter the number of copies the library owns: " <<
"\n";
cin >> c;
return;
}

char card::show_a()
{
return a;
}

char card::show_b()
{
return b;
}

int card::show_c()
{
return c;
}

//Main function
int main()
{
do{

card s1, s2, no1;
s1.store_a();
s2.store_b();
no1.store_c();
s1.show_a();
s2.show_b();
no.show_c();

}while (a|b|c != 'exit');

what is a|b|c supposed to be? Note that you have neither declared a nor b
nor c in this scope. Also, 'exit' is not a valid literal. Probably you want
that to be std::string( "exit" ).
return 0;
}

You seem to think that char is a string type. It isn't. You should include
the header <string> and use std::string wherever you have used char.



Best

Kai-Uwe
 
W

wittempj

Alex said:
Hi Guys,

The code presented below does not compile. Could you give me a few
suggestions to make it compile?

/*
This program creates a class called "card" that maintains a library
card
catalogue entry. The class stores a book title, author, and number of
copies
on hand. It stores the title and author as strings and the "the number
on hand"
as an integer. Using a public member function called "store()" to store
a book's
information. It uses a public member function called "show()" to
display the
information. It includes a short "main()" function to demonstrate the
class.
*/

#include <iostream>
using namespace std;

class card {
//private to class "card"
char a, b;
int c;

public:
void store_a(char title);
void store_b(char author);
void store_c(int no);

char show_a();
char show_b();
int show_c();
};

//define functions
void card::store_a(char title)
{
a = title;
cout << "Please enter the title of the book: " << "\n";
cin >> a;
return;
}

void card::store_b(char author)
{
b = author;
cout << "Please enter the author of the book: " << "\n";
cin >> b;
return;
}

void card::store_c(int no)
{
c = no;
cout << "Please enter the number of copies the library owns: " <<
"\n";
cin >> c;
return;
}

char card::show_a()
{
return a;
}

char card::show_b()
{
return b;
}

int card::show_c()
{
return c;
}

//Main function
int main()
{
do{

card s1, s2, no1;
s1.store_a();
s2.store_b();
no1.store_c();
s1.show_a();
s2.show_b();
no.show_c();

}while (a|b|c != 'exit');
return 0;
}

Thank you.

when I compile I get, so lets work on this list

martin@jordaan:~$ g++ test1.cpp -o hello
test1.cpp:79:19: warning: multi-character character constant
test1.cpp: In function 'int main()':
test1.cpp:72: error: no matching function for call to
'card::store_a()'
test1.cpp:20: note: candidates are: void card::store_a(char)
test1.cpp:73: error: no matching function for call to
'card::store_b()'
test1.cpp:29: note: candidates are: void card::store_b(char)
test1.cpp:74: error: no matching function for call to
'card::store_c()'
test1.cpp:38: note: candidates are: void card::store_c(int)
test1.cpp:77: error: 'no' was not declared in this scope
test1.cpp:79: error: 'a' was not declared in this scope
test1.cpp:79: error: 'b' was not declared in this scope
test1.cpp:79: error: 'c' was not declared in this scope

her's your code, line numbered

1 #include <iostream>
2 using namespace std;
3
4 class card {
5 //private to class "card"
6 char a, b;
7 int c;
8
9 public:
10 void store_a(char title);
11 void store_b(char author);
12 void store_c(int no);
13
14 char show_a();
15 char show_b();
16 int show_c();
17 };
18
19 //define functions
20 void card::store_a(char title)
21 {
22 a = title;
23 cout << "Please enter the title of the book: " << "\n";
24 cin >> a;
25 return;
26
27 }
28
29 void card::store_b(char author)
30 {
31 b = author;
32 cout << "Please enter the author of the book: " << "\n";
33 cin >> b;
34 return;
35
36 }
37
38 void card::store_c(int no)
39 {
40 c = no;
41 cout << "Please enter the number of copies the library
owns: " <<
42 "\n";
43 cin >> c;
44 return;
45
46 }
47
48 char card::show_a()
49 {
50 return a;
51
52 }
53
54 char card::show_b()
55 {
56 return b;
57
58 }
59
60 int card::show_c()
61 {
62 return c;
63
64 }
65
66 //Main function
67 int main()
68 {
69 do{
70
71 card s1, s2, no1;
72 s1.store_a();
73 s2.store_b();
74 no1.store_c();
75 s1.show_a();
76 s2.show_b();
77 no.show_c();
78
79 }while (a|b|c != 'exit');
80 return 0;
81
82 }

1. line 79, error message: test1.cpp:79:19: warning: multi-character
character constant
That has to do with the ' exit' constant you comapare a char with. to
make this work you have to take a single character termination
specifier, e.g.change this line to

}while (a|b|c != 'q');

2. line 79, error messages:
test1.cpp:79: error: 'a' was not declared in this scope
test1.cpp:79: error: 'b' was not declared in this scope
test1.cpp:79: error: 'c' was not declared in this scope

You didn't declare a, b and c in this scope, being the main function,
you can fix this by adding two lines after line 81:

char a, b;
int c;

3. messages :
test1.cpp:72: error: no matching function for call to
'card::store_a()'
test1.cpp:20: note: candidates are: void card::store_a(char)
test1.cpp:73: error: no matching function for call to
'card::store_b()'
test1.cpp:29: note: candidates are: void card::store_b(char)
test1.cpp:74: error: no matching function for call to
'card::store_c()'
test1.cpp:38: note: candidates are: void card::store_c(int)

You define methods for class card to store private data like:
10 void store_a(char title);
11 void store_b(char author);
12 void store_c(int no);

So you have to call them with an argument, so change lines 72-74 to
e.g.
s1.store_a('a');
s2.store_a('b');
no.store_c(1);

4.error message: test1.cpp:77: error: 'no' was not declared in this
scope
This is prabably a typo, change line 71 to

card s1, s2, no.

When you change it to this it will compile, but then you have to start
fixing some of the logic - as the while loop will turn out to be
tricky, but thats something I suggest you study first yourself
 
P

Philip Potter

Alex said:
Hi Guys,

The code presented below does not compile. Could you give me a few
suggestions to make it compile?

Hi there. What errors are you receiving? What tools are you using? (This is
covered by the comp.lang.c++ FAQ, question 5.8).
#include <iostream>
using namespace std;

class card {
//private to class "card"

I would replace this comment with "private:" (though this is a style issue,
so feel free to ignore me).
char a, b;
int c;

public:
void store_a(char title);
void store_b(char author);
void store_c(int no);

char show_a();
char show_b();
int show_c();
};

//define functions
void card::store_a(char title)
{
a = title;
cout << "Please enter the title of the book: " << "\n";
cin >> a;
return;
}

What is the purpose of "title" in this function? You write it to a, then you
overwrite a with input from cin.

Also, "char" doesn't do what you think it does. It stores only a single
character; you probably want std::string (or the wide-character variant).

Similar comments apply to store_b() and store_c()...
char card::show_a()
{
return a;
}

This doesn't show a at all. It merely returns its value, which you do
nothing with below when you call card::show_a(). (Similar comments apply to
show_b() and show_c().
//Main function
int main()
{
do{

card s1, s2, no1;
s1.store_a();
s2.store_b();
no1.store_c();
s1.show_a();
s2.show_b();
no.show_c();

Why do you have 3 separate card variables when you could have achieved the
same with just 1? For example:

card x;
x.store_a();
x.store_b();
x.store_c();
x.show_a();
x.show_b();
x.show_c();

This keeps the 3 bits of data on one card called "x", rather than spreading
it across 3 cards.
}while (a|b|c != 'exit');

This line doesn't do what you think it does. Firstly, there are no variables
named "a", "b" or "c" in scope. (s1, s2 and no1 each have their own a, b and
c, but you can't see them because they are private).

Secondly, 'exit' is not a valid character constant. Single quotes (') are
used for single characters, double quotes(") for character arrays.

Thirdly, | performs a bitwise-or. This almost certainly isn't what you want.
If you actually had 3 std::strings called a, b and c, you would probably
want to write:
} while (a != "exit" && b != "exit" && c != "exit");
...although you may find that input from cin has a trailing \n character and
fails to match these conditions. Stripping the newline is left as an
exercise.
return 0;
}

Good luck!

Philip
 
P

Philip Potter

2. line 79, error messages:
test1.cpp:79: error: 'a' was not declared in this scope
test1.cpp:79: error: 'b' was not declared in this scope
test1.cpp:79: error: 'c' was not declared in this scope

You didn't declare a, b and c in this scope, being the main function,
you can fix this by adding two lines after line 81:

char a, b;
int c;

You mean like this? It still can't find them on my machine.
 
F

Frederick Gotham

Alex posted:
This program creates a class called "card" that maintains a library card
catalogue entry.
The class stores a book title, author, and number of copies on hand.


Since when does a library card have a book title, author, number of copies on
hand... ?

It stores the title and author as strings and the "the number on hand"
as an integer.


Again, why does a library card have an author or a quantity?

<snip code>


Looks like you want a BookRecord, or something to that effect..
 
B

bpuzon

Alex wrote:

Try to post the errors compilers gives you.
The "char" type stores single characters, not strings. Try std::string.

In (a|b|c != 'exit') you try to convince the compiler that 'exit' is a
signle character.
 
J

Joel Haugen

Frederick said:
Alex posted:



Since when does a library card have a book title, author, number of copies on
hand... ?




Again, why does a library card have an author or a quantity?

<snip code>


Looks like you want a BookRecord, or something to that effect..
It is a library card catalog entry....you missed the 2nd line of the
sentence.
 
F

Frederick Gotham

Joel Haugen posted:
It is a library card catalog entry....you missed the 2nd line of the
sentence.


That implies that you have a catalogue full of library cards.
 
H

Howard

Frederick Gotham said:
Joel Haugen posted:



That implies that you have a catalogue full of library cards.

<off-topic>

Says who? This has nothing to do with "library cards", which are ID cards
for users of the library's services.

A "card catalogue" is the collection of cards relating to the books kept in
the library. It's used to help you find where in the library the book you
want is shelved, and also to provide information about the book, such as
author and publisher. (Modern libraries may put the catalogue online, but
when I was in school it consisted of hundereds of drawers full of cards.)

Besides, what do you care? The name of his data structure is hardly
relevant to the question.

-Howard
 
H

Howard

bpuzon said:
Alex wrote:
In (a|b|c != 'exit') you try to convince the compiler that 'exit' is a
signle character.

Not to mention the fact that it's an (incorrect) attempt at combining three
different boolean comparisons into one statement. The sub-expression a|b|c
performs a bitwise-OR on the three (non-existent) variables, not a
logical-OR which would return the truth value of the comparison.

In order to compare three values, and ask if any of them are equal to a
known value, the expression needs to actually contain three comparisons.
The results of each comparison can be ORed together, if desired, but
combining the three left-hand-sides into one expression is not equivalent.

-Howard
 
O

osmium

:

The code presented below does not compile. Could you give me a few
suggestions to make it compile?

/*
This program creates a class called "card" that maintains a library
card
catalogue entry. The class stores a book title, author, and number of
copies
on hand. It stores the title and author as strings and the "the number
on hand"
as an integer. Using a public member function called "store()" to store
a book's
information. It uses a public member function called "show()" to
display the
information. It includes a short "main()" function to demonstrate the
class.
*/

#include <iostream>
using namespace std;

class card {
//private to class "card"
char a, b;

I suggest you abandon the idea of compiling. When you get it to work your
library will only be able to have something less than 257 books. (Since a
char has only 256 values in most systems) You need an array of char or a
string (from STL) , depending on how your course or book is structured. If
you know, or should know, string, use it.

As soon as you are aware of sizable logic flaws you should fix them instead
of continuing to compile. The program won't do what you want when you are
done so continuing to compile is kind of like beating a dead horse.
 
A

Alex

Dear Osmium,

I thank you for your kind words...

I am an Engineer who has little experience programing but an abundance
of ideas that could be put to good use if I can learn this skill (which
I am currently teaching myself by reading a book). Combining this will
my desire to obtain paid employment in Australia, I really must learn
C++ to achieve this simple goal.

With your wise and knowledgeable counsel it will make my journey easier
and less fool hardy. Please forgive me for my dithering stupidity as "I
know not what I do in C++."

Thanks to all and thanks to you.


Regards,


Alex Rodriguez,
Australia.
"The land of the Kangaroo, Koala and Emu!"
 
A

Alex

Dear Martin,

Thank you very much, this is just what I wanted. Assistance to get over
this hurdle so that I can try and work it out myself. If you don't hear
from me, that means I have worked it out.

Regards,

Alex Rodriguez,
Australia.
"The land of the Kangaroo, Koala and Emu!"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top