Thread: Help me!!Please!!!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    8

    Help me!!Please!!!

    Please help me how to convert this C code to C++ with Class:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <mem.h>
    #include <string.h>
    #include <conio.h>
    
    #define MAX 100
    
    struct congnhan {
      char hoten[35];
      } danhsach[MAX];
    int n = 0;
    
    void nhapmoi()
    {
      char hoten[35], tmp[3];
      int i;
      do {
        printf("\nNhap ho ten CN : ");
        gets(hoten);
        if (strlen(hoten))
        {
          strcpy(danhsach[n].hoten,hoten);
          gets(tmp);
          n++;
        }
      } while (strlen(hoten));
    }
    
    void timkiem()
    {
      char hoten[5];
      int i = 0, found = 0;
      printf("\nCho ho ten CN can tim : ");
      gets(hoten);
      if (strlen(hoten))
        while (i<n)
          if (strcmp(danhsach[i].hoten,hoten) == 0)
          {
             printf("\nHo va ten : %s", danhsach[i].hoten);
             found = 1;
             break;
          }
          else
            i++;
      if (!found)
        printf("\nKhong tim thay!!!");
    }
    
    
    void menu()
    {
      printf("\n**QUAN LY CONG NHAN**");
      printf("\n* [1]. Them     *");
      printf("\n* [2]. Tim kiem *");
      printf("\n* [3]. Thoat    *");
      printf("\n***************");
      printf("\n Chon lua ? ");
    }
    
    void main()
    {
      char traloi;
      do {
        menu();
        do {
          traloi = getch();
        } while (traloi < '1' || traloi > '2');
        putc(traloi, stdout);
        switch (traloi)
        {
           case '1' : nhapmoi();
                      break;
           case '2' : timkiem();
                      break;
        }
      } while (traloi != '3');
    }
    It have got some errors,please help me to check it.Thanks.I'm newbie
    Last edited by KySiRo; 11-05-2006 at 01:30 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include <mem.h>
    > #include <conio.h>
    There is no need for non-standard header files in such a simple program.

    > gets(hoten);
    The world's most dangerous function.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    > void main()
    main always returns an int
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    What compiler are you using? If it's some ancient borland "turbo" variety or microsoft visual C++ 5 or 6, then it's not going to be up to dealing with modern C++.

    Here's an example of a struct and a class doing pretty much the same thing.
    Code:
    #include <iostream>
    using namespace std;
    
    struct foo {
        int a;
    };
    class bar {
        public:
            void reader();
        private:
            int a;
    };
    
    void bar::reader ( ) {
        cin >> a;
    }
    
    void foo_reader ( foo *instance ) {
        cin >> instance->a;
    }
    
    int main ( ) {
        foo var1;
        bar var2;
        foo_reader( &var1 );
        var2.reader();
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Sure.
    Code:
    #include <iostream>
    #define MAX 100
    
    struct congnhan {
        char hoten[35];
    };
    
    class ChineseStuff{
        public:
            ChineseStuff(){
                n=0;
            }
    
            void nhapmoi(){
                char hoten[35], tmp[3];
                int i;
                do{
                    printf("\nNhap ho ten CN : ");
                    std::cin.getline(hoten,5,'\n');
                    if (strlen(hoten)){
                        strcpy(danhsach[n].hoten,hoten);
                        gets(tmp);
                        n++;
                    }
                } while (strlen(hoten));
            }
    
            void timkiem(){
                char hoten[5];
                int i = 0, found = 0;
                printf("\nCho ho ten CN can tim : ");
                std::cin.getline(hoten,5,'\n');
                if (strlen(hoten))
                while (i<n)
                if (strcmp(danhsach[i].hoten,hoten) == 0)
                {
                    printf("\nHo va ten : %s", danhsach[i].hoten);
                    found = 1;
                    break;
                }
                else
                i++;
                if (!found)
                printf("\nKhong tim thay!!!");
            }
    
            void menu(){
                printf("\n**QUAN LY CONG NHAN**");
                printf("\n* [1]. Them     *");
                printf("\n* [2]. Tim kiem *");
                printf("\n* [3]. Thoat    *");
                printf("\n***************");
                printf("\n Chon lua ? ");
            }
        private:
            int n;
            congnhan danhsach[MAX];
    };
    
    int main(void){
        ChineseStuff stuff;
        char traloi;
        do{
            stuff.menu();
            do{
                traloi = std::cin.get();
            } while (traloi < '1' || traloi > '2');
            std::cin.ignore();
            std::cout<<traloi;
            switch (traloi){
                case '1' : stuff.nhapmoi();
                    break;
                case '2' : stuff.timkiem();
                    break;
            }
        } while (traloi != '3');
        return 0;
    }
    Like this?
    Last edited by maxorator; 11-05-2006 at 02:02 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    Thanks.Please help me write a programme simple C++ (use Class) to manage Student:registration,search student by name.I need it in a hurry,please.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Do you mean every student has one class object?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    yes,bro.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I need it in a hurry,please.
    You mean it's a last second "hail mary" pass into the end zone to save your ass from being roasted, rather than actually wanting to learn how to do it.

    Assuming you're still on the course after this assignment, come back when you have days to spare, not hours.

    We've posted examples, now try and use that for your own code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    He's probably got roasted by now.
    Code:
    #include <iostream>
    #include <vector>
    #define MAX 100
    
    class Student{
        public:
            Student(){
                studage=0;
                studid=0;
            }
            int GetStudentId(){
                return studid;
            }
            int GetStudentAge(){
                return studage;
            }
            char* GetStudentName(char* dest,int length){
                if(name[0]!='\0'){
                    strncpy(dest,name,length);
                }
                return dest;
            }
            bool SetData(int id,char* student,int age){
                if(student[0]!='\0'){
                    strncpy(name,student,sizeof(name));
                    studid=id;
                    studage=age;
                    return true;
                }
                return false;
            }
        private:
            char name[256];
            int studid,studage;
    };
    
    int main(void){
        int id=1;
        char buf[256],buf2[256];
        char option;
        std::vector<Student> Students;
        std::vector<Student>::iterator it;
        Student temp;
        do{
            std::cout<<"\n1. View students.\n2. Register new students\n3. Search students\n4. Exit this program\n--> ";
            option=std::cin.get();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            switch(option){
                case '1':
                    if(!Students.empty()){
                        it = Students.begin();
                        for(it=Students.begin();it!=Students.end();it++){
                            printf("Id: %i -- Name: %s -- Age: %i\n",it->GetStudentId(),
                                it->GetStudentName(buf,256),it->GetStudentAge());
                        }
                    }
                    else{
                        printf("There are no students.\n");
                    }
                    printf("Press enter to continue.");
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                    break;
                case '2':
                    printf("Enter student's name:");
                    std::cin.getline(buf,256,'\n');
                    printf("Enter student's age:");
                    std::cin.getline(buf2,10,'\n');
                    temp.SetData(id,buf,strtol(buf2,NULL,10));
                    Students.push_back(temp);
                    id++;
                    printf("Press enter to continue.");
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                    break;
                case '3':
                    printf("Search by 1)id 2)name 3)age : ");
                    option=std::cin.get();
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                    printf("Search: ");
                    std::cin.getline(buf,256,'\n');
                    switch(option){
                        case '1':
                            if(!Students.empty()){
                                it = Students.begin();
                                for(it=Students.begin();it!=Students.end();it++){
                                    if(it->GetStudentId()==strtol(buf,NULL,10)){
                                        printf("Id: %i -- Name: %s -- Age: %i\n",it->GetStudentId(),
                                            it->GetStudentName(buf2,256),it->GetStudentAge());
                                        break;
                                    }
                                }
                            }
                            else{
                                printf("There are no students.\n");
                            }
                            break;
                        case '3':
                            if(!Students.empty()){
                                it = Students.begin();
                                for(it=Students.begin();it!=Students.end();it++){
                                    if(it->GetStudentAge()==strtol(buf,NULL,10)){
                                        printf("Id: %i -- Name: %s -- Age: %i\n",it->GetStudentId(),
                                            it->GetStudentName(buf2,256),it->GetStudentAge());
                                    }
                                }
                            }
                            break;
                        default:
                            if(!Students.empty()){
                                it = Students.begin();
                                for(it=Students.begin();it!=Students.end();it++){
                                    if(strstr(it->GetStudentName(buf2,256),buf)!=NULL){
                                        printf("Id: %i -- Name: %s -- Age: %i\n",it->GetStudentId(),buf2,it->GetStudentAge());
                                    }
                                }
                            }
                            else{
                                printf("There are no students.\n");
                            }
                            break;
                    }
                    printf("Press enter to continue.");
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                    break;
                case '4':
                    return 0;
                    break;
                default:
                    printf("This is not a valid option.\n");
            }
        } while(true);
        return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    Hic,i can't run it in BC++ 3.1

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Moxorator, is he paying you to do his programs for him?

    @KySiro, BC++ 3.1 is a rather acheint compiler. You should get a more updated one. There are several free ones to download, such as code::blocks and devC++. If you wanna go with MS then Msvc++2005 Express is free to download form their website.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    I've been studying a lot at this Forum.Because i'm just a newbie,so i need a lot of help.
    Thank maxorator very much.
    @swgh:thank u too.I don't pay Moxorator any thing.I just need his help.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    I've a question: In code Moxorator writed,what is "std"?when i run program,compiler warn:"Type qualifier "std" must be a struct or class name"?

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    std is a type qualifier. Here is an example:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    You prefix the name of the object, eg: cout to the using qualifier and it avoids you having to type std:: before every iostream object name. You can just type cout not std::cout

  14. #14
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Or you could just type
    Code:
    using namespace std;
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I've a question: In code Moxorator writed,what is "std"?when i run program,compiler warn:"Type qualifier "std" must be a struct or class name"?
    Now that's a problem with ancient compilers.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed