469,647 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

n00b help

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 <iostream.h>
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.
Jul 22 '05 #1
12 1797

"Christo" <ch***@juststuff.co.uk> wrote in message
news:2u*************@uni-berlin.de...
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.

here is the code i have used...

#include <iostream.h>
#include <iostream>
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++.
void main (void)
int main()

main always returns an int, again anyone whoe tells you different is wrong.
{
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;
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

{
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

}
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
Jul 22 '05 #2
Christo wrote:
borland c++ 5.01

character constant must be one or two characters long
Eh?
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 :)
Regards,

--
Lionel B

Jul 22 '05 #3
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>
<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 File\n\n";

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;
}


Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cl*************@news.t-online.com...
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>


<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 File\n\n";

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
Jul 22 '05 #5


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.
Jul 22 '05 #6

"Christo" <ch***@juststuff.co.uk> wrote in message
news:2u*************@uni-berlin.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 File\n";

john
Jul 22 '05 #7

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

#include <iostream.h>


#include <iostream>
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++.
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
Jul 22 '05 #8

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"Christo" <ch***@juststuff.co.uk> wrote in message
news:2u*************@uni-berlin.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 File\n";

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
Jul 22 '05 #9

"Christo" <ch***@juststuff.co.uk> wrote in message
news:2u*************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"Christo" <ch***@juststuff.co.uk> wrote in message
news:2u*************@uni-berlin.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 File\n";

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
Jul 22 '05 #10
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.

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.
Jul 22 '05 #11

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"Christo" <ch***@juststuff.co.uk> wrote in message
news:2u*************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...
>
> "Christo" <ch***@juststuff.co.uk> wrote in message
> news:2u*************@uni-berlin.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 File\n";
>
> 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
Jul 22 '05 #12

"Ron Natalie" <ro*@sensor.com> wrote in message
news:41***********************@news.newshosting.co m...
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.)

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


Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anupam Kapoor | last post: by
1 post views Thread by newgenre | last post: by
2 posts views Thread by ducky | last post: by
6 posts views Thread by Charles | last post: by
8 posts views Thread by HardHackz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.