Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-28-2006, 05:56 PM
cooldude cooldude is offline
Newbie
 
Join Date: Nov 2006
Posts: 1
Credits: 0
Rep Power: 0
cooldude is on a distinguished road
Default Recursion Problem

ok, heres my code

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void convert (int num1, int base1)
{
int remainder;

remainder = num1 % base1;

if (num1 / base1 == 0)
{
if (remainder <= 9)
cout << remainder;

else if (remainder > 10)
{
cout << static_cast <char> (remainder + 55);
}

}

else
{
num1 /= base1;
convert (num1, base1);
}
}

int main()
{
int base;
int num;

cout << "Please enter the base: ";
cin >> base;

cout << "Please enter the number: ";
cin >> num;

cout << "The converted number is ";

convert(num, base);

cout << endl;

system ("pause");
return 0;
}


what i am trying to do here is when i send in two numbers, a base and a number, i am trying to convert it into a new output like this

i send in my base 16 and my number by 575

i should get something like 3F

but i always get 2

does anybody have any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-28-2006, 09:30 PM
Lop's Avatar   
Lop Lop is offline
Speaks fluent binary
 
Join Date: May 2006
Posts: 1,149
Credits: 112
Rep Power: 16
Lop will become famous soon enough
Default

You are not storing your remainders anywhere.

It should look like this (your math):
5 ) 559
-----
5 ) 111 r 4
-----
5 ) 22 r 1
----
5 ) 4 r 2
---
0 r 4 = 4214 (base 5)

And then for the base 16 you are going to need something like this:

Code:
const char ccBaseChars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
And then called like this:
Code:
ccBaseChars [Remainder];
__________________
Lop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-29-2006, 10:34 AM
Crane's Avatar   
Crane Crane is offline
Programming Expert
 
Join Date: Nov 2005
Posts: 399
Credits: 1
Rep Power: 13
Crane is on a distinguished road
Default

You declare the remainder inside of the function so everytime the function is executed the variable gets deleted.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-29-2006, 11:42 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,485
Last Blog:
wxWidgets is NOT code ...
Credits: 830
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Out of curiosity, why are you doing this with recursion instead of using a while loop?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 07:46 AM
i have a problem please help me!!!???? stack Java Help 8 09-22-2007 03:17 PM
[C] Comparison problem Alhazred C and C++ 1 08-29-2007 04:58 AM
null exception problem connor7777 C# Programming 2 03-28-2007 11:37 AM
A small problem in the output The_Master C and C++ 3 12-13-2006 12:04 PM


All times are GMT -5. The time now is 08:03 PM.

Contest Stats

Xav ........ 1322.18
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 830.24
Brandon W ........ 735.07
chili5 ........ 309.39
Steve.L ........ 236.23
dcs ........ 216.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads