#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>

using namespace std;

int dectoint (string st);
int hextoint (string st);
int bintoint (string st);


string inttobin (int n);
//string inttohex (int n);
//string inttodec (int n);

int main()

{
string st;
cout<< "Enter a number in dec,hex,or binary: \n";
getline(cin,st);

if (st[0]=='$')
cout << st<< "=" << hextoint(st)<<", "<< st <<", "<< intobin(hextoint(st))<<endl;

//else if(st[0]=='%')
//cout << st<< "=" << bintoint(st)<<", "<< inttohex((bintoint(st))<< ", "<<st<< endl;

//else if(st[0]!='$' && st[0]!='%') cout<<st<<"="<<dectoint(st)<<", "<< inttohex(st)<< ", "<<inttobin(st)<<endl;

}


int dectoint(string st)
{
int n=0;
	for (int i=0; i<st.length(); i++)
	{
	 char ch = st [i];
	 n= n*10+(ch-'0');
	}
return n;
}

int hextoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*16+(ch-'0');
	}
return n;
}

int bintoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*2+(ch-'0');
	}
return n;
}	 

string inttobin (int n)
{
ostringstream buffer;
string result=buffer.str();
int n;
int num;

while (n>0) 
{
n=n/2;
num=n%2;
if (n>1)buffer<< num; 
else if (n ==1) buffer<<1;
}

return result;
	
}

//string inttohex (int n);
//string inttodec (int n);

I just want my program to take a number and put it through the hextoint function then pass that to the intobin function! When I compile i get a few errors:inttobin not declared in this scope AND declaration of 'int n' shadows a function

What can I do to fix this in a very simple way?

Edited 6 Years Ago by hdb25: n/a

inttobin is spelled incorrectly on line 25.

Remove line 72, the redeclaration of n is masking your function parameter n.

that helps lol geez this programming stuff is editing intensive. It is still not giving me to correct result in binary terms, but I think thats just because the function isn't quite right. Thanks!

Ok so the program compiles fine, but when I actually run it, it is not printing out the result of intobin(hextoint(st))

Ok so the program compiles fine, but when I actually run it, it is not printing out the result of intobin(hextoint(st))

So finish formatting your code properly and post it. We can't do anything if we can't see the current code. And please explain in detail what's wrong, not just it won't print.

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>

using namespace std;

int dectoint (string st);
int hextoint (string st);
int bintoint (string st);


string inttobin (int n);
string inttohex (int n);

int main()

{
 string st;
 cout<< "Enter a number in dec,hex,or binary: \n";
 getline(cin,st);

 if (st[0]=='$')
 cout << st<< "=" << hextoint(st)<<", "<< st <<", "<< inttobin(hextoint(st))<<endl;

 /*else if(st[0]=='%')
 cout << st<< "=" << inttohex(bintoint(st))<<", "<< inttohex((bintoint(st))<< ", "<<st<< endl;

 else if(st[0]!='$' && st[0]!='%') 
 cout << st<< "=" << inttohex(st)<< ", "<<inttobin(st)<<endl;
*/
return 0;
}


int dectoint(string st)
{
int n=0;
	for (int i=0; i<st.length(); i++)
	{
	 char ch = st [i];
	 n= n*10+(ch-'0');
	}
return n;
}

int hextoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*16+(ch-'0');
	}
return n;
}

int bintoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*2+(ch-'0');
	}
return n;
}	 

string inttobin (int n)
{
 ostringstream buffer;
 string result=buffer.str();
 int num;
 while (n>0) 
 {
	num=n%2;
	if (n>1)buffer<< num; 
 	else if (n ==1) buffer<<n;
 	n=n/2;
 }
 return result;
}

/*string inttohex (int n)
{ 
 ostringstream buffer;
 string result=buffer.str();
 int num;
 while (n>=0)
 {
	num=n%16;
	buffer<<num='0';
	n=n/16;
 }
 return result;
}*/

When ran I enter $13 and get:
$13=19, $13,

In the blank space after the last comma, it should have SOMETHING at least im hoping for 1101 ????

You never load result with anything. At the end of inttobin() you've loaded buffer, then output result. But you didn't move buffer into result.

so i have to define string result=buffer.str() at the end of the buffer inputs?

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>

using namespace std;

int dectoint (string st);
int hextoint (string st);
int bintoint (string st);


string inttobin (int n);
//string inttohex (int n);
//string inttodec (int n);

int main()

{
string st;
cout<< "Enter a number in dec,hex,or binary: \n";
getline(cin,st);

if (st[0]=='$')
cout << st<< "=" << hextoint(st)<<", "<< st <<", "<< intobin(hextoint(st))<<endl;

//else if(st[0]=='%')
//cout << st<< "=" << bintoint(st)<<", "<< inttohex((bintoint(st))<< ", "<<st<< endl;

//else if(st[0]!='$' && st[0]!='%') cout<<st<<"="<<dectoint(st)<<", "<< inttohex(st)<< ", "<<inttobin(st)<<endl;

}


int dectoint(string st)
{
int n=0;
	for (int i=0; i<st.length(); i++)
	{
	 char ch = st [i];
	 n= n*10+(ch-'0');
	}
return n;
}

int hextoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*16+(ch-'0');
	}
return n;
}

int bintoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*2+(ch-'0');
	}
return n;
}	 

string inttobin (int n)
{
ostringstream buffer;
string result=buffer.str();
int n;
int num;

while (n>0) 
{
n=n/2;
num=n%2;
if (n>1)buffer<< num; 
else if (n ==1) buffer<<1;
}

return result;
	
}

//string inttohex (int n);
//string inttodec (int n);

I just want my program to take a number and put it through the hextoint function then pass that to the intobin function! When I compile i get a few errors:inttobin not declared in this scope AND declaration of 'int n' shadows a function

What can I do to fix this in a very simple way?

no need for stringstream.. try it like this (not tested):

string inttobin(int n){
 
  string result;
  while (n > 0){
    if (n%2 == 0) result.insert(0, "0");
    else result.insert(0, "1");
    n = n/2;
  }
  
  return result;
}

wow that worked great! and I didnt even have to reverse the buffer string or anything! Thanks a bunch

This question has already been answered. Start a new discussion instead.