C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

n00b help

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Christo
Guest





PostPosted: Wed Oct 27, 2004 8:54 am    Post subject: n00b help Reply with quote



borland c++ 5.01

character constant must be one or two characters long

get this when compiling my first c++ program can anyone out there help?

it is highlighting this line as the problem

cout << "Please Enter First number: ";

that is correct isnt it though?

here is the code i have used...

#include void main (void)
{
int iFirst, iSecond, iAnswer;
char cOperator, cCmd;
cout << endl;
cout << endl;
cout << "Press any key to start the program or type help to view help: ";
cin >> cCmd; endl;
if (cCmd == 'help')
cout << "Help File" endl;
cout << endl;
cout << "Using this calculator is simple below is the key for the
operators"; endl;
cout << "+ = Add n - = Subtract n / = Divide n * = Multiply";
cout << endl;
cout << "Please restart to program to continue"; endl;
else
cout << "Please select which operator would would like to use +/-: ";
cin >> cOperator; endl;
cout << "Please Enter First number: ";
cin >> iFirst; endl;
cout << "Please Enter Second Number: ";
cin >> iSecond;
if (cOperator == '+')
iAnswer = iFirst+iSecond;
else if
(cOperator == '/')
iAnswer = iFirst/iSecond;
else if
(cOperator == '-')
iAnswer = iFirst-iSecond;
else if
(cOperator == '*')
iAnswer = iFirst*iSecond;
else
cout << endl;
cout << "****Please Input A Correct Operator****" endl;
cout << "The Answer To your Sum is: ";
cout << iAnswer;
}


i know it is probably not the easiest way to create a simple calculator but
i know this way will work but i dont know what is casusing the failure when
i try to compile

any help would be much appreciated
--
Thanks,

Chris.


Back to top
John Harrison
Guest





PostPosted: Wed Oct 27, 2004 9:23 am    Post subject: Re: n00b help Reply with quote




"Christo" <chris (AT) juststuff (DOT) co.uk> wrote

Quote:
borland c++ 5.01

character constant must be one or two characters long

get this when compiling my first c++ program can anyone out there help?

it is highlighting this line as the problem

cout << "Please Enter First number: ";

that is correct isnt it though?

Yes, but many other corrections needed.

Quote:

here is the code i have used...

#include

#include using namespace std;

C++ does not have a header file called <iostream.h>, any one who tells you
different is wrong. The cirrect header file is <iostream> without the .h.
Many compilers however support <iostream.h> but you should not use it
because it isn't proper C++. A few very old compilers do not have
<iostream>, they only have <iostream.h>, if that is the case for you then
you should get a better compiler. There are plently of free compilers
available which support modern C++.

Quote:
void main (void)

int main()

main always returns an int, again anyone whoe tells you different is wrong.

