473,856 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2145

"Christo" <ch***@juststuf f.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***@juststuf f.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***@juststuf f.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***@juststuf f.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***@juststuf f.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

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

Similar topics

1
1789
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 to read the whole file, change the line, and write it all back out. Not exactly easy on the memory, but I see no other solution. so far: patchme.seek(offset) patchme.write(a2b_hex(edit)) # the data is in hex first patchme.close
3
1535
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 try to import a module named as above, i (obviously) get tracebacks from python interpreter. is there a way to continue naming python sources as above, and still use it as python modules ? i can ofcourse change the name to
1
1546
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 forum you see anywhere. I am having a problem getting started as I am new to .NET and sql. The app, once unzipped, tells me to "Make note of your SQL server name, username, password, database name. You need to supply these info. later." My problem is I don't know where to find out that info....
2
1479
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 (free) starting points for me to get going. I'm wondering about tutorials, source code, etc... Stuff that will take me from absolute square 1. Also, which compilers would be suggested? i have the bloodshed one and microsoft's visual studio express version as well. Are there any others that...
4
10149
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> #include <cstdlib> using namespace std;
6
1512
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 main () { const std::string exclam = "!"; const std::string message = "Hello" + ", world" + exclam; return 0; }
8
1428
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 total n00b, but any help?
0
1141
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). The window that opens is named "tk" rather than the name I assign to it. Can anyone shed light on why. EDIT: Nevermind. The error was resulting because I was attempting to run the file from my flash drive. Who knew. n00b. Can an admin delete this thread?
2
4236
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 one opens and a "random text" is displayed. <div style="width: 500px;"> <div style="float: left; padding: 20px;"> Image Title 1<br/> <img src="test.jpg"/> </div> <div style="float: left; padding: 20px;">
0
9758
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9528
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7929
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5757
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.