佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 2851|回复: 99

关于C++的问题。。。。

[复制链接]
发表于 14-10-2008 05:13 PM | 显示全部楼层 |阅读模式
Assignment 交了, 就别管它了。

最近学了return, 可是不是很明白。我知道除了void function 以外, int, double, float 等都可以return。 可是为什么要return? 有return跟没return有什么不同?

以下是我刚刚作的, 没有error可是run的时候停在不应该停的地方
  1. #include <iostream.h>
  2. #include <stdlib.h>

  3. int calc(int, int);

  4. int main()
  5. {
  6. int x=0, y=0, answer;
  7. cout<<"lease enter the value of x.\n";
  8. cin>>x;
  9. cout<<"lease enter the value of y.\n";
  10. cin>>y;
  11. answer=calc(x, y);
  12. cout<<" x times y is equals to "<<answer<<".\n";
  13. }

  14. int calc (int x, int y)
  15. {
  16. return x*y;
  17. }
复制代码
问题2:
学了file handling, 所以就试试看,打了一个file做data file。 program没有error, 可是却call不出来
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <fstream.h>

  4. ifstream file_input ("name.dat";

  5. void main()
  6. {
  7. char name[100];

  8. for (int x=1; !file_input.eof(); x++)
  9. {
  10. cout<<"\n\t"<<x<<"\t "<<name;
  11. file_input>>name;
  12. }
  13. file_input.close();
  14. }
复制代码
我的name.dat是
ali
ahmad
chua
siaokia

[ 本帖最后由 Wongkokchoy 于 14-10-2008 06:05 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 14-10-2008 06:46 PM | 显示全部楼层
return 是return你的value会去function main.
第二个不大懂·你讲什么。
回复

使用道具 举报

 楼主| 发表于 14-10-2008 07:59 PM | 显示全部楼层
  1. #include <iostream.h>

  2. #include <stdlib.h>



  3. int calc(int, int);



  4. int main()

  5. {

  6. int x=0, y=0, answer;

  7. cout<<"lease enter the value of x.\n";

  8. cin>>x;

  9. cout<<"lease enter the value of y.\n";

  10. cin>>y;

  11. answer=calc(x, y);

  12. cout<<" x times y is equals to "<<answer<<".\n";

  13. }



  14. int calc (int x, int y)

  15. {

  16. return x*y;

  17. }
复制代码


这个吗?
我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 | 显示全部楼层
大大的建议我都试过了, 可是也不能解决问题。。。。
回复

使用道具 举报

Follow Us
发表于 15-10-2008 11:23 AM | 显示全部楼层
加悲哀河川讲的不是能吗?试过了,能喔。。。
回复

使用道具 举报

 楼主| 发表于 15-10-2008 11:37 AM | 显示全部楼层
har.....我用的是Borland C++, 更正了还是不能咧...

Borland 坏了吗?
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
那么你把error msg贴出来看看。
回复

使用道具 举报

 楼主| 发表于 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.
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <fstream.h>


  4. void main()
  5. {
  6. char name[100];

  7. ifstream file_input ("name.dat");

  8. if (!file_input)
  9. {
  10.     cout << "error opening file" << endl;
  11.     exit(1);
  12. }

  13. for (int x=1; !file_input.eof(); x++)
  14. {
  15.     file_input.getline( name, 100);
  16.     cout<<"\n\t"<<x<<"\t "<<name;
  17. }
  18. file_input.close();
  19. }
复制代码

[ 本帖最后由 onlylonly 于 16-10-2008 12:52 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 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 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 12:12 AM , Processed in 0.297355 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表