作者yangwen5301 (大番薯)
看板NTUBIME-103
標題About C++HW01
時間Mon Sep 27 23:00:42 2010
Somebody ask about C++, so I post it here.
(I can't type chinese on my telnet.)
The following contents are for references. What teacher tells is more important:
The "//" written in the .cpp means annotations. It doesn't effects the program.
The program will always needed to be written in the "main" function.
This is how it works:
#include <iostream> //I forget what it is, but it is surely needed.
int main()
{
}
But we still need a command to call a function.
So, here are two ways to enter the command:
1.enter "std:" at the top of the line when you need to call a function.
2.#include <iostream>
using namespace std; //This is the command
int main()
{
}
Basic commands to know:
1.int a;
This command is used to give a value to the memory(RAM) by giving it a char a.
You can replace a with what ever you want. It's up to you.
Somehow, if you want to enter a dicimal fraction,
you have to use "double" instead.
2.cout<<"";
This command can let you display words on the monitor.
Type the words you want to display inside the "".
But sometimes unexpected errors happened,
For example: "\n" written in the "" is a command to change line.
Be aware!
3.cin>>a;
This is a command to let users input a character or a value.
replace "a" with the character that you have assigned for the function.
4."=" and "=="
"="means to assign a thing to another.
ex:a=4 means a is 4
But"==" means equal.
ex:e==5 means e equals 5.
5.system ("pause");
This command makes the window stay open.
6.return 0;
This command give the computer the command what should the computer do next
after the program ends. This line is neccesary, too. It must be added.
Here's a example for a program(Used to calculate average.):
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
double sum;
cout<<"Please enter three integers.\n";
cin>>a;
cout<<"\n"; //Used just to change line.
cin>>b;
cout<<"\n";
cin>>c;
sum=(a+b+c)/3;
cout<<"The average of the three integer is:"<<sum;
system ("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.36.245.17
1F:推 freezingice:威哥!! 推推推!! 09/27 23:00
※ 編輯: yangwen5301 來自: 114.36.245.17 (09/27 23:09)
※ 編輯: yangwen5301 來自: 114.36.245.17 (09/27 23:10)
※ 編輯: yangwen5301 來自: 114.36.245.17 (09/27 23:11)
2F:推 yogira:可以使用PCMan Lite(不推Combo)或者PieTTY。 09/27 23:14
3F:推 steve1012:C++不能抄 這樣丟上來不是害我少了一種方法哈哈 09/27 23:19
4F:推 ss355227:這篇是? 等不及功課板嗎XD 09/27 23:25
5F:推 m07070:What the ... 09/28 00:57
6F:→ yangwen5301:The example is not the homework. 09/28 13:21