Question: C++ HW HELP PLEASE QUICK!! I'm receiving an error in my code @line 127 and @line222 please fix and resubmit code thank you #include #include
C++ HW HELP PLEASE QUICK!! I'm receiving an error in my code @line 127 and @line222
please fix and resubmit code thank you
#include
#include
#include
using namespace std;
//Function to get choice of user, more number to convert
bool isMore() {
char input;
cout << "Are there more numbers (Y/N)? ";
cin >> input;
if (input == 'Y' || input == 'y')
return true;
else
return false;
}
// Get input number and base from user
string getNumberWithBase() {
string userInput;
cout << "Type in an integer number in the format (n)b " << "where n is the representation of the number in base b : ";
cin >> userInput;
return userInput;
}
// Get input target base from user
int getTargetBase() {
int targetBase;
cout << "What is the target base? ";
cin >> targetBase;
return targetBase;
}
int val(char c)
{
if (c >= '0' && c <= '9')
return (int)c - '0';
else
return (int)c - 'A' + 10;
}
char revVal(int num)
{
if (num >= 0 && num <= 9)
return (char)num;
else
return 'A' + (num-10);
}
bool isValid(string num, int base) {
int len = num.size();
for (int i = len - 1; i >= 0; i--)
{
// A digit in input number must be
// less than number's base
if (val(num[i]) >= base)
{
cout << "Invalid Number ";
return false;
}
}
return true;
}
// Below are three function to convert the number from any base to any another base
//toDigits function used to Convert a positive number n to its digit representation in base b.
string toDigits(int n, int b) {
int digits[100],i = 0;
string num="";
while (n > 0) {
digits[i]=n % b;
num = to_string(digits[i]) + num;
n = n / b;
i++;
}
return num;
}
//Compute the number given by digits in base b.
int fromDigits(int d[], int len, int b) {
int n = 0;
for (int i = 0; i < len; i++)
n = b * n + d[i];
return n;
}
//Convert the digits representation of a number from base b to base c.
string convertBase(string num, int base, int targetBase) {
//First convert number to array of integer using function val
int digits[100];
int i;
for ( i = 0; i < num.size(); i++) {
digits[i] = val(num[i]);
}
int b = fromDigits(digits, i,base);
return toDigits(b, targetBase);
}
void doPrintOut(string input, string output) {
ofstream outdata; // outdata is like cin
outdata.open("output.txt"); // opens the file
if (!outdata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
outdata << input << "=" << output << " ";
outdata.close();
}
int main() {
string userInput, output;
string num, target;
int base, targetBase;
cout << "This is a program to read and write integer numbers in different bases. ";
while (isMore()) {
userInput = getNumberWithBase();
targetBase = getTargetBase();
//userInput will conatin (n)b, now to get n and b using sub string function
num = userInput.substr(1, userInput.find(")")-1);
base = stoi(userInput.substr(userInput.find(")")+1));
if (isValid(num, base))
target = convertBase(num, base, targetBase);
output = "(" + target + ")" + to_string(targetBase);
cout << output << " ";
doPrintOut(userInput, target);
}
int n;
cin >> n;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
