By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

sum two numbers using ONE variable.

P: 2
hi everyone,

can anyone tel me how to make a code in c/c++ which inputs two numbers from the user and add them by using ONE and only ONE variable in the entire program.
Mar 13 '07 #1
Share this Question
Share on Google+
5 Replies

P: 39
you could probably do it with strings or char* otherwise some ambiguous old school C/assembly way i'm sure. Would you consider using registers as a variable?
Mar 13 '07 #2

Expert Mod 2.5K+
P: 4,677
I would think another option would be to overload the '>>' operator...
Mar 13 '07 #3

P: 39
what my overly-complicated suggestion not good enough?
Mar 13 '07 #4

Expert Mod 2.5K+
P: 4,677
what my overly-complicated suggestion not good enough?
Haha, of course it was good, I was merely offering another method. Personally I find pointers easier to use than overloading and re-including header files, or however you choose to implement, but the concept might be easier.

I'm actually just threatened by your posts - wanted to try to solidify my 'Moderator' status by padding the post count (not that I'm doing that here either....ooooh! almost at 800!)
Mar 13 '07 #5

P: 1,806
Since you know that there are two numbers....(and given you only use 1 var I assume you're not looking for robust code, and don't have to check validity of input etc (in my favourite pseudicide ):

Expand|Select|Wrap|Line Numbers
  2. int a=0;
  4. a = a + getch();
  5. a = a+ getch();
Now pray that the user only enters numbers less than 255.....Or you can probably find a better function to read inputs......
Mar 13 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.