n00b帮助 [英] n00b help

查看:88
本文介绍了n00b帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

borland c ++ 5.01

字符常量必须是一个或两个字符长


在编译我的第一个c ++程序时可以得到这个任何人都可以有帮助吗?


它突出了这一行作为问题


cout<< 请输入第一个数字:;


这是正确的不是吗?


这里是我用过的代码.. 。


#include< iostream.h>

void main(无效)

{

int iFirst,iSecond,iAnswer;

char cOperator,cCmd;

cout<< endl;

cout<< endl;

cout<< 按任意键启动程序或键入帮助以查看帮助:" ;;

cin>> CCMD; endl;

if(cCmd ==''help'')

cout<< 帮助文件 endl;

cout<< endl;

cout<< 使用这个计算器很简单,下面是

运算符的关键; endl;

cout<< " + = Add \\\
- = Subtract \\\
/ = Divide \\\
* = Multiply";

cout<< endl;

cout<< 请重新启动程序以继续; endl;

else

cout<< 请选择要使用的运营商+/-:" ;;

cin>> cOperator; endl;

cout<< 请输入第一个号码:;

cin>>我先来; endl;

cout<< 请输入第二个数字:;

cin>> iSecond;

if(cOperator ==''+'')

iAnswer = iFirst + iSecond;

否则如果

(cOperator ==''/'')

iAnswer = iFirst / iSecond;

否则如果

(cOperator ==' ' - '')

iAnswer = iFirst-iSecond;

否则如果

(cOperator ==''*'')

iAnswer = iFirst * iSecond;

else

cout<< endl;

cout<< ****请输入正确的操作员**** endl;

cout<< 你的总和的答案是:" ;;

cout<< iAnswer;

}

i知道它可能不是创建一个简单计算器最简单的方法但

i知道这种方式会起作用但我不知道

i尝试编译时失败的原因是什么?


任何帮助都会非常感激

-

谢谢,


克里斯。

解决方案



"克里斯托" < CH *** @ juststuff.co.uk>在消息中写道

新闻:2u ************* @ uni-berlin.de ...

borland c ++ 5.01
字符常量必须是一个或两个字符长

在编译我的第一个c ++程序时可以有人帮助吗?

它突出显示这一行问题

cout<< 请输入第一个数字:;

这是正确的不是吗?


是的,但还需要许多其他更正。

这里是我用过的代码...

#include< iostream.h>


#include< iostream>

使用命名空间std;


C ++没有调用头文件< iostream.h>,任何告诉你

不同的人都是错的。正确的头文件是< iostream>没有.h。

许多编译器支持< iostream.h>但你不应该使用它b / b
因为它不适合C ++。一些非常古老的编译器没有

< iostream>,它们只有< iostream.h>,如果是这样你那么

你应该得到更好的编译器。有很多免费的编译器

支持现代C ++。

void main(void)


int main()


main总是返回一个int,再次告诉你不同的人是错的。

