Thread: problem using concatenation of arrays[*new]

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    158

    Post problem using concatenation of arrays[*new]

    @Elysia
    Code:
    // crcvectors.cpp : Defines the entry point for the console application.
    //
    
    
    #include "stdafx.h"
    #include<iostream>
    #include<conio.h>
    #include<cctype>
    #include<vector>
    using namespace std;
    
    
    int main()
    {
           int n,lim_fcs,lim_pattern,tot_size,len;
           cout<<"\nEnter the length of message\n";
           cin>>n;
           vector<int>message(n);
        
        
           vector<int>::iterator p; 
        
    
    
           cout<<"\nEnter the message\n";
    
    
           for(p=message.begin();p!=message.end();p++)
                 {
        
                     cin>>*p;
                 }
    
    
            cout<<"\nThe message is:\n";
            for(int i=0;i<message.size();i++)
    
    
                {
        
                    cout<<message.at(i);
                    cout<" ";
                }
        //storing here to retrieve it later for xor
             vector<int>message1(n);
    
    
           for(int i=0;i<message.size();i++)
                {
                    message1.at(i)=message.at(i);
                }
          cout<<endl;
          cout<<"Enter the Limit for fcs(frame check sequence)"<<endl;
          cin>>lim_fcs;
          cout<<endl;
          cout<<"Enter the size for Pattern vector"<<endl;
          cin>>lim_pattern;
          vector<int>pattern(lim_pattern);
          vector<int>::iterator q;
          cout<<"Enter the Pattern"<<endl;
          for(q=pattern.begin();q!=pattern.end();q++)
               {
                  cin>>*q;
    
    
               }
    
    
          cout<<"The pattern you entered is:"<<endl;
    
    
           for(int j=0;j<pattern.size();j++)
               {
                   cout<<pattern.at(j);
                   cout<<" ";
        
               }
          cout<<endl;
          tot_size=n+lim_fcs;
    
    
          len=tot_size-n;
          vector<int>a(len,(int)0);
        //for(int j=0;j<len;j++)
        //{
            //cout<<a.at(j);
            //cout<<" ";
        //}
          for(int j=0;j<len;j++)
             {
                message.push_back(a[j]);
             }
    
    
        cout<<"The new Message is :"<<endl;
        for(int k=0;k<tot_size;k++)
             {
                 cout<<message.at(k) ;
                 cout<<" ";
             }
        cout<<endl;
        vector<int>result(tot_size);
        for(int k=0;k<len;k++)
        
                 result.at(k)=message.at(k)^pattern.at(k);
    
    
        cout<<"result is:"<<endl;
        for(int k=0;k<len;k++)
        
                 cout<<result.at(k)<<"\t";
        cout<<endl;
        for(int k=0;k<len;k++)
                 message1.push_back(result.at(k));
        cout<<"The message to send(at sender side is)"<<endl;// the message ready to be sent 
        for(int index=0;index<tot_size;index++)
             {
                   cout<<message1.at(index);
                  cout<<" ";
             }
        cout<<endl;
        
        getch();
            return 0;
    }

    Doors are always open for improvement....

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Doors are always open for improvement....
    true, Vectors ar copyable

    this
    Code:
             vector<int>message1(n);
     
     
           for(int i=0;i<message.size();i++)
                {
                    message1.at(i)=message.at(i);
                }
    could be

    Code:
    vector<int>message1 = message;
    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by ZuK View Post
    true, Vectors ar copyable

    this
    Code:
             vector<int>message1(n);
     
     
           for(int i=0;i<message.size();i++)
                {
                    message1.at(i)=message.at(i);
                }
    could be

    Code:
    vector<int>message1 = message;
    Kurt
    Yes ...Thanks

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    But One thing more...Why the IDE returns an error time and again(but sometimes ignore it) in the followings(errors commented)
    Code:
    
    	 vector<string> vectorOne, vectorTwo;  
    
    
    
    
        vectorOne.push_back("I'm first")
    
    
        vectorTwo.push_back("I'm second");
    
    
         
    
    
        //  display contents of vectorOne
    
    
        cout << "vectorOne: ";
    
    
        for (vector<string>::iterator index=vectorOne.begin(); index!=vectorOne.end(); ++index) {
    
    
            cout << *index; /*here and also in the same subsequent scenarios the compiler returns an error:
    
    error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
    1>          with
    1>          [
    1>              _Elem=char,
    1>              _Traits=std::char_traits<char>,
    1>              _Ax=std::allocator<char>
    
    
    */
        }
    
    
        cout << endl;
    
    
         
    
    
        //  display contents of vectorTwo
    
    
        cout << "vectorTwo: ";
        for (vector<string>::iterator index=vectorTwo.begin(); index!=vectorTwo.end(); ++index) {
    
    
            cout << *index;
        }
    
    
        cout << endl << endl;
    
    
        //  swap vector contents
    
    
        cout << "Using swap() to exchange contents of vectorOne and vectorTwo." << endl << endl;
    
    
        vectorOne.swap(vectorTwo);
    
    
         
    
    
        //  display contents of vectorOne
    
    
            cout << "vectorOne: ";
    
    
            for (vector<string>::iterator index=vectorOne.begin(); index!=vectorOne.end(); ++index) {
    
    
                cout << *index;
    
    
            }
            cout << endl;
    
    
             
    
    
            //  display contents of vectorTwo
    
    
            cout << "vectorTwo: ";
    
    
            for (vector<string>::iterator index=vectorTwo.begin(); index!=vectorTwo.end(); ++index) {
    
    
                cout << *index;
    
    
            }
    
    
            cout << endl << endl;

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Quote Originally Posted by ZuK View Post
    true, Vectors ar copyable

    this
    Code:
             vector<int>message1(n);
     
     
           for(int i=0;i<message.size();i++)
                {
                    message1.at(i)=message.at(i);
                }
    could be

    Code:
    vector<int>message1 = message;
    Kurt
    But it probably really should be:
    Code:
    vector<int>message1( message);
    Using the constructor instead of the assignment operator will avoid the possibility of an unnecessary operation of calling the constructor followed by the assignment operator.


    But One thing more...Why the IDE returns an error time and again(but sometimes ignore it) in the followings(errors commented)
    Post a complete small program that illustrates the problem. Are you sure you included all the required header files?

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jimblumberg View Post
    Using the constructor instead of the assignment operator will avoid the possibility of an unnecessary operation of calling the constructor followed by the assignment operator.

    Jim
    Code:
    vector<int> message1(message);
    is exactly equivalent to

    Code:
    vector<int> message1 = message;
    because the compiler invokes a constructor - in this case, the copy constructor - when you use the assignment operator with a declaration.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    ...is exactly equivalent to...
    No, they are not exactly equivalent (the assignment in not considered an explicit context, so if you have a constructor marked as explicit, it will create a compile error).
    But you are right in that it invokes the copy constructor instead of creating a temporary and then the assignment operator.

    But One thing more...Why the IDE returns an error time and again(but sometimes ignore it) in the followings(errors commented)
    A guess, but...
    Did you remember to include <string>? If not, you might get that error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by jimblumberg View Post




    Post a complete small program that illustrates the problem. Are you sure you included all the required header files?

    Jim
    Th code is just above this serial post i.e post#4

  9. #9
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by Elysia View Post


    A guess, but...
    Did you remember to include <string>? If not, you might get that error.
    NO..it didn't work...Still the SAME!!!

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    This code snippet works. Has your class taught you anything about how to calculate CRC's yet?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> vectorOne, vectorTwo;  
        vectorOne.push_back("I'm first");
        vectorTwo.push_back("I'm second");
        cout << "vectorOne: ";
        // note there's only one string in vectorOne, so why have this loop?
        for (vector<string>::iterator index=vectorOne.begin(); index!=vectorOne.end(); ++index)
            cout << *index;
        cout << endl;
        return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Post the smallest and simplest program that you expect should compile but which results in the "Still the SAME!!!" error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by rcgldr View Post
    This code snippet works.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> vectorOne, vectorTwo;  
        vectorOne.push_back("I'm first");
        vectorTwo.push_back("I'm second");
        cout << "vectorOne: ";
        // note there's only one string in vectorOne, so why have this loop?
        for (vector<string>::iterator index=vectorOne.begin(); index!=vectorOne.end(); ++index)
            cout << *index;
        cout << endl;
        return 0;
    }
    But with me it returns an error on line 14...

    Quote Originally Posted by rcgldr View Post
    Has your class taught you anything about how to calculate CRC's yet?
    I have done it

  13. #13
    Registered User
    Join Date
    Jul 2013
    Posts
    158
    Quote Originally Posted by laserlight View Post
    Post the smallest and simplest program that you expect should compile but which results in the "Still the SAME!!!" error.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<string> vectorOne, vectorTwo;  
        vectorOne.push_back("I'm first");
        vectorTwo.push_back("I'm second");
        cout << "vectorOne: ";
        // note there's only one string in vectorOne, so why have this loop?
        for (vector<string>::iterator index=vectorOne.begin(); index!=vectorOne.end(); ++index)
            cout << *index;  error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
    1>          with
    1>          [
    1>              _Elem=char,
    1>              _Traits=std::char_traits<char>,
    1>              _Ax=std::allocator<char>
    ]
        cout << endl;
        return 0;
    }
    


    Error commented against line

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    No, they are not exactly equivalent (the assignment in not considered an explicit context, so if you have a constructor marked as explicit, it will create a compile error).
    Elkvis' observation was concerned with copying a std::vector<int>, and was correct, since std::vector's copy constructor is not "marked as explicit".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by jeedi khan View Post
    Error commented against line
    What compiler are you using? What operating system are you using?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in concatenation of arrays
    By jeedi khan in forum C++ Programming
    Replies: 68
    Last Post: 08-22-2013, 08:56 AM
  2. concatenation
    By valthyx in forum C Programming
    Replies: 5
    Last Post: 01-17-2010, 08:22 AM
  3. K&R problem !! String concatenation :(
    By karanmitra in forum C Programming
    Replies: 9
    Last Post: 08-18-2005, 05:20 AM
  4. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  5. Concatenation ?
    By koolguysj in forum C Programming
    Replies: 8
    Last Post: 04-15-2005, 04:26 PM