| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1285 人关注过本帖
标题:写了一个小练习 有错误 请大神们给瞅瞅呗
只看楼主 加入收藏
哒哒哒啦啦啦
Rank: 1
等 级:新手上路
帖 子:75
专家分:4
注 册:2016-2-26
结帖率:76.92%
收藏
已结贴  问题点数:20 回复次数:6 
写了一个小练习 有错误 请大神们给瞅瞅呗
题目是:输入一个序号,作为对应的箱子号,但是老板不喜欢4,所以把有4的数字全都剔除出去,我这个理论上只在100以内成立,可是编译完了有一个错误,搜了半天也不知道怎么改,想法是先列一个有100个数的数组(元素是0-100),用一个指针指向首地址,从1开始循环,循环次数小于输入值,当判断出来指向的数子含有4时,就指向下一个地址。请大神们瞅瞅,先给跪了
#include "stdAfx.h"
#include <string>
#include<iostream>
#include <tchar.h>
using namespace std

int _tmain(int argc,_TCHAR*argv[])
{
int input;
int a=0,n=0,i=1;
int *p;
long n[100];
cout<<"请输入序号:"<<endl;
cin>>input;
for(;i<=100;i++)
{
    n[i]=i;
}
p=n;
for(;a<=input;a++,p++)
{
if   ((a/10)%10==4||a%10==4)==1) //(a/100)%10==4;
{
    p=p+1;
}
}
cout<<"箱号为:"<<*p<<endl;
return 0;
}

//这是报的错误。。。
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
C:\Users\Jin\Desktop\a.cpp(7) : error C2144: syntax error : missing ';' before type 'int'
C:\Users\Jin\Desktop\a.cpp(7) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.

a.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: include 箱子 元素 
2016-02-26 22:43
哒哒哒啦啦啦
Rank: 1
等 级:新手上路
帖 子:75
专家分:4
注 册:2016-2-26
收藏
得分:0 
等啊等
2016-02-26 22:51
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:20 
输入的数为input,输出应该是没有4箱号的箱子的排第input个的箱子的箱号吧。
程序代码:
#include <iostream>

using namespace std;

int main()
{
    int input;
    int a=0,i=1;
    int *p;
    int n[100];

    cout<<"Please input index:";
    cin>>input;

    for(;i<=100;i++)
    {
       n[i-1]=i;
    }
    p=n;
    for(;a<input;p++)
    {
       //cout<<(*p);
       if((*p)%10==4||(*p)/10==4)  continue;
       a++;
    }

    cout<<"Xianghao is:"<<*(--p)<<endl;

    return 0;
}

   唯实惟新 至诚致志
2016-02-27 08:41
哒哒哒啦啦啦
Rank: 1
等 级:新手上路
帖 子:75
专家分:4
注 册:2016-2-26
收藏
得分:0 
回复 3楼 qq1023569223
谢谢大神,看了半天明白了,然后改了一下我原来的循环,原来循环有问题,但还是报错。。。感觉他报的这几项好像没问题呀。。
#include "stdAfx.h"
#include <string>
#include<iostream>
#include <tchar.h>
using namespace std;

int _tmain(int argc,_TCHAR*argv[])
{
int input;
int a=0,n=0,i=1;
int *p;
int n[100];
cout<<"请输入序号:"<<endl;
cin>>input;
for(;i<100;i++)
{
    n[i-1]=i;
}
p=n;
for(;a<input;a++,p++)
{

if((*p)%10==4||(*p)/10==4)
{
    a=a-1;
    p=p+1;
}
}
cout<<"箱号为:"<<*(--p)<<endl;
return 0;
}
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
C:\Users\Jin\Desktop\a.cpp(12) : error C2040: 'n' : 'int [100]' differs in levels of indirection from 'int'
C:\Users\Jin\Desktop\a.cpp(17) : error C2109: subscript requires array or pointer type
C:\Users\Jin\Desktop\a.cpp(17) : error C2106: '=' : left operand must be l-value
C:\Users\Jin\Desktop\a.cpp(19) : error C2440: '=' : cannot convert from 'int' to 'int *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

a.obj - 1 error(s), 0 warning(s)
2016-02-27 09:59
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
首先我不敢保证你的算法是不是对的,但是语法上,你定义一个整数n却不用,后面又定义了一个数组n,所以这样就混乱了。

   唯实惟新 至诚致志
2016-02-27 10:08
哒哒哒啦啦啦
Rank: 1
等 级:新手上路
帖 子:75
专家分:4
注 册:2016-2-26
收藏
得分:0 
回复 5楼 qq1023569223
谢谢,一举中的呀,删了后又改了一句就对了
2016-02-27 11:13
哒哒哒啦啦啦
Rank: 1
等 级:新手上路
帖 子:75
专家分:4
注 册:2016-2-26
收藏
得分:0 
这是最后正确的:
#include "stdAfx.h"
#include <string>
#include<iostream>
#include <tchar.h>
using namespace std;

int _tmain(int argc,_TCHAR*argv[])
{
int input;
int a=1,i=1;
int *p;
int n[100];
cout<<"请输入序号:"<<endl;
cin>>input;
for(;i<100;i++)
{
    n[i-1]=i;
}
p=n;
for(;a<=input;a++,p++)
{

if((*p)%10==4||(*p)/10==4)
{
    a=a-1;
                         //这里原来的p=p+1删去了,当遇到4时,a不加一,再循环第二次时*p就已经向后挪一位了                        
}
}
cout<<"箱号为:"<<*(--p)<<endl;
return 0;
}

[此贴子已经被作者于2016-2-27 11:18编辑过]

2016-02-27 11:14
快速回复:写了一个小练习 有错误 请大神们给瞅瞅呗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013228 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved