Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ string assignment to struct

C++ string assignment to struct

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasedata-structureshelp
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fotsing
    wrote on last edited by
    #1

    Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code

    bool delet(){
      Int f=0;
      Cout<<"enter name:";
       char del\[20\];
       Con.getline(del,20);
       for(int i=0;  i<=index; ++i){
         If(phone\[i\].name==del){
            for(int j=i; j<=index; ++j){
            Phone\[j\].num=Phone\[j+1\].num;
            Phone\[j\].TphoneN=phone\[j+1\].TphoneN;
            Phone\[j\].name=Phone\[j+1\].name;
            }
          f=1;
          }
    

    }
    }

    <\pre>

    L V CPalliniC 3 Replies Last reply
    0
    • F Fotsing

      Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code

      bool delet(){
        Int f=0;
        Cout<<"enter name:";
         char del\[20\];
         Con.getline(del,20);
         for(int i=0;  i<=index; ++i){
           If(phone\[i\].name==del){
              for(int j=i; j<=index; ++j){
              Phone\[j\].num=Phone\[j+1\].num;
              Phone\[j\].TphoneN=phone\[j+1\].TphoneN;
              Phone\[j\].name=Phone\[j+1\].name;
              }
            f=1;
            }
      

      }
      }

      <\pre>

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You cannot use the == operator to compare arrays. You need to use strcmp or similar. You

      F 1 Reply Last reply
      0
      • F Fotsing

        Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code

        bool delet(){
          Int f=0;
          Cout<<"enter name:";
           char del\[20\];
           Con.getline(del,20);
           for(int i=0;  i<=index; ++i){
             If(phone\[i\].name==del){
                for(int j=i; j<=index; ++j){
                Phone\[j\].num=Phone\[j+1\].num;
                Phone\[j\].TphoneN=phone\[j+1\].TphoneN;
                Phone\[j\].name=Phone\[j+1\].name;
                }
              f=1;
              }
        

        }
        }

        <\pre>

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        It is a pseudo-code? Could you instead post a compilable and properly formatted code snippet? Or, at least, explain what errors or other problems you have with this code?

        F 1 Reply Last reply
        0
        • F Fotsing

          Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code

          bool delet(){
            Int f=0;
            Cout<<"enter name:";
             char del\[20\];
             Con.getline(del,20);
             for(int i=0;  i<=index; ++i){
               If(phone\[i\].name==del){
                  for(int j=i; j<=index; ++j){
                  Phone\[j\].num=Phone\[j+1\].num;
                  Phone\[j\].TphoneN=phone\[j+1\].TphoneN;
                  Phone\[j\].name=Phone\[j+1\].name;
                  }
                f=1;
                }
          

          }
          }

          <\pre>

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          If you are using C++ then a std::string is better option than an array of characters.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • V Victor Nijegorodov

            It is a pseudo-code? Could you instead post a compilable and properly formatted code snippet? Or, at least, explain what errors or other problems you have with this code?

            F Offline
            F Offline
            Fotsing
            wrote on last edited by
            #5

            no it is not a pseudo code am trying to write a program that can register a contact, delete or update the contact. so when i compile the is an error message "invalid array assignment" within the the delete function which is the code i posted before. the complete code is

            #include
            #include
            #include
            #include

            using namespace std;
            enum type{ home,work,fax,mobile,other };
            struct contact{
            char num[15];
            char name[20];
            char TphoneN[6];
            }phone[5];
            const int max=5;
            int index=0;

            //reading function
            bool reading(){
            char v;
            if(index<=5){
            cout<<"please enter phone number: ";
            gets(phone[index].num);

            	cout<<"please enter the name: ";
            	gets(phone\[index\].name);
            
            	cout<<"the type of number( fax,  mobile,  home,  work,  other): ";
            	gets(phone\[index\].TphoneN);
            	/\*cin>>v;
            	switch(v){
            		case 'f': phone\[index\].TphoneN='fax';
            			break;
            		case 'm': phone\[index\].TphoneN='mobile';
            			break;
            		case 'h': phone\[index\].TphoneN='home';
            			break;
            		case 'w': phone\[index\].TphoneN='work';
            			break;
            		case 'o': phone\[index\].TphoneN='other';
            			break;
            		default: cout<<"please enter a valid key: ";
            	}\*/
            	index++;
            	
            	return true;
            }
            else 
            	return false;
            		
            }
            

            //deleting function
            bool delet(){
            int flag=0;
            cout<<"please enter the name of the person u wanna delete: ";
            char del[20];
            cin.getline(del,20);
            for(int i=0; i<=index; ++i){
            if(phone[i].name==del){
            for(int j=i; j<=index; ++j){

            			phone\[j\].num=phone\[j+1\].num;
            			phone\[j\].TphoneN=phone\[j+1\].TphoneN;
            			phone\[j\].name=phone\[j+1\].name;
            	 }
            	 flag=1;
            	}
            

            }

            if(!flag){
            	return false;
            }
            else{
            		index--;
            	return true;
            }
            

            }

            //function displqying all contacts
            void all(){
            for(int i=0; i<=index; ++i){
            cout<>x;
            switch(x){
            case 1:
            bool y=reading();
            if(y==true){
            cout<<"contact successfuly saved";
            }
            else
            cout<<"an error accure could not save your contact";
            break;
            case 2:
            bool y=delet();
            if(y==true){
            cout<<"contact successfuly deleted";
            }
            else
            cout<<"sorry your name does not exist";
            break;
            case 3:
            all();
            break;
            case 4:
            cout<<"still sea

            1 Reply Last reply
            0
            • L Lost User

              You cannot use the == operator to compare arrays. You need to use strcmp or similar. You

              F Offline
              F Offline
              Fotsing
              wrote on last edited by
              #6

              okay i take note thanks. i still have an error message "invalid array assignment"

              S 1 Reply Last reply
              0
              • F Fotsing

                okay i take note thanks. i still have an error message "invalid array assignment"

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                That is because you are assigning arrays to arrays, and that is invalid code in C/C++:

                phone[j].num=phone[j+1].num;
                phone[j].TphoneN=phone[j+1].TphoneN;
                phone[j].name=phone[j+1].name;

                You can use the appropriate functions for C strings, e. g. strcpy. But it would be much better if you just used std::string instead of C strings!

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                F 1 Reply Last reply
                0
                • S Stefan_Lang

                  That is because you are assigning arrays to arrays, and that is invalid code in C/C++:

                  phone[j].num=phone[j+1].num;
                  phone[j].TphoneN=phone[j+1].TphoneN;
                  phone[j].name=phone[j+1].name;

                  You can use the appropriate functions for C strings, e. g. strcpy. But it would be much better if you just used std::string instead of C strings!

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  F Offline
                  F Offline
                  Fotsing
                  wrote on last edited by
                  #8

                  Thanks

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups