#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.

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.