473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ style questions (OOP)

Ok, here's the deal. I have a nice class definition and a whole bunch
of inline functions to go along with it in the same file. My question
is, do I need an implementation file for the class, even though I have
an inline for every single function? Or can I just use the class and
it's inlines as a header file for programs that actually use the class
and defined functions?
While I'm here I might as well ask this one too. Here is a function
that I have in the same class that I was talking about above:

inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared. I don't have to declare it private
right? I tried that and it didn't work, I got the same error. How is
the loop structure, am I doing something wrong here? I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.
Thanks in advance for any help, it is greatly appreciated.
neo88
Jul 22 '05 #1
29 1750
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared.


Come on now. Didn't it really say "initialized"?
Is it right? What value do you think "quit" has
when you first use it?

--
Regards,
Buster.
Jul 22 '05 #2
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared.


Come on now. Didn't it really say "initialized"?
Is it right? What value do you think "quit" has
when you first use it?

--
Regards,
Buster.
Jul 22 '05 #3
"neo88" <so****@truevine.net> wrote
Ok, here's the deal. I have a nice class definition
and a whole bunch of inline functions to go along
with it in the same file. My question is, do I need
an implementation file for the class, even though I
have an inline for every single function? Or can I
just use the class and it's inlines as a header file
for programs that actually use the class and
defined functions?


You could get away with just the header file, but it's good practice to have
a corresponding .cpp file, even if all it does is include the header file as
a sanity check. As your project grows, you might find that some functions,
old or new, might be better placed out-of-line or you might want to add
version information to the files in static strings or compile-time checks or
static support functions, etc.. Any changes you make in the future will then
affect how your libraries and/or applications are built. Also, unless you
comment it clearly, someone who only sees the hearder file might waste time
looking for the .cpp file. There's value in a consistent build structure.

Claudio Puviani
Jul 22 '05 #4
"neo88" <so****@truevine.net> wrote
Ok, here's the deal. I have a nice class definition
and a whole bunch of inline functions to go along
with it in the same file. My question is, do I need
an implementation file for the class, even though I
have an inline for every single function? Or can I
just use the class and it's inlines as a header file
for programs that actually use the class and
defined functions?


You could get away with just the header file, but it's good practice to have
a corresponding .cpp file, even if all it does is include the header file as
a sanity check. As your project grows, you might find that some functions,
old or new, might be better placed out-of-line or you might want to add
version information to the files in static strings or compile-time checks or
static support functions, etc.. Any changes you make in the future will then
affect how your libraries and/or applications are built. Also, unless you
comment it clearly, someone who only sees the hearder file might waste time
looking for the .cpp file. There's value in a consistent build structure.

