# Convert an integer in base b1 to an integer in base b2

Page 1 of 1

## 9 Replies - 866 Views - Last Post: 07 January 2009 - 11:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=79687&amp;s=f4faabf5d6828588ea7d6f6c88a6450d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 frosha

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 07-January 09

# Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 02:28 AM

Hi,

Am frosha and please I need a help with my project
its in microsoft visual C++

1. Theme: Implementing a course related C++ program.

2. Objectives:
a. Have the students to practice programming in C++.
b. Have the students to practice documenting their programs.
c. Have the students to practice numbering conversion.
d. Have the students to practice analyzing the program time complexity.

3. Topic: Implement in C++ a program to solve the following problem:
Covert an integer in base b1 to an integer in base b2, where b1 and b2 are integers and 2≤b1≤10 and 2≤b2≤10.

This is my code

```#include <iostream.h>
#include <cmath>
int main()
{int FBase , SBase ,number, counter,n[10],q;
cout << "select the base from 2 to 10 :" ;
cin  >> FBase ;
cout << "write the Number which you want to convert : " ;
cin >> number ;
q= 0 ;
counter = 0 ;
while ( number != 0 )
{
q += (number %10 * pow(FBase,counter) );
number  = number/10;
counter++;
}
cout << "convert to decimal = " << q  << endl;
cout << "select the base to convert " ;
cin >> SBase ;
counter = 0 ;
while ( q != 0 )
{
n[counter] =q % SBase ;
q = q / SBase ;
counter = counter + 1 ;
}
for (counter = counter -1  ; counter >= 0 ; counter--)
cout << n[counter] ;
cout << endl;
system("pause");
return 0 ;
}

```

I have one ERROR
I realy dont know how to handle it

please also I want to know if its right with the problem or not !!

I realy need to solve this code right
please

Waiting..[u]

** Edit **

Is This A Good Question/Topic? 0

## Replies To: Convert an integer in base b1 to an integer in base b2

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 109
• Posts: 1,530
• Joined: 25-January 07

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 02:44 AM

I haven't tried to run your code, but I found this one
q += ((number %10) * (int)pow(FBase,counter) ); function pow returns a double data type result. So result of total expression becomes double, whereas your RHS is integer [variable q]. So either make q as a double or typecase the result of pow the way I did.

I hope this will help you.
Was This Post Helpful? 0

### #3 no2pencil

• Original Digital Gansta

Reputation: 4465
• Posts: 24,913
• Joined: 10-May 07

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 04:04 AM

** Renamed title to be descriptive of problem **
Was This Post Helpful? 0

### #4 frosha

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 07-January 09

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 05:13 AM

AmitTheInfinity, on 7 Jan, 2009 - 01:44 AM, said:

I haven't tried to run your code, but I found this one
q += ((number %10) * (int)pow(FBase,counter) ); function pow returns a double data type result. So result of total expression becomes double, whereas your RHS is integer [variable q]. So either make q as a double or typecase the result of pow the way I did.

I hope this will help you.

I Understund, but the problem is that I dont know how to solve the ERROR
Was This Post Helpful? 0

### #5 AmitTheInfinity

• C Surfing ∞

Reputation: 109
• Posts: 1,530
• Joined: 25-January 07

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 05:18 AM

Replace your statement with the one I posted.
Was This Post Helpful? 0

### #6 frosha

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 07-January 09

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 06:26 AM

AmitTheInfinity, on 7 Jan, 2009 - 04:18 AM, said:

Replace your statement with the one I posted.

!!!
you are right
We forget some thing here !
Thanx so much

but now, the code is right with the main problem?
Was This Post Helpful? 0

### #7 AmitTheInfinity

• C Surfing ∞

Reputation: 109
• Posts: 1,530
• Joined: 25-January 07

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 06:35 AM

I would suggest you to validate the number [to be converted] for given base before converting it to decimal.

for example if I input First base as 2 but I entered number to be converted as 3, and program will still continue! even though there is no 3 in binary base!

I haven't checked whether it works or not, but I don't find any logical mistakes in it so It should work for you.

This post has been edited by AmitTheInfinity: 07 January 2009 - 06:36 AM

Was This Post Helpful? 0

### #8 frosha

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 07-January 09

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 09:37 AM

AmitTheInfinity, on 7 Jan, 2009 - 05:35 AM, said:

I would suggest you to validate the number [to be converted] for given base before converting it to decimal.

for example if I input First base as 2 but I entered number to be converted as 3, and program will still continue! even though there is no 3 in binary base!

I haven't checked whether it works or not, but I don't find any logical mistakes in it so It should work for you.

OK I will see and tell you about it

THANX you ..

Was This Post Helpful? 0

### #9 frosha

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 07-January 09

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 11:27 PM

AmitTheInfinity, on 7 Jan, 2009 - 01:44 AM, said:

I haven't tried to run your code, but I found this one
q += ((number %10) * (int)pow(FBase,counter) ); function pow returns a double data type result. So result of total expression becomes double, whereas your RHS is integer [variable q]. So either make q as a double or typecase the result of pow the way I did.

I hope this will help you.

Still there is one ERROR

error C2065: 'system' : undeclared identifier

??
Was This Post Helpful? 0

### #10 no2pencil

• Original Digital Gansta

Reputation: 4465
• Posts: 24,913
• Joined: 10-May 07

## Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 11:31 PM

frosha, on 8 Jan, 2009 - 12:27 AM, said:

error C2065: 'system' : undeclared identifier

Add the following includes to your code.

```#include <stdio.h>
#include <stdlib.h>

```

Was This Post Helpful? 0

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).