|
查看: 2854|回复: 99
|
关于C++的问题。。。。
[复制链接]
|
|
|
Assignment 交了, 就别管它了。
最近学了return, 可是不是很明白。我知道除了void function 以外, int, double, float 等都可以return。 可是为什么要return? 有return跟没return有什么不同?
以下是我刚刚作的, 没有error可是run的时候停在不应该停的地方 - #include <iostream.h>
- #include <stdlib.h>
- int calc(int, int);
- int main()
- {
- int x=0, y=0, answer;
- cout<<"lease enter the value of x.\n";
- cin>>x;
- cout<<"lease enter the value of y.\n";
- cin>>y;
- answer=calc(x, y);
- cout<<" x times y is equals to "<<answer<<".\n";
- }
- int calc (int x, int y)
- {
- return x*y;
- }
复制代码 问题2:
学了file handling, 所以就试试看,打了一个file做data file。 program没有error, 可是却call不出来 - #include <iostream.h>
- #include <stdlib.h>
- #include <fstream.h>
- ifstream file_input ("name.dat";
- void main()
- {
- char name[100];
- for (int x=1; !file_input.eof(); x++)
- {
- cout<<"\n\t"<<x<<"\t "<<name;
- file_input>>name;
- }
- file_input.close();
- }
复制代码 我的name.dat是
ali
ahmad
chua
siaokia
[ 本帖最后由 Wongkokchoy 于 14-10-2008 06:05 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 14-10-2008 06:46 PM
|
显示全部楼层
return 是return你的value会去function main.
第二个不大懂·你讲什么。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 14-10-2008 07:59 PM
|
显示全部楼层
- #include <iostream.h>
- #include <stdlib.h>
- int calc(int, int);
- int main()
- {
- int x=0, y=0, answer;
- cout<<"lease enter the value of x.\n";
- cin>>x;
- cout<<"lease enter the value of y.\n";
- cin>>y;
- answer=calc(x, y);
- cout<<" x times y is equals to "<<answer<<".\n";
- }
- int calc (int x, int y)
- {
- return x*y;
- }
复制代码
这个吗?
我run的时候, 在我`enter y 的 value 之后就停了(run 完了), 不知道什么问题.... |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 01:02 AM
|
显示全部楼层
你忘记方getch()了!
正确code:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int calc(int, int);
int main()
{
int x=0, y=0, answer;
cout<<"lease enter the value of x.\n";
cin>>x;
cout<<"lease enter the value of y.\n";
cin>>y;
answer=calc(x, y);
cout<<" x times y is equals to "<<answer<<".\n";
getch();
} |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 01:10 AM
|
显示全部楼层
原帖由 Wongkokchoy 于 14-10-2008 07:59 PM 发表 
#include
#include
int calc(int, int);
int main()
{
int x=0, y=0, answer;
coutx;
couty;
answer=calc(x, y);
cout
MAIN没有return 0? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-10-2008 09:08 AM
|
显示全部楼层
|
大大的建议我都试过了, 可是也不能解决问题。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 11:23 AM
|
显示全部楼层
加悲哀河川讲的不是能吗?试过了,能喔。。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-10-2008 11:37 AM
|
显示全部楼层
har.....我用的是Borland C++, 更正了还是不能咧...
Borland 坏了吗? |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 12:48 PM
|
显示全部楼层
回复 8# Wongkokchoy 的帖子
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int calc(int, int);
int main()
{
int x=0, y=0, answer;
cout<<"lease enter the value of x.\n";
cin>>x;
cout<<"lease enter the value of y.\n";
cin>>y;
answer=calc(x, y);
cout<<" x times y is equals to "<<answer<<".\n";
getch();
}
int calc (int x, int y)
{
return x*y;
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-10-2008 02:19 PM
|
显示全部楼层
那么可能我的 borland C++ 有问题了, 我直接copy你的code下去也不能 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-10-2008 08:16 PM
|
显示全部楼层
可是我把code给我的朋友try, 也是不行呢。 |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 09:00 PM
|
显示全部楼层
|
你是在command prompt里跑你的程序吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-10-2008 09:56 PM
|
显示全部楼层
我用borland C++ 里面的run 来run咯。。
试了几架电脑, 可是都不行  |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 10:25 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-10-2008 10:41 PM
|
显示全部楼层
不是有error, 可是key in x 和 y 之后就停了 |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 11:53 PM
|
显示全部楼层
回复 1# Wongkokchoy 的帖子
turbo c++ 5.02 的问题吧
1.
尝试
int calc (int x, int y)
{
return (x*y);
}
或
int calc (int x, int y)
{
int product = x * y;
return product;
2.- #include <iostream.h>
- #include <stdlib.h>
- #include <fstream.h>
- void main()
- {
- char name[100];
- ifstream file_input ("name.dat");
- if (!file_input)
- {
- cout << "error opening file" << endl;
- exit(1);
- }
- for (int x=1; !file_input.eof(); x++)
- {
- file_input.getline( name, 100);
- cout<<"\n\t"<<x<<"\t "<<name;
- }
- file_input.close();
- }
复制代码
[ 本帖最后由 onlylonly 于 16-10-2008 12:52 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 16-10-2008 12:05 AM
|
显示全部楼层
cout<<" x times y is equals to "<<answer<<".\n";试试换成
cout<<" x times y is equals to "<<answer<<endl;
然后在getch();下面放 return 0; |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 16-10-2008 09:52 AM
|
显示全部楼层
大大的建议统统都不行
还是没改变。。。。
要reinstall borland c++ 吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 16-10-2008 09:59 AM
|
显示全部楼层
回复 16# onlylonly 的帖子
onlylonly, 为什么当我run你给我的code的时候, 他显示error opening file。
到底是什么一回事?
我现在的lecturer 过于注重theory, 不曾真正run 给我们看, 所以不懂那里出错了 |
|
|
|
|
|
|
|
|
|
|
发表于 16-10-2008 10:03 AM
|
显示全部楼层
回复 19# Wongkokchoy 的帖子
很明显是文件名字错误, 应该你是将 data.dat.txt 当成 data.dat
在windows里, 默认是没有显示文件 extention 的, 去 windows explorer 或 my computer / tools/ folder options/ view/ advance steeings 哪里, 寻找 hide extension for known file type, 取消勾勾
[ 本帖最后由 onlylonly 于 16-10-2008 10:04 AM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|