lyxleo 2019-04-04 17:38 采纳率: 0%
浏览 661

C++中将输入的值存入指针后如何正确存读输入值?

我写了一段C++代码,要求输入一个值存入指针,再通过这个指针读取值。该怎样输入读取我忘了,是不是像我这样?求帮帮忙!

#include <iostream>
using namespace std;
int main(){
    char *command;          //声明了一个指针
    cout<<"输入一个数字"<<endl;
    cin>>command;          //我不太清楚这样输入行不行
    cout<<"您输入的是:"<<*command;
    return 0;
}

好久没写C++代码了,有错误请大家指出!

  • 写回答

1条回答 默认 最新

  • dudu200508 2019-04-04 20:38
    关注

    个人认为应该加&吧,建议你试一下

    粘一个同学的程序代码,你看看里面应该有XD

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<cstdio>
    using namespace std;
    struct Student{
        string id;
        string name;
        string sex;
        string adress;
        string telephone;
        double chinese;
        double math;
        double english;
        double average;
        double classmate;
        double teacher;
        double morality;
        double all;
        double all_order;
    };
    vector<Student*>students;
    
    inline void de(){
        while(cin.get()!='\n')continue;
    }
    
    int find1(string id){
        for(int i=0;i<students.size();i++){
            if(students[i]->id==id)
                return i;
        }
        return -1;
    };
    int find2(string name){
        for(int i=0;i<students.size();i++){
            if(students[i]->name==name)
                return i;
        }
        return -1;
    }
    void print(Student* st){
        cout<<"id:"<<st->id<<endl;
        cout<<"name:"<<st->name<<endl;
        cout<<"sex:"<<st->sex<<endl;
        cout<<"adress:"<<st->adress<<endl;
        cout<<"telephone:"<<st->telephone<<endl;
        cout<<"chinese score:"<<st->chinese<<endl;
        cout<<"math score:"<<st->math<<endl;
        cout<<"english score:"<<st->english<<endl;
        cout<<"score from clssmate:"<<st->classmate<<endl;
        cout<<"score form teacher:"<<st->teacher<<endl;
        cout<<"morality score:"<<st->morality<<endl;
        cout<<"comprehensive score:"<<st->all<<endl;
        cout<<"comprehensive rank:"<<st->all_order<<endl;
    }
    bool cmp1(Student *a,Student *b){
        return a->all>b->all;
    }
    void judge(double& a){
        double x;
        while(cin>>x){
            if(x>=0&&x<=100){
                a=x;
                break;
            }
            else
                printf("please enter a score between 0 and 100!\n");
        }
    }
    void a(){
        char order;
        while(1){
            printf("------------------------------------------------------\n");
            printf("input 'y' to insert student's information\n");
            printf("input 'q' to quit to menu\n");
            printf("------------------------------------------------------\n");
            cin>>order;
            de();
            if(order=='q')break;
            else if(order!='y'){
                printf("------------------------------------------------------\n");
                printf("please input a right order!\n");
                printf("------------------------------------------------------\n");
                continue;
            }
            printf("------------------------------------------------------\n");
            printf("please input students information in order\n");
            printf("------------------------------------------------------\n");
            Student *st=new Student;
            printf("id:\n");
            cin>>st->id;
            printf("name:\n");
            cin>>st->name;
            printf("sex:\n");
            cin>>st->sex;
            printf("adress:\n");
            cin>>st->adress;
            printf("telephone:\n");
            cin>>st->telephone;
            printf("chinese score:\n");
            judge(st->chinese);
            printf("math score:\n");
            judge(st->math);
            printf("english score:\n");
            judge(st->english);
            printf("score from clssmate:\n");
            judge(st->classmate);
            printf("score form teacher:\n");
            judge(st->teacher);
            printf("morality score:\n");
            judge(st->morality);
            st->average=(st->chinese+st->math+st->english)/3;
            st->all=st->average*0.6+st->classmate*0.1+st->teacher*0.2+st->morality*0.1;
            students.push_back(st);
        }
        sort(students.begin(),students.end(),cmp1);
        for(int i=0;i<students.size();i++){
            students[i]->all_order=i+1;
        }
    }
    void b(){
        char order;
        while(1){
            printf("------------------------------------------------------\n");
            printf("input 'y' to change or delete student's information\n");
            printf("input 'q' to quit to menu\n");
            printf("------------------------------------------------------\n");
            cin>>order;
            de();
            if(order=='q')
                break;
            else if(order!='y'){
                printf("------------------------------------------------------\n");
                printf("please input a right order!\n");
                printf("------------------------------------------------------\n");
                continue;
            }
            while(1){
                printf("------------------------------------------------------\n");
                printf("input '1' to search student by id\n");
                printf("input '2' to search student by name\n");
                printf("------------------------------------------------------\n");
                cin>>order;
                de();
                int i;
                Student *st;
                if(order=='1'){
                    printf("------------------------------------------------------\n");
                    printf("input the student's id:\n");
                    printf("------------------------------------------------------\n");
                    string s;
                    cin>>s;
                    de();
                    i=find1(s);
                }
                else if(order=='2'){
                    printf("------------------------------------------------------\n");
                    printf("input the student's name:\n");
                    printf("------------------------------------------------------\n");
                    string s;
                    cin>>s;
                    de();
                    i=find2(s);
                }
                else{
                    printf("------------------------------------------------------\n");
                    printf("please input the right order!\n");
                    printf("------------------------------------------------------\n");
                    continue;
                }
    
                if(i==-1){
                    printf("------------------------------------------------------\n");
                    printf("no such student's information!\n");
                    printf("------------------------------------------------------\n");
                }
                else{
                    st=students[i];
                    while(1){
                        printf("------------------------------------------------------\n");
                        printf("how do you want to do with the student's information?\n");
                        printf("input 'c' to change it's information\n");
                        printf("input 'd' to delete it's information\n");
                        printf("------------------------------------------------------\n");
                        cin>>order;
                        de();
                        if(order=='c'){
                            while(1){
                                printf("------------------------------------------------------\n");
                                printf("whitch information do you want to change?\n");
                                printf("please input the order\n");
                                printf("a for id,b for name,c for sex,d for adress\n");
                                printf("e for telephone,f for chinese score,g for math scroe\n");
                                printf("h for english score,i for score from classmate\n");
                                printf("j for score from teacher,k for morality score\n");
                                printf("------------------------------------------------------\n");
                                cin>>order;
                                de();
                                if(order>='a'&&order<='k'){
                                    printf("please input the new information\n");
                                    switch(order){
                                        case 'a':cin>>st->chinese;break;
                                        case 'b':cin>>st->name;break;
                                        case 'c':cin>>st->sex;break;
                                        case 'd':cin>>st->adress;break;
                                        case 'e':cin>>st->telephone;break;
                                        case 'f':judge(st->chinese);break;
                                        case 'g':judge(st->math);break;
                                        case 'h':judge(st->english);break;
                                        case 'i':judge(st->classmate);break;
                                        case 'j':judge(st->teacher);break;
                                        case 'k':judge(st->morality);break;
                                    }
                                    st->all=st->average*0.6+st->classmate*0.1+st->teacher*0.2+st->morality*0.1;;
                                }
                                else{
                                    printf("------------------------------------------------------\n");
                                    printf("please input the right order!\n");
                                    printf("------------------------------------------------------\n");
                                    continue;
                                }
                                break;
                            }
                        }
                        else if(order=='d'){
                            students.erase(students.begin()+i);
                            delete st;
                        }
                        else{
                            printf("------------------------------------------------------\n");
                            printf("please input the right order!\n");
                            printf("------------------------------------------------------\n");
                            continue;
                        }
                        break;
                    }
                }
                break;
            }
        }
        sort(students.begin(),students.end(),cmp1);
        for(int i=0;i<students.size();i++){
            students[i]->all_order=i+1;
        }
    }
    void c(){
        if(students.size()==0){
            printf("------------------------------------------------------\n");
            printf("there's no student's information at all!\n");
            printf("------------------------------------------------------\n");
            return ;
        }
        for(Student* st:students){
            printf("------------------------------------------------------\n");
            print(st);
            printf("------------------------------------------------------\n");
        }
    }
    void d(){
        char order;
        while(1){
            printf("------------------------------------------------------\n");
            printf("input 'y' to search student's information\n");
            printf("input 'q' to quit to menu\n");
            printf("------------------------------------------------------\n");
            cin>>order;
            de();
            if(order=='q')
                break;
            else if(order!='y'){
                printf("------------------------------------------------------\n");
                printf("please input a right order!\n");
                printf("------------------------------------------------------\n");
                continue;
            }
            else{
                while(1){
                    printf("------------------------------------------------------\n");
                    printf("input '1' to search student's information by id\n");
                    printf("input '2' to search student's information by name\n");
                    printf("input 'q' to return upper layer\n");
                    printf("------------------------------------------------------\n");
                    cin>>order;
                    de();
                    if(order=='q')break;
                    else if(order=='1'){
                        printf("------------------------------------------------------\n");
                        printf("input the student's id:\n");
                        printf("------------------------------------------------------\n");
                        string s;
                        cin>>s;
                        de();
                        int i=find1(s);
                        if(i==-1){
                            printf("------------------------------------------------------\n");
                            printf("no such student's information!\n");
                            printf("------------------------------------------------------\n");
                        }
                        else{
                            printf("------------------------------------------------------\n");
                            print(students[i]);
                            printf("------------------------------------------------------\n");
                        }
                    }
                    else if(order=='2'){
                        printf("------------------------------------------------------\n");
                        printf("input the student's name:\n");
                        printf("------------------------------------------------------\n");
                        string s;
                        cin>>s;
                        de();
                        int i=find2(s);
                        if(i==-1){
                            printf("------------------------------------------------------\n");
                            printf("no such student's information!\n");
                            printf("------------------------------------------------------\n");
                        }
                        else{
                            printf("------------------------------------------------------\n");
                            print(students[i]);
                            printf("------------------------------------------------------\n");
                        }
                    }
                    else{
                        printf("------------------------------------------------------\n");
                        printf("please input the right order!\n");
                        printf("------------------------------------------------------\n");
                    }
                }
            }
        }
    }
    void q(){
        Student *tmp;
        for(long i=(long)students.size()-1;i>=0;i--){
            tmp=students[i];
            students.erase(students.begin()+i);
            delete tmp;
        }
    }
    int main(){
        char order;
        do{
            printf("*****************************************\n");
            printf("*                                       *\n");
            printf("*                 menu                  *\n");
            printf("*               请输入指令                *\n");
            printf("*             a:插入学生信息              *\n");
            printf("*             b:修改学生信息              *\n");
            printf("*             c:浏览学生信息              *\n");
            printf("*             d:查询学生信息              *\n");
            printf("*             q:退出程序                 *\n");
            printf("*                                       *\n");
            printf("*****************************************\n");
            cin>>order;
            de();
            switch(order){
                case 'a':a();break;
                case 'b':b();break;
                case 'c':c();break;
                case 'd':d();break;
                case 'q':
                    q();
                    printf("------------------------------------------------------\n");
                    printf("thank you for using my system!\n");
                    printf("------------------------------------------------------\n");
                    break;
            }
        }while(order!='q');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