c++很菜的问题,帮忙解决一下

来源:百度知道 编辑:UC知道 时间:2024/04/28 04:10:06
我想做一个小程序就是如果输入a1 他就会输出相应的数字比如
a1=10
a2=45
你输入a1会输出10
......a2......45
这样子的....谁能帮我写一下~~小弟感激不尽

#include <iostream.h>
#include <string.h>
void main()
{
char test[3];
cin >>test;
if (strcmp(test,'a1')==0)
cout<<10;
else if (strcmp(test,'a2')==0)
cout<<45;
}

//用switch-case实现
#include <iostream>
using namespace std;
void main()
{
char a,b;
cin>>a>>b;
switch(b)
{
case '1': cout<<10<<endl;break;
case '2': cout<<45<<endl;break;
}
}
//我的最短,最简单

#include <string.h>
#include <iostream.h>
void main()
{
char test[3];
cin >>test;
if (strcmp(test,'a1')==0)
cout<<"10"<<endl;
else if (strcmp(test,'a2')==0)
cout<<"45"<<endl;
}

定义一个数组test[3],和'a1','a2'比较。相同输出对应的值。
s