Quote:
{
int iFirst, iSecond, iAnswer;
char cOperator, cCmd;
cout << endl;
cout << endl;
cout << "Press any key to start the program or type help to view help:
";
cin >> cCmd; endl;

cin >> cCmd;

Quote:
if (cCmd == 'help')

OK here's your big mistake. You obviously think that cCmd can be any number
of characters. But 'char cCmd' means a /single/ character. If you want
multiple characters you should use strings and double quotes. For instance

#include <iostream>
#include <string>
using namespace std;

int main()
{
cout << "Press any key to start the program or type help to view help:
";
string cmd;
getline(cin, cmd);
if (cmd == "help")

Looks like you've forgotten to put curly brackets here

{

Quote:
cout << "Help File" endl;
cout << endl;
cout << "Using this calculator is simple below is the key for the
operators"; endl;
cout << "+ = Add n - = Subtract n / = Divide n * = Multiply";
cout << endl;
cout << "Please restart to program to continue"; endl;

And here

}

Quote:
else

And here

{

and various other places.

I think you are learning C++ from an out of date source, your code is
old-fashioned. It would be a good idea to get a modern C++ text book.

john



Back to top
Lionel B
Guest





PostPosted: Wed Oct 27, 2004 9:33 am    Post subject: Re: n00b help Reply with quote



Christo wrote:
Quote:
borland c++ 5.01

character constant must be one or two characters long

Eh?

Quote:
get this when compiling my first c++ program can anyone out there
help?

it is highlighting this line as the problem

cout << "Please Enter First number: ";

(and the error message is...?). In fact this line is not in itself a
problem - many other things are.

Hints:

* Look up the if .. else ... syntax and find out about curly brackets.

* Look up the difference between single and double quotes.

* Look up what "endl" is/does and when/how to use it.

Also:

* Get a good C++ book - there are *many* things wrong with this program
(which I'm sure other posters will point out to you) and the coding
style is apalling.

* Start with a simpler program - "Hallo world" is always good Smile
Regards,

--
Lionel B


Back to top
Rolf Magnus
Guest





PostPosted: Wed Oct 27, 2004 9:39 am    Post subject: Re: n00b help Reply with quote

Christo wrote:

Quote:
borland c++ 5.01

character constant must be one or two characters long

get this when compiling my first c++ program can anyone out there help?

it is highlighting this line as the problem

cout << "Please Enter First number: ";

Are you sure about that? See below.

Quote:

that is correct isnt it though?

Yes.

Quote:
here is the code i have used...

#include

cout, cin and endl will then be in namespace std, so you need to add e.g.:

using std::cout;
using std::cin;
using std::endl;

Quote:
void main (void)

main must return int. Nothing else is allowed by the C++ standard. Even if
your compiler might accept it, there is no reason to violate the standard
in this case.

Quote:
{
int iFirst, iSecond, iAnswer;
char cOperator, cCmd;

Note that each of cOperator and cCmd can hold exactly one single character.
For cCmd, that is not enough, since it can't hold the string "help". So you
should add #include <string> and using std::string; above, and then change
the above line into:

char cOperator;
string cCmd;

Quote:
cout << endl;
cout << endl;
cout << "Press any key to start the program or type help to view help:
"; cin >> cCmd; endl;

The endl at the end doesn't do anything. It evaluates a function pointer and
does nothing with it. You probably meant:

cout << endl;

Quote:
if (cCmd == 'help')

Are you sure that this wasn't the line in question? 'help' is illegal, since
character constants can hold one single character, but yours is 4
characters long. If you make cCmd a string as mentioned above, you can
write:

if (cCmd == "help")

Quote:
cout << "Help File" endl;
cout << endl;

You're missing an operator << in the first of those two lines. Note also
that endl does two things. It writes a n to the output stream and flushes
it. You don't need the flush here, so you could simply write:

cout << "Help Filenn";

This saves you two unnecessary flushes and is less to type.

Quote:
cout << "Using this calculator is simple below is the key for the
operators"; endl;

Again a no-op at the end.

Quote:
cout << "+ = Add n - = Subtract n / = Divide n * = Multiply";
cout << endl;
cout << "Please restart to program to continue"; endl;

And here.

Quote:
else
cout << "Please select which operator would would like to use +/-: ";
cin >> cOperator; endl;

And here again.

Quote:
cout << "Please Enter First number: ";
cin >> iFirst; endl;

And another one.
Btw, what if the user enters something that isn't a nuber?

Quote:
cout << "Please Enter Second Number: ";
cin >> iSecond;
if (cOperator == '+')
iAnswer = iFirst+iSecond;
else if
(cOperator == '/')
iAnswer = iFirst/iSecond;
else if
(cOperator == '-')
iAnswer = iFirst-iSecond;
else if
(cOperator == '*')
iAnswer = iFirst*iSecond;
else
cout << endl;
cout << "****Please Input A Correct Operator****" endl;
cout << "The Answer To your Sum is: ";
cout << iAnswer;
}


Back to top
Christo
Guest





PostPosted: Wed Oct 27, 2004 12:19 pm    Post subject: Re: n00b help Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote

Quote:
Christo wrote:

borland c++ 5.01

character constant must be one or two characters long

get this when compiling my first c++ program can anyone out there help?

it is highlighting this line as the problem

cout << "Please Enter First number: ";

Are you sure about that? See below.


that is correct isnt it though?

Yes.

here is the code i have used...

#include
iostream.h> is an outdated non-standard header. Use <iostream> instead.
cout, cin and endl will then be in namespace std, so you need to add e.g.:

using std::cout;
using std::cin;
using std::endl;

void main (void)

main must return int. Nothing else is allowed by the C++ standard. Even if
your compiler might accept it, there is no reason to violate the standard
in this case.

{
int iFirst, iSecond, iAnswer;
char cOperator, cCmd;

Note that each of cOperator and cCmd can hold exactly one single
character.
For cCmd, that is not enough, since it can't hold the string "help". So
you
should add #include <string> and using std::string; above, and then change
the above line into:

char cOperator;
string cCmd;

cout << endl;
cout << endl;
cout << "Press any key to start the program or type help to view help:
"; cin >> cCmd; endl;

The endl at the end doesn't do anything. It evaluates a function pointer
and
does nothing with it. You probably meant:

cout << endl;

if (cCmd == 'help')

Are you sure that this wasn't the line in question? 'help' is illegal,
since
character constants can hold one single character, but yours is 4
characters long. If you make cCmd a string as mentioned above, you can
write:

if (cCmd == "help")

cout << "Help File" endl;
cout << endl;

You're missing an operator << in the first of those two lines. Note also
that endl does two things. It writes a n to the output stream and flushes
it. You don't need the flush here, so you could simply write:

cout << "Help Filenn";

This saves you two unnecessary flushes and is less to type.

cout << "Using this calculator is simple below is the key for
the
operators"; endl;

Again a no-op at the end.

cout << "+ = Add n - = Subtract n / = Divide n * = Multiply";
cout << endl;
cout << "Please restart to program to continue"; endl;

And here.

else
cout << "Please select which operator would would like to use +/-:
";
cin >> cOperator; endl;

And here again.

cout << "Please Enter First number: ";
cin >> iFirst; endl;

And another one.
Btw, what if the user enters something that isn't a nuber?

cout << "Please Enter Second Number: ";
cin >> iSecond;
if (cOperator == '+')
iAnswer = iFirst+iSecond;
else if
(cOperator == '/')
iAnswer = iFirst/iSecond;
else if
(cOperator == '-')
iAnswer = iFirst-iSecond;
else if
(cOperator == '*')
iAnswer = iFirst*iSecond;
else
cout << endl;
cout << "****Please Input A Correct Operator****" endl;
cout << "The Answer To your Sum is: ";
cout << iAnswer;
}


Thank you, I gotta start learning from my mistakes, It's my university they
insist on using borland c++ 5.01 which i think was released in mid 90's

thanks all for the hwlp I am gonna go off and try to get it working



Back to top
Christo
Guest





PostPosted: Wed Oct 27, 2004 12:35 pm    Post subject: Re: n00b help Reply with quote



we have not covered strings in the intro to c++ course yet at uni, just int,
float and char, so i havent used them, i dont want to get confused early on,
even though i have programmed before with vb and used php i think c++ is
going to be a step up for me.

I got the program working with one minor problem... whenever i get my answer
it prints this line regardless if i input the correct operator (+ - * /)

can anyone give me any advice... it shouldnt print this since it was a
correct operator should it.

here is the code now........

#include <iostream>

int main()
{
int iFirst, iSecond, iAnswer;
char cOperator, cCmd;
cout << endl;
cout << endl;
cout << "Press any key to start the program or type *h* to view help: ";
cin >> cCmd; endl;
if (cCmd == 'h')
{
cout << "Help File" << endl;
cout << endl;
cout << "Using this calculator is simple below is the key for the
operators";
cout << "n + = Add n - = Subtract n / = Divide n * = Multiply";
cout << endl;
cout << "Please restart to program to continue"; endl;
}
else
{
cout << "Please select which operator would would like to use +/-: ";
cin >> cOperator; endl;
cout << "Please Enter First number: ";
cin >> iFirst; endl;
cout << "Please Enter Second Number: ";
cin >> iSecond;
if (cOperator == '+')
{
iAnswer = iFirst+iSecond;
}
else if (cOperator == '/')
{
iAnswer = iFirst/iSecond;
}
else if (cOperator == '-')
{
iAnswer = iFirst-iSecond;
}
else if (cOperator == '*')
{
iAnswer = iFirst*iSecond;
}
else
{
cout << endl;
cout << "****Please Input A Correct Operator****" << endl; // This
is the line that gets printed even if all above // conditions are
true
}
}
cout << endl;
cout << "The Answer To your Sum is: ";
cout << iAnswer;
}

at least it is outputting the answer.. just that nasty warning asking for
correct operator, I was told this was quite ambitious for my first program
by my uni lecturer and was told to stick to maybe an adding calculator
alone, but i thought i would have a go, i can see it is very simple and
really isnt that hard to impliment. I am learning and apprecate any help i
receive from people on usenet.


Back to top
John Harrison
Guest





PostPosted: Wed Oct 27, 2004 12:49 pm    Post subject: Re: n00b help Reply with quote


"Christo" <chris (AT) juststuff (DOT) co.uk> wrote

Quote:


we have not covered strings in the intro to c++ course yet at uni, just
int,
float and char, so i havent used them, i dont want to get confused early
on,
even though i have programmed before with vb and used php i think c++ is
going to be a step up for me.

I got the program working with one minor problem... whenever i get my
answer
it prints this line regardless if i input the correct operator (+ - * /)


It doesn't when I run it. And nor can I see any reason that it would. I
think you must be mistaken, probably the code you have posted and the code
you are running are not the same.

And please drop all those silly endl

cin >> cCmd; endl;

This endl is doing NOTHING.

And replace all the other endl with n

cout << "Help File" << endl;

should be

cout << "Help Filen";

john



Back to top
Howard
Guest





PostPosted: Wed Oct 27, 2004 3:15 pm    Post subject: Re: n00b help Reply with quote


"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:


#include <iostream.h

#include using namespace std;

C++ does not have a header file called different is wrong. The cirrect header file is <iostream> without the .h.
Many compilers however support <iostream.h> but you should not use it
because it isn't proper C++. A few very old compilers do not have
iostream>, they only have <iostream.h>, if that is the case for you then
you should get a better compiler. There are plently of free compilers
available which support modern C++.

void main (void)

int main()

main always returns an int, again anyone whoe tells you different is
wrong.

rant


Ever used CodeWarrior? In order to maintain compatibility with OS9, they
sttill give you a void main() function when creating a project. It's not a
matter of someone "telling you" it's right, it's just what you get when
starting a project. Granted, it's non-standard, and ought to be changed
(both by the user and by Metrowerks), but in the context in which it was
used, it's correct, in that it compiles and executes without error.

Also, in some compilers (such as CodeWarrior), iostream.h is simply a
wrapper that does just what you've described (sort of): it includes
<iostream>, then some other headers it needs, and then performs using
statements as needed (depending upon certain compiler flags). It's
perfectly valid to use, and works fine. Sure, it's less portable, but
that's not actually always a concern of the programmer, is it?

I know you're describing good practices, but it's not always a matter of
someone "telling you" the wrong thing, but rather what the compiler provides
by default. Sure I could get a new compiler, one that didn't encourage bad
behavior, but I have a lot of thrid-party SDKs that absolutely depend upon
my using CodeWarrior (on the Mac) or VC++ (on the PC), so that's really not
an option, is it?

Basically, I guess I'm just suggesting that folks ease up on the way such
things are presented to the posters (esp. beginners). Letting them know the
right thing to do is fine, but that should be more of an aside, not as if
it's the main problem with their code. I know, I know: "we've gotta teach
'em right, or they'll never learn!" I still think we oughta lighten up.
Something more like "By the way, according to the C++ standard, main should
return int." should be sufficient.

"That's my opinion. I could be wrong."

</rant>

-Howard

"I'm never wrong. I thought I was wrong once, but I was mistaken." -Anon



Back to top
Christo
Guest





PostPosted: Wed Oct 27, 2004 3:25 pm    Post subject: Re: n00b help Reply with quote


"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:

"Christo" <chris (AT) juststuff (DOT) co.uk> wrote in message
news:2u9iunF27jf24U1 (AT) uni-berlin (DOT) de...


we have not covered strings in the intro to c++ course yet at uni, just
int,
float and char, so i havent used them, i dont want to get confused early
on,
even though i have programmed before with vb and used php i think c++ is
going to be a step up for me.

I got the program working with one minor problem... whenever i get my
answer
it prints this line regardless if i input the correct operator (+ - * /)


It doesn't when I run it. And nor can I see any reason that it would. I
think you must be mistaken, probably the code you have posted and the code
you are running are not the same.

And please drop all those silly endl

cin >> cCmd; endl;

This endl is doing NOTHING.

And replace all the other endl with n

cout << "Help File" << endl;

should be

cout << "Help Filen";

john



ok thank you, i have just been taught that i thought endl; took you onto a
new line, i am not using a GUI for this just the console, at the minute i am
a begginner



Back to top
John Harrison
Guest





PostPosted: Wed Oct 27, 2004 3:48 pm    Post subject: Re: n00b help Reply with quote


"Christo" <chris (AT) juststuff (DOT) co.uk> wrote

Quote:

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:2u9jn3F27r3mtU1 (AT) uni-berlin (DOT) de...

"Christo" <chris (AT) juststuff (DOT) co.uk> wrote in message
news:2u9iunF27jf24U1 (AT) uni-berlin (DOT) de...


we have not covered strings in the intro to c++ course yet at uni, just
int,
float and char, so i havent used them, i dont want to get confused
early
on,
even though i have programmed before with vb and used php i think c++
is
going to be a step up for me.

I got the program working with one minor problem... whenever i get my
answer
it prints this line regardless if i input the correct operator (+ - *
/)


It doesn't when I run it. And nor can I see any reason that it would. I
think you must be mistaken, probably the code you have posted and the
code
you are running are not the same.

And please drop all those silly endl

cin >> cCmd; endl;

This endl is doing NOTHING.

And replace all the other endl with n

cout << "Help File" << endl;

should be

cout << "Help Filen";

john



ok thank you, i have just been taught that i thought endl; took you onto a
new line, i am not using a GUI for this just the console, at the minute i
am
a begginner


endl does take you to a new line on output (it does nothing on input) but so
does 'n'. The difference is that n is easier to type and that endl as well
as taking you to a newline also flushes the output stream. Almost all the
time you don't care about flushing the output stream, maybe you don't even
know what it means.

cin >> cCmd; endl;

is just plain wrong, endl has no meaning on input, and if this exercise is
being marked you'll lose marks for that.

cout << "Help File" << endl;

is OK, but it flushes the output stream for no obviously good reason. To me
it just seems like one of those things that gets copied (or taught) for no
discernable reason but everyone ends up doing it anyway.

But anyway, the main thing is that you got your program working. Did you
sort out why that extra line was always appearing?

john



Back to top
Ron Natalie
Guest





PostPosted: Wed Oct 27, 2004 5:22 pm    Post subject: Re: n00b help Reply with quote

Howard wrote:

Quote:
rant

Ever used CodeWarrior? In order to maintain compatibility with OS9, they
sttill give you a void main() function when creating a project. It's not a
matter of someone "telling you" it's right, it's just what you get when
starting a project. Granted, it's non-standard, and ought to be changed
(both by the user and by Metrowerks), but in the context in which it was
used, it's correct, in that it compiles and executes without error

<RANT> Who the hell cares. If you have to bend over to support a non-standard
compiler, that's fine. Howevever, we recommend the standard language
here. Frankly, it's got nothing to do with OS9 but everything to do
with CodeWarriors poor implementation. Whether nor not the OS has a
return value, main still needs to be an int-returning function.

Simmilarly for your other issues. The idea is people come here for
instruction. Telling them to do non-standard things just because some
compilers support it isn't productive.

Back to top
Christo
Guest





PostPosted: Wed Oct 27, 2004 5:43 pm    Post subject: Re: n00b help Reply with quote


"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:

"Christo" <chris (AT) juststuff (DOT) co.uk> wrote in message
news:2u9subF28fkn9U1 (AT) uni-berlin (DOT) de...

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:2u9jn3F27r3mtU1 (AT) uni-berlin (DOT) de...

"Christo" <chris (AT) juststuff (DOT) co.uk> wrote in message
news:2u9iunF27jf24U1 (AT) uni-berlin (DOT) de...


we have not covered strings in the intro to c++ course yet at uni,
just
int,
float and char, so i havent used them, i dont want to get confused
early
on,
even though i have programmed before with vb and used php i think c++
is
going to be a step up for me.

I got the program working with one minor problem... whenever i get my
answer
it prints this line regardless if i input the correct operator (+ - *
/)


It doesn't when I run it. And nor can I see any reason that it would. I
think you must be mistaken, probably the code you have posted and the
code
you are running are not the same.

And please drop all those silly endl

cin >> cCmd; endl;

This endl is doing NOTHING.

And replace all the other endl with n

cout << "Help File" << endl;

should be

cout << "Help Filen";

john



ok thank you, i have just been taught that i thought endl; took you onto
a
new line, i am not using a GUI for this just the console, at the minute i
am
a begginner


endl does take you to a new line on output (it does nothing on input) but
so
does 'n'. The difference is that n is easier to type and that endl as
well
as taking you to a newline also flushes the output stream. Almost all the
time you don't care about flushing the output stream, maybe you don't even
know what it means.

cin >> cCmd; endl;

is just plain wrong, endl has no meaning on input, and if this exercise is
being marked you'll lose marks for that.

cout << "Help File" << endl;

is OK, but it flushes the output stream for no obviously good reason. To
me
it just seems like one of those things that gets copied (or taught) for no
discernable reason but everyone ends up doing it anyway.

But anyway, the main thing is that you got your program working. Did you
sort out why that extra line was always appearing?

john


oh damn i am stupid i didnt realize i had it on the cin

haha

not yet no, i am thining it might just be my machine, stuck it on my account
at uni so i check it out on friday and ask me lecturer about it.

Thanks for the info



Back to top
Howard
Guest





PostPosted: Wed Oct 27, 2004 6:02 pm    Post subject: Re: n00b help Reply with quote


"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:
Howard wrote:

rant

Ever used CodeWarrior? In order to maintain compatibility with OS9, they
sttill give you a void main() function when creating a project. It's not
a matter of someone "telling you" it's right, it's just what you get when
starting a project. Granted, it's non-standard, and ought to be changed
(both by the user and by Metrowerks), but in the context in which it was
used, it's correct, in that it compiles and executes without error

RANT> Who the hell cares. If you have to bend over to support a
non-standard
compiler, that's fine. Howevever, we recommend the standard language
here. Frankly, it's got nothing to do with OS9 but everything to do
with CodeWarriors poor implementation. Whether nor not the OS has a
return value, main still needs to be an int-returning function.

(Just going by CWRon's response when I asked why their main returns void.)

Quote:

Simmilarly for your other issues. The idea is people come here for
instruction. Telling them to do non-standard things just because some
compilers support it isn't productive.

I'm not advocating telling them to do the wrong thing, just not to
concentrate *so much* on those two items when responding to a post that's
about something else. And to respond in a kindler, gentler manner, not jump
on them for making a mistake that their IDE may actually have made for them.
That's all.

-Howard





Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.