{
int iFirst,iSecond,iAnswer;
char cOperator,cCmd;
cout<< endl;
cout<< endl;
cout<< 按任意键启动程序或键入帮助以查看帮助:
" ;; cin>> CCMD; ENDL;


cin>> cCmd;

if(cCmd ==''help'')


好​​了,这是你的大错。你显然认为cCmd可以是任何数字

的字符。但'char cCmd''表示/ single / character。如果你想要

多个字符,你应该使用字符串和双引号。例如


#include< iostream>

#include< string>

using namespace std;


int main()

{

cout<< 按任意键启动程序或输入帮助以查看帮助:

" ;;

string cmd;

getline(cin, cmd);

if(cmd ==" help")

看起来你忘了在这里放大括号


{

cout<< 帮助文件 endl;
cout<< endl;
cout<< 使用这个计算器很简单,下面是
操作员的关键; endl;
cout<< " + = Add \\\
- = Subtract \\\
/ = Divide \\\
* = Multiply";
cout<< endl;
cout<< 请重新启动程序以继续; ENDL;


这里


}

else




和这里


{


和其他各种地方。


我想你是从过时的源代码中学习C ++,你的代码是老式的b
。获得一本现代C ++教科书是个好主意。


john


Christo写道:

borland c ++ 5.01

字符常量必须是一个或两个字符长


Eh?

在编译我的时候得到这个第一个c ++程序可以有人在那里
帮忙吗?
它突出了这一行作为问题

cout<< 请输入第一个数字:;




(错误信息是......?)。事实上这条线本身并不是一个问题 - 很多其他的东西。


提示:


*查找if ... else ...语法,找出大括号。


*查看单引号和双引号之间的区别。


*查看endl的内容是/是,何时/如何使用它。


另外:


*获得一本好的C ++书 - 有很多*东西这个程序有问题

(我相信其他海报会指出你)和编码

风格正在酝酿。


*从一个更简单的程序开始 - Hallo world总是很好:)

问候,


-

Lionel B


Christo写道:

borland c ++ 5.01

字符常量必须是一个或两个字符长
在编译我的第一个c ++程序时得到这个可以帮助那些人吗?

它突出了这一行作为问题

cout<< 请输入第一个号码:;


你确定吗?见下文。

这是正确的不是吗?


是的。

这里是我用过的代码...

#include< iostream.h>


< iostream.h>是一个过时的非标准标题。使用< iostream>相反。

cout,cin和endl将在命名空间std中,所以你需要添加例如:


使用std :: cout;

使用std :: cin;

使用std :: endl;

void main(void)


main必须返回int。 C ++标准不允许任何其他内容。即使

您的编译器可能会接受它,在这种情况下也没有理由违反标准



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


请注意,每个cOperator和cCmd只能包含一个字符。

对于cCmd,这还不够,因为它不能保持字符串帮助。所以你要

应该添加#include< string>并使用std :: string;上面,然后将上面一行改为



char cOperator;

string cCmd;

cout<< endl;
cout<< endl;
cout<< 按任意键启动程序或键入帮助以查看帮助:
" ;; cin>> CCMD; ENDL;


最后的endl什么都不做。它评估函数指针和

对它没有任何作用。你可能意味着:


cout<<如果(cCmd ==''help'')


你确定这不是问题吗? ''help''是非法的,因为

字符常量可以容纳一个单个字符,但是你的字符长为4个字符。如果你让cCmd成为如上所述的字符串,你可以写一下:b
$ b if(cCmd ==" help")
cout<< 帮助文件 endl;
cout<< ENDL;


你错过了一个运营商<<在这两行中的第一行。还要注意

,endl会做两件事。它将\ n写入输出流并刷新

。你不需要冲洗,所以你可以简单地写:


cout<< 帮助文件\ n \ nn;


这样可以节省两次不必要的刷新,而且输入的次数更少。

cout<< 使用这个计算器很简单,下面是
操作员的关键; ENDL;


最后一次是无操作。

cout<< " + = Add \\\
- = Subtract \\\
/ = Divide \\\
* = Multiply";
cout<< endl;
cout<< 请重新启动程序以继续; ENDL;


这里。

其他
cout<< 请选择要使用的运营商+/-:" ;;
cin>> cOperator; ENDL;


又来了。

cout<< 请输入第一个数字:;
cin>>我先来; ENDL;


还有另一个。

顺便说一下,如果用户输入的东西不是什么?该怎么办?

cout< < 请输入第二个数字:;
cin>> iSecond;
if(cOperator ==''+'')
iAnswer = iFirst + iSecond;
如果
(cOperator ==''/'')
iAnswer = iFirst / iSecond;
如果
(cOperator =='' - '')
iAnswer = iFirst-iSecond;
如果
(cOperator = =''*'')
iAnswer = iFirst * iSecond;
其他
cout<< endl;
cout<< ****请输入正确的操作员**** endl;
cout<< 你的总和的答案是:" ;;
cout<< iAnswer;
}




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.

解决方案


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




这篇关于n00b帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