ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《卷轴教程之C++入门》#4 C++程序的输入与输出 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#4 C++程序的输入与输出

C++ Program Input and Output

现在我们就要编写第二个程序了
OK, I think we’re ready to write our second program.
这次的程序里 我们要提示用户
In this program, we want to simply prompt the user
输入自己的姓名
to enter in their name,
以及年龄
and then prompt them to enter in their age,
然后把输入的姓名和年龄显示出来
and then display their name and their age.
我们会用到上次讲变量时
So we’ll make use of some of the concepts
涉及的一些概念
that we recently covered dealing with variables,
我们也会学习
and we’ll also look at the concept of
如何从用户那里获取输入
getting input from the user.
我们还没学过输入
So we haven’t done that before…
只学过向屏幕输出
We’ve just done output to the screen.
好 现在就开始创建项目
OK, so let’s go ahead and create our project.
点击菜单 “File”
So we’ll go to here to ‘File’,
鼠标移到 “New”
‘New’,
选择 “C++ Project”
‘C++ Project’,
我们就把这个项目叫做 “Program2”
and we’re gonna call this C++ project ‘Program2’.
“Project type” 选择 “Empty Project”
For ‘Project type’, leave it as ‘Empty Project’.
如果你同时有几个工具链的话
‘Toolchains’, make sure ‘MinGW GCC’ is selected
请在 “Toolchains” 选择 “MinGW GCC”
if you have more than one toolchain,
然后点击 “Finish”
and click on ‘Finish’.
现在 “Project Explorer” 里可以看到我们的项目了
And now that we have our project here on the ‘Project Explorer’,
右键单击它
go ahead and right click,
选择 “New” 然后选择 “Source File”
select ‘New’, and then we’ll select ‘Source File’,
我们给源文件起个和项目一样的名字
and we’ll name the source file the same thing as the program,
不过末尾要加上扩展名”cpp”
except that we need to have the ‘cpp’ extension.
这是 C++ 源文件的惯用扩展名
OK, so that’s just convention for C++ source files.
然后点击 “Finish”
And click on ‘Finish’.
好了 进入编辑器界面 可以开始写代码了
So here we go, we’re in our editor now, we can start typing.
第一行代码
The first line is gonna be the same first line
和上次的一样
we saw on our other program.
输入 “#include”
We’ll have a pound, ‘include’,
左尖括号 “iostream”
open angle bracket, ‘iostream’.
“iostream” 的作用是引入
So this ‘iostream’ is bringing in the facilities
从键盘获取输入
to be able to do input, getting input from the keyboard,
以及输出到屏幕的功能
and also doing output to the screen.
此外 我们还要引入
The other thing that we’re gonna have here
对字符串数据类型的支持
is support for the string data type,
为此我们需要加入另一个库
so we actually need to bring in another library for that.
所以输入 “#include”
So we need to do pound, ‘include’,
左尖括号 再输入 “string”
open angle bracket, and then just ‘string’ here.
按几次回车键
OK, come down a couple of lines,
输入 “using”
and then we’ll have the ‘using’,
“namespace” 空格 “std”
‘namespace’, space, standard.
同样 目前我也不会对这行代码解释太多
Again, I’m not gonna go into the details of this just yet,
你只要知道它可以帮我们节省一些代码就行了
other than to say it’s gonna save us a few keystrokes.
现在输入 main 函数
So now we can type in our main function.
输入 “int main”
So ‘int’, ‘main’,
左括号 右括号
open paren, close paren,
按回车键
come down a line,
左花括号 回车
open brace, enter, and it should automatically
右花括号会自动加入
insert our close brace,
一切进展顺利
so everything is going well so far.
这就是我们程序的基本架构
So this is the basic structure of our program.
和上次的看起来差不多
Doesn’t look too different than what we had before this time,
只是多了这行 “#include”
other than we have this ‘#include’
它用于从 C++ 标准库引入字符串库
for the string library from the C++ standard library.
那么 然后
OK, so now, we can… umm…
我们现在要做的事是
I guess one thing that we could do at this particular time is
在 main 函数中加入一行”return 0″
do a ‘return 0’ here inside our main function.
因为 main 函数必须返回一个 int 类型的值
So our main function must return an int value,
如果程序顺利运行 我们应该返回 0
and if things are successful, we should return a value of 0.
在这个程序中 我们要提示用户输入
So in this program, we’re getting both a name
姓名和年龄
and an age from the user,
为此我们要声明变量
so we need to declare variables
用来存放这两个信息
so that it’s able to support those types.
要存放姓名 我们需要用 string 类型
So to support the name, we’ll have a type ‘string’.
而变量名是 “name”
The name of the variable will be ‘name’.
给它赋一个空字符串的初值
And then we’ll initialize it to the empty string,
空字符串就是两个双引号
so the empty string is just double quote,
中间没有空格
no space, double quote.
不知道为什么
And for some reason or another, Eclipse is compiling about…
Eclipse 对 “string” 报错
complaining about ‘string’ here.
我们先保存
So let’s go and save,
看看能不能消除掉错误
and see if we can get it from complaining, I don’t know.
或者加个空格 现在可以了
Do a space in there? OK, there it goes.
看来 Eclipse
So for whatever reason
反应有些迟钝
it’s a little bit too late in recognizing
刚刚才认出我们引入了字符串库
that we did in fact have the string library.
现在没问题了
But thing’s are OK now.
Eclipse 不再报错
It’s not complaining and fully recognizes that,
我们继续
so we’re good to go.
我们还需要声明
The other thing that we need to declare is
一个存放年龄的变量
a variable for age.
输入 “int age”
So we’ll do ‘int’, ‘age’,
也给它赋初值
and we’ll also initialize it,
赋一个 0
and we’ll initialize it to zero.
在声明变量的同时赋初值
So, it’s a good idea to always initialize your variables
是个好习惯
so soon as you declare them,
就算你马上要赋新的值也无所谓
even if you’ll overwrite them, you know, shortly after this.
现在我们要做什么呢
So what do we need to do now?
我们要提示用户
At this point, we need to prompt the user
输入姓名
to input their name.
所以如果你还记得在屏幕上输出的方法的话
So if you remember the way we got output to the screen,
在这里使用cout 就是这个cout
we use ‘cout’, so ‘cout’,
然后使用插入符<<
and then we use the insertion operator,
再然后 在这里打上一些文字
and then we could type some text, so maybe
比如 “Enter your name”
‘Enter your name’,
冒号 空格 然后打上反斜线和n
colon, space, and we’ll do slash, ‘n’ here
在这里放置一个换行符
to put a new line character there.
好的 然后我们要…
Alright, and then we’ll do…
实际上 在这里我们打算做一些之前没做过的
actually we’ll do something new here.
比如 做cin
So let’s do ‘cin’,
替代cout的字符输出
so instead of ‘cout’ for doing character output,
可以使用cin进行字符的输入
we can do ‘cin’ for character input.
cin 空格 然后
So ‘cin’, space, and this time,
在这里替换掉插入运算符<<
instead of doing the insertion operation,
我们把这个叫做取出
we’ll do what’s called extraction.
也可以叫它输入符
We could also call this the input operator,
因为它从键盘输入
where we’re getting input from the keyboard.
指向了其他目录
So it goes in the other direction,
尖括号 尖括号
so close angle bracket, close angle bracket,
空格
space,
现在我们就需要指明我们实际从键盘输入的信息了
and now we need to say what are we actually putting
随便输入
the keyboard input, whatever they type in,
比如他们的名字是Bob
maybe their name is Bob…
也就是将要存储的特殊值
and where we’re gonna store that particular value,
我们把它存储到name
Well we’re gonna store it into ‘name’,
所以在这里输入name
so we put ‘name’ here.
然后空出一行
Come down a couple of lines.
我们一会儿会做另外的两行系列
We’ll do another series of two lines,
就像这样的 不过不是这次
just like this, except this time
在这里打上”Enter your age”冒号
we’re going to use ‘Enter your age’, colon,
我想我不应该在这里添加换行符
and maybe I won’t put the new line character there
这样你就能清楚的看到
so you can see the difference between having a new line character
换行符有无的区别了
and not having the new line character.
在上一个程序中 这个区别展现的不是很清楚
We really didn’t get a nice showcase of the difference
不过这次不会了
in our last program, but we’ll see it here,
在这里打上分号
and put a semicolon there.
然后继续做”cin”
And then we’ll do ‘cin’,
使用提取符
and use our extraction operator,
把一些东西提取到屏幕
extracting something from the keyboard,
在这个案例中 把这些东西(输入)放进了变量”age”中
and placing it into, in this case, the variable ‘age’.
所以现在 无论你在”Enter your name”之后键入什么
So now, whatever they’ve typed in
它都将被存储到”name”这个变量中
after ‘Enter your name’ will be stored in ‘name’.
同样的 在”Enter your age”之后的任何键入
And then whatever they’ve typed in
都会被存储到变量”age”中
after ‘Enter your age’ will be stored in ‘age’.
好的 最后让我们来输出这些我们刚才输入的
Alright, and then finally we’ll output to them basically
基本相同的信息
same information that they inputted to us.
这里是cout
So we’ll say ‘cout’,
然后是插入符
and then use the insertion operator,
“Your name is”和冒号
and say ‘Your name is’, colon,
再一次求取插入符 然后是”name”
and then insertion operator again, ‘name’,
之后 或许可以输入一个句号
and then maybe after that, we put a period.
好的 现在为了知道换行符是如何工作的
And let’s make use of the new line character again,
我们要再一次利用这个换行符了 键入换行符
and see how this works, so new line character,
然后键入
and then we’ll say
“Your age is” 冒号 空格
‘Your age is’, colon, space,
插入符的另一边
and come on the other side of that,
键入”age”
and then we’ll say ‘age’,
然后在结尾处也键入一个句号吧
and then we’ll end that line with a period as well,
所以这里放置一个句号
so let’s go ahead and put a period in there.
老天 不是逗号 是句号
Oops, not a comma but a period.
结尾处添加一个分号
And then put a semicolon at the end.
每次你要创建一个字符串时
So each time you’re interested in specifying a string literal,
那就是我们必须确保字符串在双引号之间
we need to make sure that string literal is inside double quotes,
除非 你有一些变量来存储字符串
whereas if we have some variable,
那样的话仅仅键入这个变量名就可以了
we just type that variable name as it is.
就像这样
OK… kind of showing, I guess,
我认为我们在这里做的分组是合理的
logical grouping of what we’re doing here,
这就是合乎逻辑的变量声明
and this is, logically, declaring our variables,
提示用户输入名字 然后再输入名字
prompting the user for the name, then getting the name,
提示用户输入年龄 好后就得到了年龄
prompting user for the age, then getting the age,
再然后显示出用户的名字和年龄
and then displaying both the user’s name and their age.
好的 保存下这段代码
Alright, so let’s go and save this.
那么刚才写的程序现在就可以编译了
OK, so now we’re ready to compile our program,
所以现在点击这里的”Build”按钮
so go ahead and click on the ‘Build’ button here,
然后再一次点击”Build”按钮进行
and again, the ‘Build’ button involves
编译和链接
both compiling and linking,
现在这段代码就被编译成了
so we’re compiling this code here
一个目标文件
and generating an object file
这个文件还会被链接到”iosream”和”string”库
that will be linked with other object files
里的其他目标文件
from ‘iostream’ and ‘string’ library,
然后就会生成一个运行在特别架构上的
and then we’ll generate an executable file
可执行文件
that runs on this particular architecture.
这些步骤就是每次你点击”Build”按钮的时候编译器所完成的工作
So all that’s going on whenever we click this ‘Build’ button here.
现在点击”Run”按钮
And now we’ll actually run our program
用来运行程序
by clicking on the ‘Run’ button.
下方就是控制台了
And down here on the console,
实际上我们现在不得不从键盘端输入一些东西
we’ll actually have to input something from the keyboard.
你看 这里打印出”Enter your name”
So it says, ‘Enter your name’,
现在如果左键单击控制台内的某处
and if we left click somewhere within this console,
就会发现这里出现了一个插入点
you’ll notice that it puts the insertion point
就在”Enter your name之下”
here below ‘Enter your name’,
这也就是插入换行符的原因了
and that’s because we put this new line character.
如果在这里没有添加换行符
If we didn’t put the new line character in there,
那么就不得不和”Enter your name”
then we’ll be inputing the name
在同一行输入了
on the same line as ‘Enter your name’.
现在在这里键入名字 “Bob”
So we’ll say that the name is ‘Bob’,
现在等一小会儿
and then now I’m justing waiting,
假设名字是 “Bobby”
you know, maybe our name is ‘Bobby’
所以继续键入”by”
so we could type in ‘by’,
或者说我们还想在这里输入姓和名
or maybe we put a first name and a last name there.
现在这个程序不会做任何事除非我们敲击”Enter”
It’s not gonna do anything more until we hit the ‘Enter’ button,
所以实际上它正在等待操作
so it’s basically waiting for us to say
等待我们从键盘端输入名字
that we’re done typing our name.
现在敲击”Enter”
So hit ‘Enter’,
然后输入年龄
and now we’re gonna enter our age,
或许这个Bob的年龄是22
and maybe the age of ‘Bob’ is
所以我们在这里键入22
22, so we put 22 in there,
然后敲击”Enter”
and hit ‘Enter’.
现在它打印出 “Your name is: Bob.”
And now it says ‘Your name is: Bob’,
“Your age is: 22.”
and ‘Your age is: 22’.
现在来看这一行
So all that came from this single line here,
尽管我们在这里仅仅敲了一行代码
even though we type it out on a single line,
输出结果框却显示了两行
it showed up on two different lines.
为什么会出现两行呢
Why did it show up on two different lines?
看这里 之前我们在这里插入了一个换行符
Well, we inserted this new line character,
完成所有的填充后插入了一个换行符
so after it finished all the stuff here,
自始至终它都相当于在那个句号之后
all the way up to that period,
提供了一个换行符
it then provided that new line character starting here,
把余下的内容放到了下一行
and put all this stuff on that next line.
好的 接下来需要注意它是如何把name
OK, and notice how it substituted in
替换为实际的”Bob”的
the actual name ‘Bob’,
就在这里 有一个name变量 它输出了Bob
where we had ‘name’ here, it put ‘Bob’,
同样的 这个age被替换成了22
and where we had ‘age’, it substituted in 22.
结果就是每次使用变量
So that’s what’s gonna happen whenever you output
输出一些叙述的时候
some statement and you’re using variables,
这个独有的变量名实际上会被替代成数值
it’s actually gonna replace that particular variable name
因为这个数值被存储在变量所在的位置
with the value that stored at that variable location.
好的 这基本上就是第二个程序所包含的内容了
So that’s basically our second program,
我们看了多个不同类型的变量的声明
we looked at declaring a couple of variables of different types,
比如”string” 还有”int”
one of type ‘string’, the other type ‘int’.
我们也看到了”cin”
We also looked at using ‘cin’
用来从键盘输入
to get input from the keyboard,
再然后 我们复习了”cout”的用法
and then we reviewed using ‘cout’.
在下个程序中
So, in our next program,
我可能会讲解一些基本的运算符操作
we’ll probably be doing some, maybe basic arithmetic operation.

发表评论

译制信息
视频概述

颈椎病养成之路 第一章 C++ 第四讲 程序的输入与输出

听录译者

HX

翻译译者

[B]Azrael

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=S8rICXxrKU4

相关推荐