User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,473 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 744 | Replies: 2
Reply
Join Date: Oct 2007
Posts: 2
Reputation: ComputerLand is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ComputerLand ComputerLand is offline Offline
Newbie Poster

Need advice and base conversion. Please help as soon as you can. Thanks

  #1  
Oct 19th, 2007
#include <iostream.h>
int main(void){

int base;
char num;

cout<< "Enter a base: ";
cin >> base;
while((base>=2)&&(base<=10)){
}
cin>>num;
while((num-'0') < (base)) && (c>='0'){
base>=base*2+(num-'0')
cin.get>>num;
}
cout<<base;
cin.putback>>num;
return 0;
}

Clearly, this program doesn't work very well. I am in need of help. The teacher really gave us nothing that was to be in the program. Just what the coding means like what is a character, what is a loop, etc. I got that far, but now I need some help. The program is supposed to do this:

1. Convert any base into a decimal
2. Skip over anything not in the base

An example of the input and output would be like this:

Enter a base: 5
Enter a number: 3435//=5559*1
Converted value: 269

The number supposed to convert was 3431 because everything else was a non-numeric character OR a digit not in the base. As we all know, there is no "5" in base 5. Decimal 5 is represented by "10" in base 5, so we need to skip over that.

Can someone give me an explanation of what eof, putback, and get are? My teacher told us to read about it while doing the program. I tried to apply it, but probably didn't do so properly. Can someone get me a good explanation when I apply it to cin. Thanks. Any help solving my program or giving me what restrictions to put or what to alter or anything of the sort woudl be a wondrous beacon of light for me. Thanks again. Also, can someone recommend a C++ book to me or a site with a good tutorial? I have been at this for only 10 days.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: ComputerLand is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ComputerLand ComputerLand is offline Offline
Newbie Poster

Re: Need advice and base conversion. Please help as soon as you can. Thanks

  #2  
Oct 19th, 2007
I am hypothesizing ways to convert a number to decimal. Here is my theory: I am still experimenting, and it may take a long, long, long while for my to finish. Maybe you may want to help me apply it. =D

372 base 4 is like (3*4*4)+(7*4)+2. That is because the place value with the 3 is a 4^2 and the one with the 7 is 4^1 and the 2 is 4^0. I got that. I don't know how to apply it like at all. This is because we never did anything like kind of like this. How would I like be able to discriminate between place values. If I could, I could write like base*place number or something except more abstract than that. That is my idea. Doesn't seem too bad, but probably not most efficient way out there.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need advice and base conversion. Please help as soon as you can. Thanks

  #3  
Oct 19th, 2007
Wh.. what... your teacher gave you that code? He should be fired.

For your language questions, this is a really handy site. http://www.cppreference.com/
Click on the part for C++ I/O to find putback and the like. (BTW. Don't use putback in your code. If you ever actually do have to use it, you'll know enough about programming to know when you need to use it.)

A number's radix (or base) is a textual representation for use by humans. No matter what the number's base it is stored in the computer exactly the same way. Hence, when you read the number from its textual representation you've done 99% of the work. All you need to do is send the int to cout to see its decimal representation.

For converting the input into a valid number you are on the right track. Lets break down the number 789 (decimal). That's 700 + 80 + 9, or (7 * 10^2) + (8 * 10^1) +(9 * 10^0). [When I say 10^N I mean ten raised to the power of N.]

The same holds true for other bases. In base 5, 342 is (3 * 5^2) + (4 * 5^1) + (2 * 5^0). Notice also that the exponent is 0 for the ones position, 1 for the tens position, 2 for the hundreds, 3 for the thousandths, etc.

Finally, remember that if you want to shift a number up one decimal place, just multiply it by ten (or whatever the radix is).
54 * 10 --> 540.

This should be enough to get you started. Please toss that other stuff and write something new. You'll get the correct answer much faster.

Post back with your efforts.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC