Variable merging

This is a discussion on Variable merging within the C++ Programming forums, part of the General Programming Boards category; OK, now I have rather stupid question, but i really need answer... I have 2 variables- Code: int x=4; int ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    Variable merging

    OK, now I have rather stupid question, but i really need answer...

    I have 2 variables-
    Code:
     int x=4;
     int y=3;
    Now, how do I make them sit together in one variable, like int xy=43; ?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    int xy = x * 10 + y;
    ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19
    use pointers;

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a = 56; // a equals 56
        int *b = &a; // b is an int pointer, pointing to address of a
        
        *b = 23; // set value of address pointed by b to 23
        
        cout << "a = " << a << endl << "b = " << *b << endl; // dump both vars to screen
    
        return 0;
    }
    Code:
    outcome:
      a = 23
      b = 23

  4. #4
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Here's one way:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        int x = 4;
        int y = 3;
    
        char buff1[200];
        
        std::string temp1 = itoa(x, buff1, 10);
        std::string temp2 = itoa(y, buff1, 10);
        std::string result = temp1 + temp2;
        
        int xy = atoi(result.c_str());
        
        std::cout<<"x is: "<<x<<", y is: "<<y<<", xy is: "<<xy<<std::endl;
     
        std::cin.ignore();
        std::cin.get();   
        
    }
    Last edited by Loctan; 08-04-2006 at 03:41 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Might as well use a stringstream instead of atoi and the non-standard itoa.

  6. #6
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Quote Originally Posted by Daved
    Might as well use a stringstream instead of atoi and the non-standard itoa.
    I don't know anything about stringstreams so I will go and read up on them. I didn't even realize that itoa was non standard. Thanks!

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    cppreference is always good

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21