免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10709 | 回复: 19

[C++] 这是我学习C++的第一个例子,很多错误,谁帮我看一下啊? [复制链接]

论坛徽章:
0
发表于 2009-09-28 13:13 |显示全部楼层
20可用积分
截图
--------------------
cc.PNG

最佳答案

查看完整内容

#include "stdafx.h"long fn(long n) { long temp=0; int i,flag=1; if(n

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
发表于 2009-09-28 13:13 |显示全部楼层
#include "stdafx.h"

long fn(long n)
{
        long temp=0;
        int i,flag=1;
        if(n<=0)
        {
                printf("error: n must > 0"); //刚才掉了后面的引号。。。。
                exit(1);
        }
        for(i=1;i<=n;i++)
        {
                temp=temp+flag*i;
                flag=(-1)*flag;
        }
        return temp;
}

int _tmain(int argc, _TCHAR* argv[])
{
        long temp;
        
        temp = fn(15);
        
        return 0;
}

论坛徽章:
0
发表于 2009-09-28 13:14 |显示全部楼层
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
        long temp;
       
        return 0;

        temp = fn(15);
       
}
public long fn(long n)
{
        long temp=0;
        int i,flag=1;
        if(n<=0)
        {
                printf("error: n must > 0"); //刚才掉了后面的引号。。。。
                exit(1);
        }
        for(i=1;i<=n;i++)
        {
                temp=temp+flag*i;
                flag=(-1)*flag;
        }
        return temp;
}

[ 本帖最后由 TOADLover 于 2009-9-28 13:23 编辑 ]

论坛徽章:
0
发表于 2009-09-28 13:16 |显示全部楼层
VS的WIN32程序。

论坛徽章:
0
发表于 2009-09-28 13:24 |显示全部楼层
Error        1        error C2059: syntax error : 'constant'       

c:\documents and settings\Henry\my documents\visual studio 2005\projects\win32_console1\win32_console1\win32_console1.cpp        9

论坛徽章:
0
发表于 2009-09-28 13:26 |显示全部楼层
嗯,刚才有点糊涂:
-------------------------
if (5 > 1)
        {
                printf("error: n must > 0");
        }
-------------------------
if 5 > 1
        {
                printf("error: n must > 0");
        }
--------------------------
后者,自然是不正确的。

论坛徽章:
0
发表于 2009-09-28 13:27 |显示全部楼层

C++中的public是干嘛用的?

public long fn(long n)
{
        long temp=0;
        int i,flag=1;
        if(n<=0)
        {
                printf("error: n must > 0");
                exit(1);
        }
        for(i=1;i<=n;i++)
        {
                temp=temp+flag*i;
                flag=(-1)*flag;
        }
        return temp;
}
-------------------------------------------------------------------------
这里的public是不应该有的!

论坛徽章:
0
发表于 2009-09-28 13:29 |显示全部楼层

C++中的exit又是怎么用的?

Error        1        error C3861: 'exit': identifier not found       
c:\documents and settings\henry\my documents\visual studio 2005\projects\win32_console1\win32_console1\win32_console1.cpp        25
-----------------------------
原来是没有引用stdlib.h的原因:
-----------------------
// win32_console1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"

[ 本帖最后由 TOADLover 于 2009-9-28 13:35 编辑 ]

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
发表于 2009-09-28 13:34 |显示全部楼层
还有用exit函数得include <stdlib.h>

论坛徽章:
0
发表于 2009-09-28 13:38 |显示全部楼层

回复 #8 hellioncu 的帖子

怎么用readline函数等待客户键盘输入呢?

        if (5 > 1)
        {
                printf("error: n must > 0");
                readline();       
        }
        return 0;
---------------------------------------
嗯,最后用
char c2;
        c2=getchar();
---------------------------------------
等待用户输入
++++++++++++++++++++++++++++++++++
用getch();//需要#include <conio.h>
或者getchar();//需要#include <stdio.h>
或者system ("PAUSE");//需要#include <stdlib.h>
-------------------------------------------------------------
据说getch()好像只有在borland c++上工作,getchar()在MS VC++上工作,system("PAUSE")应该在windows都工作
---》“在windows都工作” 是什么意思?
++++++++++++++++++++++++++++++++++

[ 本帖最后由 TOADLover 于 2009-9-28 14:22 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP