| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1270 人关注过本帖
标题:打印收据,数组越界的问题
只看楼主 加入收藏
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
结帖率:100%
收藏
 问题点数:0 回复次数:4 
打印收据,数组越界的问题
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Receipt
{
private:
        string name;
        string item;
        double money;
        string money_CHN;
        int year;
        int month;
        int day;
public:
        void setReceipt(string n,string i,double m,int y,int mt,int d);
        void Convert();
        void showReceipt();
        void print();
        void menu();
};
int main()
{
        int num;
        Receipt r;
        do
        {
            r.menu();
            cin>>num;
            if(num==0)    break;
            else
            {
                    r.print();
                    r.Convert();
                    r.showReceipt();
                }
        }while(num != 0);
}
void Receipt::setReceipt(string n,string i,double m,int y,int mt,int d)
{
    name=n;
    item=i;
    money = (double)((int)(m * 100)) / 100;
    year=y;
    month=mt;
    day=d;
}
void Receipt::Convert()
{
    int m;
        m = (int)(money * 100);
        string str1[] = {"分","角","元","拾","佰","仟","万","拾"};
        string str2[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        int group[8];
        int i;
        for(i = 0; m > 0; i++)
        {
            group[i] = m % 10;
            m = m / 10;
        }
        //moneyLenth = i + 1;
        for(int j = 7; j >= 0; j--)
        {
            money_CHN = money_CHN + (str2[group[j]] + str1[j]);
        }
}
void Receipt::print()
{
    name = "jonny";
    item = "jonny";
    money = 123.123;
    year = 2010;
    month = 2;
    day = 1;
    setReceipt(name, item, money, year, month, day);
}
void Receipt::showReceipt()
{
    //system("cls");
    /*cout<<"’?‘/¥???£?《?…‘∫?"<<endl;*/
    /*Sleep(1000);*/
    /*system("cls");*/
    //cout<<"\t\t"<<" ’?? ’ae〉"<<"\t\t"<<endl;
    //cout<<year<<"??"<<month<<"‘?"<<day<<"》’"<<"\t\t\t\t"<<"No.00001"<<endl;
    //cout<<"---------------------------------------------------"<<endl;
    //cout<<"Ω???μ·OE???Ω???》??’√°£∫"<<name<<endl;
    //cout<<"Ω???oe???£∫"<<item<<endl;
    //cout<<"???" << money_CHN;
    //cout<<endl;
    //cout<<"\t\t\t"<<money<<"‘(TM)"<<endl;
    //cout<<"---------------------------------------------------"<<endl;
    cout << money_CHN;
}
void Receipt::menu()
{
        //cout<<"---------------------------------"<<endl;
        //cout<<"               ≦?μ·              "<<endl;
        //cout<<"---------------------------------"<<endl;
        //cout<<"             0.??≧^              "<<endl;
        //cout<<"             1.?(TM) ’ae〉            "<<endl;
        //cout<<"---------------------------------"<<endl;
}

代码如上,很多已经注释掉了
但是运行的时候提示错误
调试的时候执行完for语句,就直接run stopped
提示:Terminated due to signal: SEGMENTATION FAULT (11)
实在是找不出问题在哪里, 有没有大神
搜索更多相关主题的帖子: private include public double money 
2017-03-28 00:29
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:0 
数组越界,观测下标变动情况可找到原因。
2017-03-28 05:25
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
回复 2楼 吹水佬
但是实际上数组并没有越界
2017-03-28 07:54
xzlxzlxzl
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖北
等 级:贵宾
威 望:125
帖 子:1091
专家分:5825
注 册:2014-5-3
收藏
得分:0 
类成员函数Convert中做一部分修改,代码如下(红色部分为修改部分):

void Receipt::Convert()
{
    int m;
        m = (int)(money * 100);
        string str1[] = {"分","角","元","拾","佰","仟","万","拾"};
        string str2[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        int group[8];
        int i;
        for(i = 0; m > 0; i++)
        {
            group[i] = m % 10;
            m = m / 10;
        }
        //moneyLenth = i + 1;
        money_CHN="";                     //将中文显示部分清空,不然第二次调用会累加第一次的数据
        for(int j = i-1; j >= 0; j--)     //这里必须从i-1开始,你不是每一个数都会是8位数
        {
            money_CHN = money_CHN + (str2[group[j]] + str1[j]);
        }
}
2017-03-28 08:34
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
以下是引用Jonny0201在2017-3-28 07:54:58的发言:

但是实际上数组并没有越界

没什么好吵的,有没有越界,是一个事实的存在,不是靠嗓门大就能让它凭空消失或凭空产生的。

        for(int j = 7; j >= 0; j--)
        {
你可以输出 group[j] 看看
            money_CHN = money_CHN + (str2[group[j]] + str1[j]);
        }

所以,建议改为 int j=i-1

2017-03-28 08:42
快速回复:打印收据,数组越界的问题
数据加载中...
 
   



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

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