任何人都可以帮助我这个c ++程序有一些错误

时间:2015-11-19 12:02:21

标签: c++ ascii

我想编写一个关于ascii的程序,它会给你char,并要求你输入小数,它只能选择字符或全部。有很多错误,我不知道如何解决它们。请帮我。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void ALL();
void character();
void ask();

int main(){
ask();
return 0;
}

void ask(){

    char type;
    char character;
    char all;
    cout<<"You can choose all or only character\ttype in(all / character)"<<endl;
    cin>>type;
    do{
if(type=='character'){
    character();
}else if(type=='all'){
    ALL();
}else{
    ask();
}
}while(1);
}

void ALL(){
int a, b;   
do{
    srand((unsigned)time(NULL));
    a=rand() %255 + 1;
    b=a;
    cout<<"What is the ascii code of "<<(char) a<<" ?"<<endl;
    cin>>b;
    if(b==a){
        cout<<"You are RIGHT!!!"<<endl;
        cout<<"----------------------------------\n----------------------------------"<<endl;
    }else{
        cout<<"You are WRONG!!!"<<endl;
        cout<<"The ascii code of "<<(char) a<<" is "<<a<<endl;
        cout<<"----------------------------------\n----------------------------------"<<endl;
    }
}while(1);

}

void character(){
    int a, b, c;    
do{
    srand((unsigned)time(NULL));
    c = rand() % 2;
    if(c==1){
        b = rand() % 26 + 65;
    }else if(c==2){
        b = rand() % 26 + 97;
    }

    b=a;
    cout<<"What is the ascii code of "<<(char) a<<" ?"<<endl;
    cin>>b;
    if(b==a){
        cout<<"You are RIGHT!!!"<<endl;
        cout<<"----------------------------------\n----------------------------------"<<endl;
    }else{
        cout<<"You are WRONG!!!"<<endl;
        cout<<"The ascii code of "<<(char) a<<" is "<<a<<endl;
        cout<<"----------------------------------\n----------------------------------"<<endl;
    }
}while(1);

}

Tt在底部[编译器]

显示了这一点

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓

24 11 C:\ Users \ Thomas \ Desktop \ program \ C ++ \ ASCII_2.cpp [警告]字符常量太长,其类型[默认启用]

26 17 C:\ Users \ Thomas \ Desktop \ program \ C ++ \ ASCII_2.cpp [警告]多字符字符常量[-Wmultichar]

C:\ Users \ Thomas \ Desktop \ program \ C ++ \ ASCII_2.cpp in function&#39; void ask()&#39;:

25 13 C:\ Users \ Thomas \ Desktop \ program \ C ++ \ ASCII_2.cpp [错误]&#39;字符&#39;不能用作函数

1 个答案:

答案 0 :(得分:1)

char type;更改为string type; ..

此外,在您要比较type == 'character'的区域,您必须将其更改为

if(type == "character")
{
  ...
}

同样改变其他比较。

OR

如果你只想在这里使用char ......

输入ca

并将条件更改为

if(type == 'c')
{
 ...
}