简单的C ++问题 - C ++ 101 [英] Simple C++ problem - C++ 101
问题描述
大家好,
我在所罗门群岛辅导计算机科学入门和
我们使用C ++作为首选语言(很好)。请注意,我不是一个C ++程序员,但是我已经在大多数
主流语言编程了20年。我们正在学习如何使用
代码,而不仅仅是C ++。课堂上的家伙们在几天内完成了他们的最终任务
,我们有一个简单的问题需要克服。
问题是这样的:
程序读入一个整数数组,指示一个方向进入的机器人
方向,在20 x 20网格中。前两个整数(从文件中读取
)是机器人的起始位置,并且必须在函数中验证为
作为20中的有效位置x 20 grid。
起始位置是文件中的全局变量。布尔值
''有效''作为全局声明为false。要验证的代码是
,如下所示:
void check_valid()
{
if ((start_x> = 0 || start_x< = 20)&&(start_y> = 0 || start_y< =
20))//你知道我的意思吗? br />
{
有效=真;
}
}
问题是,如果初始start_x和start_y位置有效,那么''if''逻辑永远不会有效。任何回复都有一个很大的谢谢。
这些家伙试图在他们的课程中取得进展
Solomons的极大不确定性(只需进行谷歌搜索和找出
关于我们过去几周遇到的麻烦。)
尼克
ni * ***************@hotmail.com 写道:大家好,
我在辅导计算机科学入门在所罗门群岛和我们使用C ++作为首选语言(很好)。请注意,我不是一个C ++程序员,但是我已经用大多数主流语言编写了20年以上的程序。我们正在学习如何编写代码,而不仅仅是C ++。班上的家伙们在几天内完成了他们的最终任务,我们有一个简单的问题需要克服。这个问题是这样的:
程序读入一个整数数组,用20×20网格指示一个进入方向的机器人。前两个整数(从文件读入
)是机器人的起始位置,必须在函数中验证为20 x 20网格内的有效位置。
起始位置是文件中的全局变量。布尔值
''valid''也被声明为false为全局。要验证的代码如下:
void check_valid()
{
if((start_x> = 0 || start_x< = 20)& ;&(start_y> = 0 || start_y< =
20))//你知道我的意思
{
有效=真;
} >}
问题是,如果初始start_x和start_y位置有效,则if逻辑永远不会起作用。任何回复都会有一个很大的谢谢。这些人试图在Solomons中有很大的不确定性(只做Google搜索并找出关于过去几周我们经历过的麻烦。
尼克
i像这样运行你的代码
//main.cpp
#include< iostream>
使用命名空间std;
bool valid = false;
int start_x = 2;
int start_y = 3;
int main()
{
if((start_x> = 0 || start_x< = 20)&&(start_y> = 0 || start_y< = < br $>
20))
{
有效=真;
cout<<" hello"<< ; endl;
}
返回0;
}
//输出>
你好
出了什么问题!
ni **************** @ hotmail.com 写道:
void check_valid()
{
if((start_x> = 0 || start_x< = 20)&& (start_y> = 0 || start_y< =
20))//你知道我的意思
{
有效=真;
}
} <问题是,如果初始start_x和start_y位置有效,则if逻辑永远不会起作用。任何回复都会有一个很大的谢谢。这些人试图在Solomons中有很大的不确定性(只做Google搜索并找出关于我们在过去几周里经历过的麻烦。)
相反,无论start_x和
$的值如何,有效都设置为true b $ b start_y因为任何数字都是大于0 *或*小于20,你
可能意味着:
if((start_x> = 0& ;& start_x< = 20)&&(start_y> = 0&& start_y< =
20))
valid = true;
如果start_x和start_y都在
范围内,这将设置为true [0,20]
Abdo Haji-Ali
程序员
In | Framez
Abdo Haji-Ali写道:ni ** ************** @ hotmail.com 写道:
void check_valid()
if(
start_x> = 0 || start_x< = 20)&& (start_y> = 0 || start_y< =
20))//你知道我的意思
{
有效=真;
}
} <问题是,如果初始start_x和start_y位置有效,则if逻辑永远不会起作用。任何回复都会有一个很大的谢谢。这些人试图在Solomons中有很大的不确定性(只做Google搜索并找出关于过去几周我们经历过的麻烦。)
相反,无论start_x和
start_y的值是多少都设置为true,因为任何数字都是大于0 *或*小于20,你可能意味着:
if((start_x> = 0&& start_x< = 20)&&(start_y> ; = 0&& start_y< =
20))
有效=真;
如果start_x和start_y都在
range [0,20]
如果网格的大小为
,忘记提一下你可能需要[0,19]的范围20 ...
Abdo Haji-Ali
程序员
In | Framez
Hi all,
I am tutoring introductory computer science in the Solomon Islands and
we are using C++ as the language of choice (nice). Please note that I
am no way a C++ programmer, but have but I have programmed in most
mainstream languages over the paste 20 years. We are learning HOW to
code, not just C++. The dudes in the class have their final assignment
in a couple of days and we have a simple problem to overcome. The
problem is this:
The program reads in an array of integers which instructs a robot which
direction to go in, in a 20 x 20 grid. The first two integers (read in
from a file) are the starting position of the robot, and have to be
validated in a function as a valid position within the 20 x 20 grid.
The starting postions are global variables in the file. The boolean
''valid'' is declared as false also as a global. The code to validate is
as follows:
void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}
The problem is that if the initial start_x and start_y positions ARE
valid, the ''if'' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we''ve been through in the last few weeks).
Nick
ni****************@hotmail.com wrote:Hi all,
I am tutoring introductory computer science in the Solomon Islands and
we are using C++ as the language of choice (nice). Please note that I
am no way a C++ programmer, but have but I have programmed in most
mainstream languages over the paste 20 years. We are learning HOW to
code, not just C++. The dudes in the class have their final assignment
in a couple of days and we have a simple problem to overcome. The
problem is this:
The program reads in an array of integers which instructs a robot which
direction to go in, in a 20 x 20 grid. The first two integers (read in
from a file) are the starting position of the robot, and have to be
validated in a function as a valid position within the 20 x 20 grid.
The starting postions are global variables in the file. The boolean
''valid'' is declared as false also as a global. The code to validate is
as follows:
void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}
The problem is that if the initial start_x and start_y positions ARE
valid, the ''if'' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we''ve been through in the last few weeks).
Nick
i run your code like this
//main.cpp
#include <iostream>
using namespace std;
bool valid = false;
int start_x = 2;
int start_y = 3;
int main()
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20))
{
valid = true;
cout<<"hello"<<endl;
}
return 0;
}
//output
hello
what`s wrong!
ni****************@hotmail.com wrote:
void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}
The problem is that if the initial start_x and start_y positions ARE
valid, the ''if'' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we''ve been through in the last few weeks).
On the contrary, valid is set to true whatever the value of start_x and
start_y because any number is "bigger than 0 *or* less than 20", you
probably meant:
if ((start_x >= 0 && start_x <= 20) && (start_y >= 0 && start_y <=
20))
valid = true;
This would set valid to true if both start_x and start_y are in the
range [0,20]
Abdo Haji-Ali
Programmer
In|Framez
Abdo Haji-Ali wrote:ni****************@hotmail.com wrote:
void check_valid( )
{
if ((start_x >= 0 || start_x <= 20) && (start_y >= 0 || start_y <=
20)) // you know what I mean
{
valid = true;
}
}
The problem is that if the initial start_x and start_y positions ARE
valid, the ''if'' logic never work. Any replies come with a big Thanks In
Advance for these guys trying to get through their course in a time of
great uncertainty in the Solomons (just do a Google search and find out
about the troubles we''ve been through in the last few weeks).
On the contrary, valid is set to true whatever the value of start_x and
start_y because any number is "bigger than 0 *or* less than 20", you
probably meant:
if ((start_x >= 0 && start_x <= 20) && (start_y >= 0 && start_y <=
20))
valid = true;
This would set valid to true if both start_x and start_y are in the
range [0,20]
Forgot to mention that you probably need a range of [0,19] if the size
of the grid is 20...
Abdo Haji-Ali
Programmer
In|Framez
这篇关于简单的C ++问题 - C ++ 101的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!