C++ needs doing

need help with doing this program


Last edited on
Hello man, here is something i've done. Hope it is good.
#include<iostream>
#include<stdlib.h>
#include<string>
#include<math.h>
#include <iomanip>
#include <sstream>
using namespace std;
int main(){
string number;
int base,convert;
char usr_rsp;
int dec;
bool valid1, valid2;
do{
do
{
cout<<"\nGive me a number: ";
cin>>number;
cout<<"\n 2-binary \t 8-octal \t 10-zecimal \t 16-hexazecimal";
cout<<"\n Enter the base of the number: ";
cin>>base;
cout<<"\n The number you entered is: "<<number<<" in base: "<<base;
cout<<"\n Are this the correct values? Y/N: ";
cin>>usr_rsp;
}
while(usr_rsp=='n' or usr_rsp=='N');
if(usr_rsp=='Y' or usr_rsp=='y')
{
switch(base)
{
case 2:
{
int value = atoi(number.c_str());
int decimal=0,i=0,rem;
while (value!=0)
{
rem= value%10;
value/=10;
decimal+=rem*pow(2,i);
i++;
}
dec=decimal;
valid1=true;
}
break;
case 8:
{
int value = atoi(number.c_str());
int decimal=0,i=0,rem;
while (value!=0)
{
rem= value%10;
value/=10;
decimal+=rem*pow(8,i);
i++;
}
dec=decimal;
valid1=true;
}
break;
case 10:
{
int value=atoi(number.c_str());
int decimal=value;
dec=decimal;
valid1=true;
}
break;
case 16:
{
int decimal=0;
stringstream ss;
ss<<hex<<number;
ss>>decimal;
dec=decimal;
valid1=true;
}
break;
default:
{
cout<<"\nThe base you chose is wrong!\n Choose again please!";
valid1=false;
}
break;
}
}
}
while (!valid1);
do{
cout<<"\n 2-binary \t 8-octal \t 10-zecimal \t 16-hexazecimal";
cout<<"\n Please enter the base you want to convert the number too: ";
cin>>convert;
switch(convert)
{
case 2:
{
int rem, i=1, binary=0;
while (dec!=0)
{
rem=dec%2;
dec/=2;
binary+=rem*i;
i*=10;
}
cout<<"\nThe number you entered converted in "<<convert<<" base is: "<<binary;
valid2=true;
}
break;
case 8:
{
int rem, i=1, octal=0;
while (dec!=0)
{
rem=dec%8;
dec/=8;
octal+=rem*i;
i*=10;
}
cout<<"\nThe number you entered converted in "<<convert<<" base is: "<<octal;
valid2=true;
}
break;
case 10:
{
cout<<"\nThe number you entered converted in "<<convert<<" base is: "<<dec;
valid2=true;
}
break;
case 16:
{
cout<<"\nThe number you entered converted in "<<convert<<" base is: "<<hex<<dec;
valid2=true;
}
break;
default:
{
cout<<"\nThe base you chose is wrong!\n Choose again please!";
valid2=false;
}
break;
}
}
while(!valid2);
}
while (usr_rsp == 'n' or usr_rsp =='N');
if (usr_rsp == 'Y'or usr_rsp == 'y')

this part does not seem to work
Topic archived. No new replies allowed.