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. 12 1980
"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
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
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; }
"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
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.
"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
"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
"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
"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
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.
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matt |
last post by:
I'd like to overwrite just one line of a binary file, based on a
position set by seek(). Is there no way to do this? As far as I can
tell I need...
|
by: Anupam Kapoor |
last post by:
hi all,
a python n00b, so please bear with me. i have a simple question:
i generally name python sources as a-simple-python-example.py. when i...
|
by: newgenre |
last post by:
I am using a pre-built package of code for my site, which is called
EasyDisc. All it does is it creates an interactive forum on your site, like
any...
|
by: ducky |
last post by:
Hi all,
The only programming experience i have under my belt so far is VB. I'm
just starting out on C++ and wonder if anybody suggests and good...
|
by: onefry |
last post by:
Hey I have this prog that i'm working on, starting my first c++ class and kind of a n00b to programming
here it is
#include <iostream>
...
|
by: Charles |
last post by:
I am learning from the Accelerated C++ book. The following example
doesn't work and I don't know why:
#include <iostream>
#include <string>
int...
|
by: HardHackz |
last post by:
Hey, I'm trying to learn C++, the problem is, when I do cout << "Hello
World!"; it always opens dos and closes it to quickly to see...i know
im a...
|
by: hockeyjk |
last post by:
All,
I'm writing a program that creates a histogram of data. IDLE is freezing up after the window opens (doesn't prompt user or graph anything). ...
|
by: benwah1983 |
last post by:
Greetings,
Here is my problem:
The following code shows a div with two small nested divs (images with a title), then the div is closed. Another...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |