i am done with my c++ class and got a passing grade but this project problem that i was suppose to do is incomplete and is stuck in my head so I want to try and finish it but i am stuck here... not sure if i am using a string correctly or how to even use one. I was told that I would need one to do a radix other than 10.

q1: I keep getting and overloaded function and I tried to solve this but i am unsure of what else to do can i get a hint?
q2: I am also having having trouble convert a int to a string and i looked it up but i am not sure how to use itoa or sprintf ...

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int conversion ();
int conversion (int ,int , int);
int i=0,j=0,p[50];
int main(){
    int radixa,radixb,number;
    cout<<"radix a(2-16) : ";
//  radixa=10;
        cin>>radixa;
        cout<<endl;
        cout<<"radix b(2-16): ";
        cin>>radixb;
        cout<<endl;
            cout<<"number to be converted: ";
                cin>>number;
                cout<<endl;
                conversion (radixa,radixb,number);
                conversion();

    cout<<endl;
}

int conversion(){
    i--;
for (;i>=0;i--){
if ((p[i] >= 0)){
    switch (p[i]){
        case 0:cout<<0; break;
        case 1:cout<<1; break;
        case 2:cout<<2; break;
               case 3:cout<<3; break;
        case 4:cout<<4; break;
        case 5:cout<<5; break;
        case 6:cout<<6; break;
        case 7:cout<<7; break;
        case 8:cout<<8; break;
        case 9:cout<<9; break;
        case 10:cout<<'a'; break;
        case 11:cout<<'b'; break;
        case 12:cout<<'c'; break;
        case 13:cout<<'d'; break;
        case 14:cout<<'e'; break;
        case 15:cout<<'f'; break;
}
}

}

return 0;
}
int conversion (int radixa, int radixb, int number){
int remainder=0;

if (radixa == 10){

      while( number >0 ){

     remainder = number % radixb;

    number /= radixb;


     p[i] = remainder;

      i++;
      j++;}
}
    //this is where i try to convert radixa as something
        //other than 10 in to an array
        // where I can then convert it into a
        // different base  if I exclude this portion below 
        //I can use base 10 as my radix a i can convert
        // to base 2-16 
        // i am trying to convert everything in to base 10
        // first then convert it out as whatever radixb
        // has been required to output

if (radixa !=10 || radixa !=16){

    while (number>0){
    string num = number;
         string nnum;  // not sure this works but i thought i might need a second string

nnum= number/(pow(radixa,num.length()-1));
number%=(pow(radixa,num.length()-1));
    i++;
    }
}

return 0;
}
VernonDozier commented: Good attitude. Assignment over, but wants to learn. +2

Recommended Answers

All 5 Replies

>>q2: I am also having having trouble convert a int to a string and i looked it up but i am >>not sure how to use itoa or sprintf

This is c++, so don't use either itoa or sprintf. Use stringstream instead

#include <sstream>
<snip>
int main()
{
    int x = 123;
    string str;
    stringstream stream;
    // convert int to string
    stream << x;
    cout << stream.str() << "\n";
    // convert stringstream to std::string
    stream >> str;
    cout << str.c_str() << "\n";
    // convert string back to int
    stream >> x;
    cout << x << "\n";
}
commented: gave great information to troubling question and provided a code to help with the problem +1

i played around with this problem for a while but it seems that i am using the length incorrectly could some one tell me what i need to do; the error says that length needs class/struct/union.

while (number>0){
		int num,x=0;
			num=number;
	string str;
    stringstream stream;
    stream << num;
	stream >> str;

num= number/(pow(radixa,num.length(x)-1));
number%=(pow(radixa,num.length(x)-1));
	x++;
	}

line 9: num.length(x) num is an integer so it doesn't have methods like length. perhaps you meant number.length() -- note that length() does not take any parameters.

i thought i turned the num from a int to a str with

int num,x=0;
num=number;
	string str;
    stringstream stream;
    stream << num;
	stream >> str;

i took your suggestion on how to change int to a string and used it but instead i changed num into a string and not number

i got rid of the x as the parameter but it still is stuck

>>i thought i turned the num from a int to a str with
All it does is put the digits of num into another variable named str. The original integer is unchanged. Its not possible to convert the data type of an an integer into a std::string or character array. All you can do is copy the digits into another variable, as you have done.

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.