943,691 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1228
  • C++ RSS
Jun 20th, 2008
0

Function to make a variable equal to other variables

Expand Post »
very simple question: what is the most easiest way to do the following

I have two variables

a=1;
b=2;

how do I make it that

c= a,b

so that c=12

I dont want to write
C++ Syntax (Toggle Plain Text)
  1. c=12;
because a and b change according to user input.

Thanks, this should be solved fast I dont really get what I should "search" for otherwise I would search instead of asking. (also the title is probably pretty bad but not sure what to call this )


Thanks
Last edited by salman213; Jun 20th, 2008 at 7:45 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 20th, 2008
0

Re: Function to make a variable equal to other variables

int c = (a * 10) + b;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jun 20th, 2008
0

Re: Function to make a variable equal to other variables

Hmm I guess that is the way to do it if you want to go mathematically .. I should have thought of that... so dumb..lol

Thanks Ancient


I will use that but just to check if anyone knows of another way to do it please do post.
Last edited by salman213; Jun 20th, 2008 at 8:17 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 20th, 2008
0

Re: Function to make a variable equal to other variables

What I posted is also the method used to convert a string of digits to an integer if you don't want to use a standard C or C++ convertion function.

Here's aother way:
int c = (a << 3) + (a << 1) + b;
Last edited by Ancient Dragon; Jun 20th, 2008 at 8:25 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jun 20th, 2008
0

Re: Function to make a variable equal to other variables

What I posted is also the method used to convert a string of digits to an integer if you don't want to use a standard C or C++ convertion function.

Here's aother way:
int c = (a << 3) + (a << 1) + b;
Edit: Wrong on my part, sorry.

Edit2: Actually... I think I got it.

a << 3 is the same as a * (2 ^ 3)
a >> 3 is the same as a / (2 ^ 3)

Correct me if I'm wrong on this.
Last edited by Alex Edwards; Jun 20th, 2008 at 8:58 pm.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 20th, 2008
0

Re: Function to make a variable equal to other variables

Edit: Wrong on my part, sorry.

Edit2: Actually... I think I got it.

a << 3 is the same as a * (2 ^ 3)
a >> 3 is the same as a / (2 ^ 3)

Correct me if I'm wrong on this.
Point made: it's obfuscatory and should only be used in the archaic systems, from whence it came, that lack the appropriate machine commands and/or a supporting assembly code library.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 20th, 2008
0

Re: Function to make a variable equal to other variables

which way is "obfuscatory?"

Is there a specific command that has not been mentioned yet which is now available that will do the same thing?


Not that I have any problem with the way described
Last edited by salman213; Jun 20th, 2008 at 10:57 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 20th, 2008
1

Re: Function to make a variable equal to other variables

Click to Expand / Collapse  Quote originally posted by salman213 ...
which way is "obfuscatory?"
Well, you are given two ways of doing the same thing:
C++ Syntax (Toggle Plain Text)
  1. int c = (a * 10) + b;
  2. int c = (a << 3) + (a << 1) + b;

One of them is straight-forward, one is not. The one that is not is "obfuscatory" (i.e. more complicated and harder to understand for no reason other than to add confusion).

Click to Expand / Collapse  Quote originally posted by salman213 ...
Is there a specific command that has not been mentioned yet which is now available that will do the same thing?
You're missing the point. When AD posted this:

C++ Syntax (Toggle Plain Text)
  1. int c = (a << 3) + (a << 1) + b;

his point was along the lines of "I've given you a perfectly good solution, yet you ask for another one with no explanation of why the first solution doesn't meet your needs." Then he gave you a solution that does the exact same thing as the first solution, but in an overly-complicated way that no one would ever use instead of the first solution. Why don't you like this?

C++ Syntax (Toggle Plain Text)
  1. int c = (a * 10) + b;

I can't think of a command that will do this any better.

Click to Expand / Collapse  Quote originally posted by salman213 ...
Not that I have any problem with the way described
Why are you asking for a different way then? I'm sure we could come up with even more confusing mathematical formulas that do the exact same thing, but that's probably not what you are looking for.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 21st, 2008
1

Re: Function to make a variable equal to other variables

Point made: it's obfuscatory and should only be used in the archaic systems, from whence it came, that lack the appropriate machine commands and/or a supporting assembly code library.
I laughed so hard when I read this...

winner
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to read data from textfile and push in array
Next Thread in C++ Forum Timeline: inheritance with input command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC