标题:数组这里报错
只看楼主
你爸边哭边
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2017-10-30
结帖率:66.67%
已结贴  问题点数:8 回复次数:7 
数组这里报错
头文件
#include<iostream.h>
class translate
{
public:
translate()
{
    string1[9][9]={"one","two","three","four","five","six","seven","eight","nine","ten"};
    string2[9][9]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
    string3[9][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
}

    int num;
    void display();
    friend ostream& operator<<(ostream&output,translate &c);
private:
char string1[9][9];
char string2[9][9];
char string3[9][10];
};
。cpp文件
#include<iostream.h>
#include"translate.h"
 ostream& operator<<(ostream&output,translate &a)
{
    int c=a.num;
    if(c<11)
    output<<string1[c-1]<<endl;
    if(c>10&&c<20)
    output<<string3[c-11];
    if(c>20&&c%10!=0)
    output<<string2[c/10-2]<<' '<<string1[c%10-1]<<endl;
    if(c>=20&&c%10==0)
        output<<string2[c%10-1];
    return output;
}
void translate::display()
{
    cout<<"请输入一个0到99的数:"<<endl;
    cin>>num;

}
main函数
#include<iostream.h>
#include"translate.h"
void main()
{
    translate c;
    c.display();
    cout<<c;

}
这个程序哪里出错了?
搜索更多相关主题的帖子: 数组 include num void output 
2018-05-15 16:35
你爸边哭边
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2017-10-30
得分:0 
求大神回复
2018-05-15 20:01
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:4 
注明 哪行代码报告错误
错误提示是什么

---------------------------------------------
string1[9][9]={"one","two","three","four","five","six","seven","eight","nine","ten"};
string1 是一个char[9][9]
string1[9] 是一个char[9] 并且越界了
string1[9][9] 是一个char 并且越界了
char 不能用 = {.............} 进行赋值操作

https://zh.
2018-05-15 22:37
你爸边哭边
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2017-10-30
得分:0 
回复 3楼 lin5161678
char应该怎么赋值呢?
2018-05-15 23:28
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:0 
回复 4楼 你爸边哭边
看书

https://zh.
2018-05-16 00:01
你爸边哭边
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2017-10-30
得分:0 
回复 5楼 lin5161678
书上没找到,能告诉我一下吗,谢谢
2018-05-16 06:43
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:4 
这代码完全没法改
看到第一句 #include<iostream.h>,<iostream.h>在上个世纪就被C++废弃了
代码也不排版
string1[9][9]={……,很明显数组溢出了,而且既非初始化,又非赋值,看不懂
void display(); 这什么玩意儿
ostream& operator<<(ostream&output,translate &c); 为什么必须要求c是可修改的
if(c<11)…… 逻辑没看懂,难道不是 if( c<20 ) 吗
void translate::display() { cout<<"请输入一个0到99的数:"<<endl; 怎么display又变成了输入?
void main() 就不说了
最重要的,明明应当是个函数,为什么要用类

程序代码:
#include <string>
#include <cassert>

std::string foo( unsigned num )
{
    static const char* less20[]  = { "zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen" };
    static const char* tenfold[] = { "twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety" };
    assert( num < 100 );
    if( num < 20 )
        return less20[num];
    if( num%10 == 0 )
        return tenfold[num/10-2];
    return tenfold[num/10-2] + std::string(" ") + less20[num%10];
}

#include <iostream>
using namespace std;

int main( void )
{
    cout<<"请输入一个0到99的数:"<<endl;
    unsigned num;
    cin >> num;
    cout << foo(num) << endl;
}

2018-05-16 09:54
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
得分:0 
回复 7楼 rjsp
吐槽辛苦了
是不是感到呼吸急促
我不敢吐槽就是怕和你一样
喘不过气

https://zh.
2018-05-16 11:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486949-1-1.html




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

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