Claudio Puviani
Jul 22 '05 #5
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
} [...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.


The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
--
Regards,
Buster.
Jul 22 '05 #6

"neo88" <so****@truevine.net> wrote in message
news:6a**************************@posting.google.c om...
While I'm here I might as well ask this one too. Here is a function
that I have in the same class that I was talking about above:

inline int Startup(void) {
If you include the implementation code here, there is no need for the
"inline" directive.
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
Missing comment marks here, before "IO stuff"? (Lousy comment, by the way.)
std::cout << "Do you wish to continue?(y/n)\n"; while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
Typo? There should be another "&&" between those two lines. Is there?

And why are you comparing a short (quit) against a char ('y', etc.)? Maybe
that's what that error means..there is no variable called quit that matches
the comparison against a char? (Just a guess.)

Also, the *entire* boolean expression has to be in parentheses, not just the
individual sub-expressions.
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
Another strange comment. Pretty obvious that Close() calls Close(), isn't
it?

But why are you calling Close() (whatever that is) inside your loop? Do you
want to "close" every time through the loop? And is there any matching Open
function?
}
Where's the rest of it? You're two closing braces short here.

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared.
Where does quit ever get a value assigned to it?

I don't have to declare it private right? I tried that and it didn't work, I got the same error. How is
the loop structure, am I doing something wrong here? I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.
That's a boolean operator, not a bitwise operator. The bitwise operator is
&, not &&.
Thanks in advance for any help, it is greatly appreciated.
neo88

Jul 22 '05 #7
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
} [...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.


The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
--
Regards,
Buster.
Jul 22 '05 #8

"Howard" <al*****@hotmail.com> wrote in message
news:c5********@dispatch.concentric.net...
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {


Oh, and I missed those lines. You can't stream data into constant
expressions. I take it you wanted to enter the data into your quit
variable, since that's what you're using to compare againt in your while
loop. You need to stream into a variable.

Howard

Jul 22 '05 #9

"neo88" <so****@truevine.net> wrote in message
news:6a**************************@posting.google.c om...
While I'm here I might as well ask this one too. Here is a function
that I have in the same class that I was talking about above:

inline int Startup(void) {
If you include the implementation code here, there is no need for the
"inline" directive.
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
Missing comment marks here, before "IO stuff"? (Lousy comment, by the way.)
std::cout << "Do you wish to continue?(y/n)\n"; while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
Typo? There should be another "&&" between those two lines. Is there?

And why are you comparing a short (quit) against a char ('y', etc.)? Maybe
that's what that error means..there is no variable called quit that matches
the comparison against a char? (Just a guess.)

Also, the *entire* boolean expression has to be in parentheses, not just the
individual sub-expressions.
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
Another strange comment. Pretty obvious that Close() calls Close(), isn't
it?

But why are you calling Close() (whatever that is) inside your loop? Do you
want to "close" every time through the loop? And is there any matching Open
function?
}
Where's the rest of it? You're two closing braces short here.

This won't compile. The gcc says that I can't use the
short quit;
because it hasn't been declared.
Where does quit ever get a value assigned to it?

I don't have to declare it private right? I tried that and it didn't work, I got the same error. How is
the loop structure, am I doing something wrong here? I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.
That's a boolean operator, not a bitwise operator. The bitwise operator is
&, not &&.
Thanks in advance for any help, it is greatly appreciated.
neo88

Jul 22 '05 #10

"Howard" <al*****@hotmail.com> wrote in message
news:c5********@dispatch.concentric.net...
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {


Oh, and I missed those lines. You can't stream data into constant
expressions. I take it you wanted to enter the data into your quit
variable, since that's what you're using to compare againt in your while
loop. You need to stream into a variable.

Howard

Jul 22 '05 #11
neo88 wrote:
ABCDEFGH
12345678

Oh really?
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #12
neo88 wrote:
ABCDEFGH
12345678

Oh really?
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #13

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:tY********************@speakeasy.net...
neo88 wrote:
ABCDEFGH
12345678

Oh really?
--


?????? Where did THAT come from ??????

Jul 22 '05 #14

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:tY********************@speakeasy.net...
neo88 wrote:
ABCDEFGH
12345678

Oh really?
--


?????? Where did THAT come from ??????

Jul 22 '05 #15
"Howard" <al*****@hotmail.com> wrote in message news:<c5********@dispatch.concentric.net>...
"Howard" <al*****@hotmail.com> wrote in message
news:c5********@dispatch.concentric.net...
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {


Oh, and I missed those lines. You can't stream data into constant
expressions. I take it you wanted to enter the data into your quit
variable, since that's what you're using to compare againt in your while
loop. You need to stream into a variable.

Howard


Thanks everyone for all the help. I'm afriad this is a lot of
information for me to digest in one big gulp. To address the "quit"
problem, is this what I need to do:
short quit = 0; // or some such
I hope so. BTW, I like my comments thank you very much.
What did Buster mean by take all the lines with "quit" away? What's
wrong with them?
Oh yeah and the two closing braces are there at the end of the
function, copy and paste mistake... sorry.
One more thing: Should I init "quit" to a variable?
short quit = a;
then stream the input to it like so:
if (std::cin << a) {
std::cout << "Please wait\n";
} else (std::cin << a) {
is the if-else loop a good way to accomplish this?
I'll take all of these suggestions into account and see what I can do
with them. If anyone has anything else to add, please don't hesitate.

My Kung Fu is stronger.
neo88
Jul 22 '05 #16
"Howard" <al*****@hotmail.com> wrote in message news:<c5********@dispatch.concentric.net>...
"Howard" <al*****@hotmail.com> wrote in message
news:c5********@dispatch.concentric.net...
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {


Oh, and I missed those lines. You can't stream data into constant
expressions. I take it you wanted to enter the data into your quit
variable, since that's what you're using to compare againt in your while
loop. You need to stream into a variable.

Howard


Thanks everyone for all the help. I'm afriad this is a lot of
information for me to digest in one big gulp. To address the "quit"
problem, is this what I need to do:
short quit = 0; // or some such
I hope so. BTW, I like my comments thank you very much.
What did Buster mean by take all the lines with "quit" away? What's
wrong with them?
Oh yeah and the two closing braces are there at the end of the
function, copy and paste mistake... sorry.
One more thing: Should I init "quit" to a variable?
short quit = a;
then stream the input to it like so:
if (std::cin << a) {
std::cout << "Please wait\n";
} else (std::cin << a) {
is the if-else loop a good way to accomplish this?
I'll take all of these suggestions into account and see what I can do
with them. If anyone has anything else to add, please don't hesitate.

My Kung Fu is stronger.
neo88
Jul 22 '05 #17
Buster <no***@nowhere.com> wrote in message news:<c5**********@newsg1.svr.pol.co.uk>...
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

[...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.


The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...


Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}

So do I need to put this function before the Startup() function? I
thought it didn't really matter....

My Kung Fu is stronger.
neo88
Jul 22 '05 #18
Buster <no***@nowhere.com> wrote in message news:<c5**********@newsg1.svr.pol.co.uk>...
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

[...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.


The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...


Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}

So do I need to put this function before the Startup() function? I
thought it didn't really matter....

My Kung Fu is stronger.
neo88
Jul 22 '05 #19
neo88 wrote:
Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}
That's a syntax error. You must specify the return type.
So do I need to put this function before the Startup() function? I
thought it didn't really matter....


You don't have to define a function before you call it, but you do have
to declare it.

--
Regards,
Buster.
Jul 22 '05 #20
neo88 wrote:
Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}
That's a syntax error. You must specify the return type.
So do I need to put this function before the Startup() function? I
thought it didn't really matter....


You don't have to define a function before you call it, but you do have
to declare it.

--
Regards,
Buster.
Jul 22 '05 #21
neo88 wrote:
[snip]

inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit; [snip]
Thanks in advance for any help, it is greatly appreciated.
neo88


Please lookup the functions: std::toupper, std::tolower.
These functions allow you to check for responses without
worrying about case:
while ((std::toupper(quit) != 'Y')
|| (std::tolower(quit) != 'n'))
{
//....

If you must have to check for upper and lower case, then
I recommend placing the responses into a string and searching
it:
const string quit_replies("yYnN");
while (replies.find(quit) == string::npos)
{
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #22
neo88 wrote:
[snip]

inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit; [snip]
Thanks in advance for any help, it is greatly appreciated.
neo88


Please lookup the functions: std::toupper, std::tolower.
These functions allow you to check for responses without
worrying about case:
while ((std::toupper(quit) != 'Y')
|| (std::tolower(quit) != 'n'))
{
//....

If you must have to check for upper and lower case, then
I recommend placing the responses into a string and searching
it:
const string quit_replies("yYnN");
while (replies.find(quit) == string::npos)
{
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #23
Thomas Matthews <Th*************************@sbcglobal.net> wrote in message news:<40**************@sbcglobal.net>...
neo88 wrote:
[snip]

inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;

[snip]
Thanks in advance for any help, it is greatly appreciated.
neo88


Please lookup the functions: std::toupper, std::tolower.
These functions allow you to check for responses without
worrying about case:
while ((std::toupper(quit) != 'Y')
|| (std::tolower(quit) != 'n'))
{
//....

If you must have to check for upper and lower case, then
I recommend placing the responses into a string and searching
it:
const string quit_replies("yYnN");
while (replies.find(quit) == string::npos)
{
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


Thanks a lot. That really helps me out! I'll look those up tonight...
didn't know that they exsited. BTW I really like the string idea you
had as well....

My Kung Fu is stronger.
neo88
Jul 22 '05 #24
Thomas Matthews <Th*************************@sbcglobal.net> wrote in message news:<40**************@sbcglobal.net>...
neo88 wrote:
[snip]

inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
}

This won't compile. The gcc says that I can't use the
short quit;

[snip]
Thanks in advance for any help, it is greatly appreciated.
neo88


Please lookup the functions: std::toupper, std::tolower.
These functions allow you to check for responses without
worrying about case:
while ((std::toupper(quit) != 'Y')
|| (std::tolower(quit) != 'n'))
{
//....

If you must have to check for upper and lower case, then
I recommend placing the responses into a string and searching
it:
const string quit_replies("yYnN");
while (replies.find(quit) == string::npos)
{
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


Thanks a lot. That really helps me out! I'll look those up tonight...
didn't know that they exsited. BTW I really like the string idea you
had as well....

My Kung Fu is stronger.
neo88
Jul 22 '05 #25
so****@truevine.net (neo88) wrote in message news:<6a**************************@posting.google. com>...
Buster <no***@nowhere.com> wrote in message news:<c5**********@newsg1.svr.pol.co.uk>...
neo88 wrote:
inline int Startup(void) {
short quit;
std::cout << "Welcome to the Agent system Neo\n"; IO stuff
std::cout << "Do you wish to continue?(y/n)\n";
while (quit != 'y') && (quit != 'Y')
(quit != 'n') && (quit != 'N') {
if (std::cin << 'y') {
std::cout << "Please wait\n";
} else (std::cin << 'n') {
Close(); // calls Close() function
} [...] I also get a
parse error before 'quit' in the while loop just before the bitwise
'and' operator.


The entire condition for the while loop has to be parenthesized:
while ((quit != 'y') && (quit != 'Y') &&
(quit != 'n') && (quit != 'N')) // ...

(You still need to initialize "quit" first though. Hint: there's
no way for you to do that and have the whole function still make
sense. You should remove all of the lines with "quit" in them.)

Regarding "if (std::cin << 'y')": you can't do it like that, I'm
afraid. The following is legal.

// ...
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\n";
}
else if (c == 'n')
{
Close (); // The comment "calls Close() function"
// is worse than useless.
}
else
{
// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...


Buster: I put the IO code you suggested above into my program, and
works fine, thanks. One tiny problem. The gcc says that I can't call
the function Close() because I have used it before it was defined.
It's defined right after Startup(). Here is the Close() function:

inline Close(void) {
std::cout << "Goodbye.\n";
}

So do I need to put this function before the Startup() function? I
thought it didn't really matter....

My Kung Fu is stronger.
neo88


Ok, here is the function right now:

inline int Startup(void) {
char quit; // cahnged this from short to char
std::cout << "Welcome to the Agent system Neo\n"; // IO stuff
std::cout << "Do you wish to continue(y/n)?\n";
while ((quit != 'y') && (quit != 'Y') && // added the Boolean
connector here
(quit != 'n') && (quit != 'N')) {
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\\n";
}
else if (c == 'n')
{
Close(); // The comment "calls Close() function"
// is worse than useless.
}
else
{

// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
}
//if (std::cin << 'y') {
//std::cout << "Please wait...\n";
//} else (std::cin << 'n') {
// Close(); calls Close() function
//}

return 0; // clean up

}

And here are the compiler errors:

Neo2.cpp:122: `quit' undeclared (first use this function)
Neo2.cpp:122: (Each undeclared identifier is reported only once for
each function it appears in.)
Neo2.cpp:133: `Close' undeclared (first use this function)
Neo2.cpp: In function `int Close()':
Neo2.cpp:158: `int Close()' used prior to declaration

How do I declare Close()? It worked fine in the code I had before....
BTW that is commented in there just so you can see it. How do I
declare quit? Doesn't the line
char quit;
do that? If not than I am very confused on what a declaration is....
But on the bright side, after I fix this, the function shouldn't
return any more errors :)

May the Source be with you.
neo88
Jul 22 '05 #26
neo88 wrote:
Ok, here is the function right now:

inline int Startup(void) {
char quit; // cahnged this from short to char
std::cout << "Welcome to the Agent system Neo\n"; // IO stuff
std::cout << "Do you wish to continue(y/n)?\n";
The loop you have below is totally wrong. You should delete it
and start again.

(i) The variable 'quit' has not been initialized. Its value is
indeterminate. (What did you think the value of 'quit' was going
to be?)

(ii) The code I supplied doesn't fit inside the while loop. It was
meant to stand alone. If you need to keep getting a single character
from standard input just change "if (std::cin >> c)" to
"while (std::cin >> c)". However, it would be less confusing to the
user if you read a line at a time, like this:

std::string s;
while (std::getline (s))
{
if (s == "y") // do something;
else if (s == "n") // do something else;
}
// If we reach here, the stream is no longer good.

while ((quit != 'y') && (quit != 'Y') && // added the Boolean
connector here
(quit != 'n') && (quit != 'N')) {
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\\n";
}
else if (c == 'n')
{
Close(); // The comment "calls Close() function"
// is worse than useless.
}
else
{

// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
}
//if (std::cin << 'y') {
//std::cout << "Please wait...\n";
//} else (std::cin << 'n') {
// Close(); calls Close() function
//}

return 0; // clean up

}

And here are the compiler errors:

Neo2.cpp:122: `quit' undeclared (first use this function)
Neo2.cpp:122: (Each undeclared identifier is reported only once for
each function it appears in.)
This error is wrong. Either there's a compiler bug, or these messages
did not come from compiling that function.
Neo2.cpp:133: `Close' undeclared (first use this function)
Neo2.cpp: In function `int Close()':
Neo2.cpp:158: `int Close()' used prior to declaration
These errors are correct. Read them, then fix it.
How do I declare Close()? It worked fine in the code I had before....
BTW that is commented in there just so you can see it. How do I
declare quit? Doesn't the line
char quit;
do that?
Yes it does.
If not than I am very confused on what a declaration is....
But on the bright side, after I fix this, the function shouldn't
return any more errors :)


I'm sorry if my tone seems a little harsh. If you ask me to I'll just
write the function for you, but I get the impression you want to figure
it out yourself. Good luck.

--
Regards,
Buster.
Jul 22 '05 #27
Buster <no***@nowhere.com> wrote in message news:<c5**********@news6.svr.pol.co.uk>...
neo88 wrote:
Ok, here is the function right now:

inline int Startup(void) {
char quit; // cahnged this from short to char
std::cout << "Welcome to the Agent system Neo\n"; // IO stuff
std::cout << "Do you wish to continue(y/n)?\n";


The loop you have below is totally wrong. You should delete it
and start again.

(i) The variable 'quit' has not been initialized. Its value is
indeterminate. (What did you think the value of 'quit' was going
to be?)

(ii) The code I supplied doesn't fit inside the while loop. It was
meant to stand alone. If you need to keep getting a single character
from standard input just change "if (std::cin >> c)" to
"while (std::cin >> c)". However, it would be less confusing to the
user if you read a line at a time, like this:

std::string s;
while (std::getline (s))
{
if (s == "y") // do something;
else if (s == "n") // do something else;
}
// If we reach here, the stream is no longer good.

while ((quit != 'y') && (quit != 'Y') && // added the Boolean
connector here
(quit != 'n') && (quit != 'N')) {
char c;
if (std::cin >> c)
{
if (c == 'y')
{
std::cout << "Please wait\\n";
}
else if (c == 'n')
{
Close(); // The comment "calls Close() function"
// is worse than useless.
}
else
{

// Deal with other possible inputs.
}
}
else
{
// Deal with an input problem.
}
// ...
}
//if (std::cin << 'y') {
//std::cout << "Please wait...\n";
//} else (std::cin << 'n') {
// Close(); calls Close() function
//}

return 0; // clean up

}

And here are the compiler errors:

Neo2.cpp:122: `quit' undeclared (first use this function)
> Neo2.cpp:122: (Each undeclared identifier is reported only once for
> each function it appears in.)


This error is wrong. Either there's a compiler bug, or these messages
did not come from compiling that function.
Neo2.cpp:133: `Close' undeclared (first use this function)
Neo2.cpp: In function `int Close()':
Neo2.cpp:158: `int Close()' used prior to declaration


These errors are correct. Read them, then fix it.
How do I declare Close()? It worked fine in the code I had before....
BTW that is commented in there just so you can see it. How do I
declare quit? Doesn't the line
char quit;
do that?


Yes it does.
If not than I am very confused on what a declaration is....
But on the bright side, after I fix this, the function shouldn't
return any more errors :)


I'm sorry if my tone seems a little harsh. If you ask me to I'll just
write the function for you, but I get the impression you want to figure
it out yourself. Good luck.


Hey, if you want to write the function, please do. I have time
requirement on this thing that is running out fast. I would like to
figure this out, cause I think I'm really close here.... However, this
program needs to get done. So give it a go Buster. In the meantime,
I've got a few things I'd like to try....

May the Source be with you.
neo88
Jul 22 '05 #28
#include <ostream>
#include <iostream>
#include <string>
#include <locale>
#include <cstdlib>

int Close (); // This is a forward declaration.
// Its type must match your definition.

int Startup ()
{
std::cout << "Welcome!\n";
std::cout << "Do you wish to continue? (y/n)\n";

std::string line;
bool quit;
while (std::getline (line, std::cin))
{
if (s.size () == 1)
{
if (std::tolower (s [0], std::locale ()) == 'y')
{
quit = false;
break;
}
else if (std::tolower (s [0], std::locale ()) == 'n')
{
quit = true;
break;
}
}
std::cout << "Please answer 'y' or 'n'.\n";
}

if (std::cin)
{
// Can you verify that 'quit' is always initialized by now?
if (quit) Close ();
else std::cout << "Please wait.\n";
}
else
{
std::cerr << "Stream error.\n";
Close ();
}

return std::rand ();
}

--
Regards,
Buster.
Jul 22 '05 #29
Buster <no***@nowhere.com> wrote in message news:<c5**********@newsg1.svr.pol.co.uk>...
#include <ostream>
#include <iostream>
#include <string>
#include <locale>
#include <cstdlib>

int Close (); // This is a forward declaration.
// Its type must match your definition.

int Startup ()
{
std::cout << "Welcome!\n";
std::cout << "Do you wish to continue? (y/n)\n";

std::string line;
bool quit;
while (std::getline (line, std::cin))
{
if (s.size () == 1)
{
if (std::tolower (s [0], std::locale ()) == 'y')
{
quit = false;
break;
}
else if (std::tolower (s [0], std::locale ()) == 'n')
{
quit = true;
break;
}
}
std::cout << "Please answer 'y' or 'n'.\n";
}

if (std::cin)
{
// Can you verify that 'quit' is always initialized by now?
if (quit) Close ();
else std::cout << "Please wait.\n";
}
else
{
std::cerr << "Stream error.\n";
Close ();
}

return std::rand ();
}


Thanks Buster :)

May the Source be with you.
neo88
Jul 22 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bozo Schmozo | last post by:
Greetings! I've searched groups.google.com already to see if I can determine if using PHP/MySQL (if needed) for a web site I wish to develop. As the subject indicated, it will be a content...
15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
0
by: MSFCLIPPER | last post by:
there are now new open source project presents new programming style design / paradigm vs OOP we invite all programmers to participate in this project project site :...
5
by: iTISTIC | last post by:
Developing a new app and am trying to make this my first truly OOP/3-Tier app. I understand the principles of the presentation, business, and data layers. I do, however, have some questions on...
13
by: M.Siler | last post by:
Let me clarify from my last post. I am not using these 4 questions as the sole screening method. Currently in, the Tampa Bay area (Florida) there is an extreme shortage of C# developers. We have...
4
by: Nemisis | last post by:
Hi everyone, I have 2 classes, Company and Contact, a company can have 1 or more contacts. A contact can only be in one company. I have created a Company class object that contains all the...
0
by: Nemisis | last post by:
Hi everyone, I have 2 classes, Company and Contact, a company can have 1 or more contacts. A contact can only be in one company. I have created a Company class object that contains all the...
3
by: Nemisis | last post by:
Hi everyone, i have currently got a classic asp web application and am wanting to upgrade it to asp.net 2.0, and also take advantage of OOP. Our current application is not using OOP so you can...
4
by: Inso Reiges | last post by:
Hi, I`m new to C++ and OOP in general and i come from long-time C mindset. So i have some style questions. I have a class that needs to be initialized by over nine parameters. So i decided to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.