How do I stop the debugger from skipping my functions? - c++

I am trying to calculate students grades from a file. When entering the if statement the debugger skips all my functions and ends the program. I think it has something to do with the file not being inputted well.
int main()
{
string temp, out = "";
ifstream RF("TestAnswers(5).txt");
while (getline(RF, temp))
{
out += temp;
}
GradeCalc gCalc;
gCalc.gradeAspects(out);
gCalc.gradeCalculation();
gCalc.grade();
}
class GradeCalc
{
private:
#define DELIM ','
struct GradeAspects
{
string studentN;
string answers;
string firstname, lastname;
double scores;
int rsize;
string percent;
string readFile; // int read;
};
vector<GradeAspects> read;
GradeAspects readFile[25];
public:
void gradeAspects(string filename);
void gradeCalculation();
void grade();
};
void GradeCalc::gradeAspects(string filename)
{
ifstream input;
string line;
int rnum = 0;
char delim;
int rsize;
input.open(filename);
if (input)
{
while (!input.eof())
{
getline(input, line);
delim = line.find(DELIM);
readFile[rnum].studentN = line.substr(0, delim);
line = line.substr(delim + 1, line.length() - 1);
delim = line.find(DELIM); // moving on to firstname by finding delim.
readFile[rnum].firstname = line.substr(0, delim);
line = line.substr(delim + 1, line.length() - 1);
delim = line.find(DELIM); // moving on to lastname by finding delim.
readFile[rnum].lastname = line.substr(0, delim); // last name
line = line.substr(delim + 1, line.length() - 1);
delim = line.find(DELIM);
readFile[rnum].answers = line; // line = line.substr(delim + 1, line.length() - 1);
rnum++;
}
}
rsize = rnum;
}
void GradeCalc::gradeCalculation()
{ // once done with reads move to calculations of how many answers were right etc.
int rnum = 0;
int rsize = 0;
string answers = readFile[0].answers;
int aSize = answers.size();
for (int i = 0; i < rsize; i++)
{
int score = 0;
for (int t = 0; t < aSize; t++)
{
if (answers[t] == readFile[i].answers[i])
score++;
{
answers[i] = score;
}
}
}
}
void GradeCalc::grade()
{
string line;
int rnum = 0;
int aSize = line.length();
int choices{};
int rsize = 0;
int grade{};
for (int i = 0; i < rsize; i++)
{
int score = 0;
readFile[i].answers = choices;
readFile[i].percent = grade;
readFile[i].percent = (choices / aSize * 100);
if (grade >= 90)
readFile[rnum].firstname = "A";
else if (grade > 80 && grade < 90)
readFile[rnum].firstname = "B";
else if (grade > 70 && grade < 80)
readFile[rnum].firstname = "C";
else if (grade < 70)
readFile[rnum].firstname = "F";
cout << "student" << readFile[rnum].firstname << " " << readFile[rnum].lastname << " had "
<< readFile[i].answers << " out of " << aSize << "and has a grade of" << grade << endl;
}
}

Related

Implement the Long Integer class. Provide the ability to perform arithmetic operations with instances of the class

I have an implementation of the Long Integer class, which should provide the ability to perform arithmetic operations with instances of the class.
It works in 95% of situations, but sometimes it shows wrong results.
The screen with wrong answers is located below the code.
HugeInt.h:
#ifndef _HUGEINGH
#define _HUGEINGH
#include <iostream>
#include <list>
#include <string>
#include <math.h>
using namespace std;
class HugeInt {
private:
char* listNumber;
int maxLength, length;
bool plus;
void expansion_size_length();
void updatingLength();
void set_listNumber(int position, char num);
char get_listNumber(int position);
char symbol_in_numeral(char symbol);
char numeral_in_symbol(char numeral);
public:
string toString();
void setNumber(string str);
HugeInt operator-(HugeInt HugeInt);
HugeInt operator+(HugeInt HugeInt);
HugeInt operator*(HugeInt HugeInt);
HugeInt(string str);
HugeInt();
HugeInt(const HugeInt &lg);
~HugeInt();
};
#endif
Hugeint.cpp:
#include "stdafx.h"
#include "HugeInt.h"
void HugeInt::expansion_size_length() {
char* tmp = new char[maxLength * 2];
for (int i = 0; i < maxLength; i++) {
tmp[i] = listNumber[i];
}
delete[] listNumber;
listNumber = tmp;
maxLength *= 2;
}
void HugeInt::updatingLength() {
while (length > 1) {
int tmp = listNumber[length - 1];
if (listNumber[length - 1] == 0) {
length--;
}
else {
return;
}
}
}
void HugeInt::set_listNumber(int position, char num) {
if (position < maxLength) {
listNumber[position] = num;
if (position >= length)
length = position + 1;
}
else {
while (position > maxLength)
expansion_size_length();
length = position + 1;
}
}
char HugeInt::get_listNumber(int position) {
if (position < length) {
return listNumber[position];
}
return 0;
}
char HugeInt::symbol_in_numeral(char symbol) {
return symbol - '0';
}
char HugeInt::numeral_in_symbol(char numeral) {
return numeral + '0';
}
string HugeInt::toString() {
string str = string();
if (!plus)
str.insert(str.size(), 1, '-');
for (int i = length - 1; i >= 0; i--) {
str.insert(str.size(), 1, numeral_in_symbol(listNumber[i] / 10));
str.insert(str.size(), 1, numeral_in_symbol(listNumber[i] % 10));
}
return str;
}
void HugeInt::setNumber(string str) {
//plus = true;
//maxLength = 10;
//listNumber = new char[maxLength];
length = 0; // дело в этом
delete[] listNumber;
listNumber = new char[str.length() + 1];
maxLength = str.length() + 1;
for (int i = length; i < maxLength; i++)
listNumber[i] = 0;
length = maxLength;
if (str.size() != 0) {
int k = 0;
if (str[0] == '-') {
plus = false;
for (int i = str.size() - 1; i > 0; i -= 2) {
if (i - 1 > 0)
listNumber[k] = symbol_in_numeral(str[i]) + symbol_in_numeral(str[i - 1]) * 10;
else listNumber[k] = symbol_in_numeral(str[i]);
k++;
}
}
else if (str[0] == '+') {
plus = true;
for (int i = str.size() - 1; i > 0; i -= 2) {
if (i - 1 > 0)
listNumber[k] = symbol_in_numeral(str[i]) + symbol_in_numeral(str[i - 1]) * 10;
else listNumber[k] = symbol_in_numeral(str[i]);
k++;
}
}
else {
plus = true;
for (int i = str.size() - 1; i >= 0; i -= 2) {
if (i - 1 >= 0)
listNumber[k] = symbol_in_numeral(str[i]) + symbol_in_numeral(str[i - 1]) * 10;
else listNumber[k] = symbol_in_numeral(str[i]);
k++;
}
}
}
updatingLength();
}
HugeInt HugeInt::operator-(HugeInt _HugeInt) {
HugeInt result = HugeInt();
if (_HugeInt.plus == this->plus) {
int length_tmp = _HugeInt.length > this->length ? _HugeInt.length : this->length;
char part_sub = 0;
int sub;
char tmp1, tmp2;
for (int i = 0; i < length_tmp; i++) {
tmp1 = listNumber[i];
tmp2 = _HugeInt.listNumber[i];
sub = ((int)listNumber[i] - (int)_HugeInt.listNumber[i] - (int)part_sub);
part_sub = 0;
if (sub < 0) {
sub += 100;
part_sub = 1;
}
//else if (sub == 0) //
//part_sub = 1; //
result.set_listNumber(i, sub);
}
if (part_sub != 0) {
part_sub = 0;
result.plus = !this->plus;
for (int i = 0; i < length_tmp; i++) {
sub = ((int)result.listNumber[i] + (int)part_sub - 100);//
part_sub = 0;
if (sub < 0) {
sub = abs(sub);
part_sub = 1;
}
//else if (sub == 0) //
//part_sub = 1; //
result.set_listNumber(i, sub % 100);
}
}
result.updatingLength();
return result;
}
else {
HugeInt lg1 = HugeInt(_HugeInt);
HugeInt lg2 = HugeInt(*this);
lg1.plus = !lg1.plus;
//HugeInt.plus = !HugeInt.plus;
return lg2 + lg1;
}
}
HugeInt HugeInt::operator+(HugeInt _HugeInt) {
HugeInt result = HugeInt();
if (_HugeInt.plus == this->plus) {
int length_tmp = _HugeInt.length > this->length ? _HugeInt.length : this->length;
char part_sum = 0;
char tmp1, tmp2;
for (int i = 0; i < length_tmp; i++) {
tmp1 = _HugeInt.listNumber[i];
tmp2 = listNumber[i];
result.set_listNumber(i, (_HugeInt.listNumber[i] + listNumber[i] + part_sum) % 100);
part_sum = (_HugeInt.listNumber[i] + listNumber[i] + part_sum) / 100;
}
if (part_sum != 0) {
result.set_listNumber(length_tmp, part_sum);
}
result.plus = plus;
result.updatingLength();
return result;
}
else {
if (_HugeInt.plus) {
//plus = !plus;
HugeInt lg1 = HugeInt(_HugeInt);
HugeInt lg2 = HugeInt(*this);
lg2.plus = !lg2.plus;
return lg1 - lg2;
}
else {
HugeInt lg1 = HugeInt(_HugeInt);
HugeInt lg2 = HugeInt(*this);
lg1.plus = !lg1.plus;
//HugeInt.plus != HugeInt.plus;
return lg2 - lg1;
}
}
}
HugeInt HugeInt::operator*(HugeInt _HugeInt) {
HugeInt result = HugeInt();
int part_mul;
int iter = 0;
HugeInt tmpHugeInt;
int tmp1, tmp2;
for (int i = 0; i < _HugeInt.length; i++) {
part_mul = 0;
tmpHugeInt = HugeInt();
for (int j = 0; j < length; j++) {
tmp1 = _HugeInt.listNumber[i];
tmp2 = listNumber[j];
iter = (int)_HugeInt.listNumber[i] * (int)listNumber[j] + (int)part_mul;
part_mul = iter / 100;
tmpHugeInt.set_listNumber(j + i, iter % 100);
}
if (part_mul != 0) {
tmpHugeInt.set_listNumber(tmpHugeInt.length, part_mul);
}
result = result + tmpHugeInt;
}
if (_HugeInt.plus != plus)
result.plus = false;
result.updatingLength();
return result;
}
HugeInt::HugeInt(string str) {
plus = true;
maxLength = 10;
listNumber = new char[maxLength];
length = 0;
setNumber(str);
}
HugeInt::HugeInt() {
plus = true;
maxLength = 10;
listNumber = new char[maxLength];
length = 0;
for (int i = length; i < maxLength; i++)
listNumber[i] = 0;
}
HugeInt::HugeInt(const HugeInt &lg) {
plus = lg.plus;
maxLength = lg.maxLength;
listNumber = new char[maxLength];
length = lg.length;
for (int i = 0; i < maxLength; i++)
if (i < lg.length){
listNumber[i] = lg.listNumber[i];
}
else{
listNumber[i] = 0;
}
}
HugeInt::~HugeInt() {
//delete[] listNumber;
}
main.cpp:
#include "stdafx.h"
#include "HugeInt.h"
int _tmain(int argc, _TCHAR* argv[])
{
/*
string num1, num2;
cout << "Enter the first number:" << endl;
cin >> num1;
cout << "Enter the second number:" << endl;
cin >> num2;
HugeInt hugeNum1 = HugeInt(num1);
HugeInt hugeNum2 = HugeInt(num2);
cout << "Entered the following numbers:" << endl;
cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;
HugeInt sum = hugeNum1 + hugeNum2;
HugeInt sub = hugeNum1 - hugeNum2;
HugeInt mul = hugeNum1 * hugeNum2;
cout << "Summary = " << sum.toString() << endl;
cout << "Substruction = " << sub.toString() << endl;
cout << "Multiplacation = " << mul.toString() << endl;
*/
HugeInt longNumber1;
HugeInt longNumber2;
HugeInt longNumberRes;
/*
int firstTrue = 1837;
int secondTrue = -8108;
char buf[32];
itoa(firstTrue, buf, 10);
string firststr(buf);
itoa(secondTrue, buf, 10);
string secstr(buf);
longNumber1.setNumber(firststr);
longNumber2.setNumber(secstr);
longNumberRes = longNumber1 + longNumber2;
int res = atoi(longNumberRes.toString().c_str());
cout << "Good " << firstTrue << "+" << secondTrue << "=" << res << endl;
int firstFalse = 312;
int secondFalse = 712;
char buf2[32];
itoa(firstFalse, buf2, 10);
string firststr2(buf2);
itoa(secondFalse, buf2, 10);
string secstr2(buf2);
longNumber1.setNumber(firststr2);
longNumber2.setNumber(secstr2);
longNumberRes = longNumber1 - longNumber2;
int res2 = atoi(longNumberRes.toString().c_str());
cout << "Bad " << firstFalse << "-" << secondFalse << "=" << res2 << endl;
*/
for (int i = 0; i < 1000; i++)
{
int first = rand() % 20000 - 10000;
int second = rand() % 20000 - 10000;
char buf[32];
itoa(first, buf, 10);
string firststr(buf);
itoa(second, buf, 10);
string secstr(buf);
longNumber1.setNumber(firststr);
longNumber2.setNumber(secstr);
longNumberRes = longNumber1 - longNumber2;
int res = atoi(longNumberRes.toString().c_str());
if (res != first - second)
cout << i << " = "<< first << "+" << second << "=" << res << endl;
}
system("pause");
return 0;
}
It prints the following results when i am doing the test with 1000 random numbers (5 of 1000 results are incorrect):
48 = 6118+-7918=-1700
268 = -8887+4887=-3900
326 = 3169+-6169=-2900
474 = 212+-712=-400
492 = 1333+-7133=-5700
545 = 8352+-9552=-1100
617 = -4395+195=-4100
Looks like something wrong with minus operation, but i can not uderstand what exactly. Please, help, what is wrong in the implementation and how to fix it?

Unable to print out information

I am writing a program which stores students' information (ID, name, major, cpa and assessment). When I compile it, there is no errors and it runs smoothly until outputting the result after inputting the information successfully. When I debug, I'm sure that the information has been successfully inserted and the error is in the display function. I really need your help to figure out what the error is.
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
string student_id;
string name;
string major;
float gpa;
char assessment;
Student()
{
student_id = "";
name = "";
major = "";
gpa = 0.0;
assessment = 'F';
}
void addinfo(string id, string student_name, string student_major, float student_gpa)
{
this->student_id = id;
this->name = student_name;
this->major = student_major;
this->gpa = student_gpa;
}
char assessstudent()
{
if (this->gpa >= 8.0)
return 'A';
if (this->gpa < 8.0 && this->gpa >= 6.0)
return 'B';
if (this->gpa < 6.0 && this->gpa >= 4.0)
return 'C';
if (this->gpa < 4.0 && this->gpa >= 2.0)
return 'D';
if (this->gpa < 2.0)
return 'F';
}
};
void swap(float* a, float* b)
{
float temp;
temp = *a;
*a = *b;
*b = temp;
}
void sortwithgrade(Student* array, int r, int l)
{
float temp = array[r].gpa;
int m = (r + l) / 2;
int i = l+1;
int j = r ;
if (r > l)
{
while (1)
{
while (array[++i].gpa <= temp)
;
while (array[--j].gpa >= temp)
;
if (i >= j)
break;
swap(array[i], array[j]);
}
swap(array[i], array[r]);
sortwithgrade(array, l, m - 1);
sortwithgrade(array, m + 1, r);
}
}
void display(Student* array, int max)
{
int i = 0;
printf("%-20s%-20s%-20s%-20s%-20s\n", "Student ID", "Student name", "Student Major", "Student gpa", "Student Assess");
while (i != max)
{
printf("%-20s%-20s%-20s%-20.2f%c\n", array[i].student_id, array[i].name, array[i].major, array[i].gpa, array[i].assessment);
i++;
}
}
int main()
{
Student list[10];
cout << "How many students do you want to add?\n";
int max;
cin >> max;
getchar();
int i = 0;
for (i = 0; i < max; i++)
{
cout << "Student number " << i << endl;
cout << "ID: ";
string stdid;
getline(cin, stdid);
cout << "Name: ";
string stdname;
getline(cin, stdname);
cout << "Major: ";
string stdmajor;
getline(cin, stdmajor);
cout << "GPA: ";
float gpa;
cin >> gpa;
getchar();
list[i].addinfo(stdid, stdname, stdmajor, gpa);
char stdassess = list[i].assessstudent();
list[i].assessment = stdassess;
}
sortwithgrade(list, 0, max - 1);
display(list, max);
return 0;
}

C++ full subtractor using hexadecimals

I have input such as:
10000000000000-1=
and
AAAAABBBBBCCCCCDDDDDEEEEEFFFFF-ABCDEF0123456789ABCDEF=
I need to convert the hexadecimal strings digit by digit into decimal and keep track if I need to borrow. I'm not sure how to adjust the value of operand 1 when a borrow occurs. Such as in the first line when you have to borrow 13 times.
Currently, I have
#include <iostream>
#include <fstream>
using namespace std;
string decimalToHex(int);
void subtraction(string, string)
int hexadecimalToDecimal(char hexVal);
int fullSubtractor(int tempOp1, int tempOp2)
int main()
{
ifstream myFile;
string l1, l2, l3, l4, l5, l6, l7, l8; //lines
string l1op1, l1op2, l2op1, l2op2, l3op1, l3op2, l4op1, l4op2, l5op1, l5op2, l6op1, l6op2, l7op1, l7op2, l8op1, l8op2; //parsed operators
myFile.open("data.txt");
if (myFile.is_open()) //check if file opened
{
cout << "File opened successfully. " << endl;
}
else
{
cerr << "File failed to open." << endl;
return 1;
}
while (!myFile.eof())
{
myFile >> l6 >> l7; //read in line by line
}
l6op1 = l6.substr(0, l6.find("-"));
l6op2 = l6.substr(l6.find("-") + 1);
l6op2 = l6op2.substr(0, l6op2.length() - 1);
std::string l6op2_zeros = std::string((l6op1.length()) - l6op2.length(), '0') + l6op2;
cout << l6; // << subtraction(l6op1, l6op2_zeros) << endl;
subtraction(l6op1, l6op2_zeros);
cout << endl;
l7op1 = l7.substr(0, l7.find("-"));
l7op2 = l7.substr(l7.find("-") + 1);
l7op2 = l7op2.substr(0, l7op2.length() - 1);
std::string l7op2_zeros = std::string((l7op1.length()) - l7op2.length(), '0') + l7op2; //appends zeros to front of second operand to make it same length as operand 1
cout << l7; // << subtraction(l7op1, l7op2) << endl;
subtraction(l7op1, l7op2_zeros);
cout << endl;
myFile.close();
return 0;
}
int fullSubtractor(int tempOp1, int tempOp2)
{
static int borrow;
int result = 0;
if ((tempOp1 < tempOp2) || ((tempOp1 == 0) && (borrow == 1)))
{
tempOp1 += 16;
result = tempOp1 - borrow - tempOp2;
borrow = 1;
}
else
{
result = tempOp1 - tempOp2;
borrow = 0;
}
return result;
}
void subtraction(string op1, string op2)
{
string result;
int tempDifference = 0, tempHex = 0;
int j = op2.length() - 1;
for (int i = op1.length() - 1; i >= 0; i--)
{
int temp1 = hexadecimalToDecimal(op1[i]);
int temp2 = hexadecimalToDecimal(op2[j]);
tempHex = fullSubtractor(temp1, temp2);
result = decimalToHex(tempHex) + result;
cout << result << " ";
j--;
}
cout << result << endl;
//return result;
}
int hexadecimalToDecimal(char hexVal)
{
int base = 1;
int dec_val = 0;
if (hexVal >= '0' && hexVal <= '9')
{
dec_val += (hexVal - 48) * base;
base *= 16;
}
else if (hexVal >= 'A' && hexVal <= 'F')
{
dec_val += (hexVal - 55) * base;
// incrementing base by power
base *= 16;
}
return dec_val;
}
string decimalToHex(int decNum)
{
stringstream ss;
ss << hex << decNum;
string hexNum(ss.str());
//cout << hexNum << endl;
return hexNum;
}

How to convert any base to base 10 for big numbers

I want to convert a big number (100,000 digits) from any base to base 10. I'm using this code:
this is reading file method
string* ReadFile(string fileName) {
ifstream inFile(fileName);
string line;
string* myArray = new string[1000000];
if (inFile.is_open()) {
int count = 0;
while (getline(inFile, line)) {
myArray[count] = line;
count++;
}
inFile.close();
}
else
{
cout << "file is not open";
}
return myArray;}
convert every character to base 10 method
int val(char c){
if (c >= '0' && c <= '9')
return (int)c - '0';
else
return (int)c - 'A' + 10; }
any base to decimal
int Base2Dec(string* arr) {
int base = stoi(arr[0]);
string str = "";
int size = 0;
while (!arr[size].empty())
++size;
for (int i = size; i > 0; i--) {
str += arr[i];
}
int len = str.length();
int power = 1;
int num = 0;
for (int i = len - 1; i >= 0; i--)
{
if (val(str[i]) >= base)
{
printf("Invalid Number");
}
num += val(str[i]) * power;
power = power * base;
}
return num;}
and main method
int main(){
string* A = ReadFile("A2.txt"); cout << Base2Dec(A);
string* B = ReadFile("B.txt"); Base2Dec(B);
cin.get();}
it works but not for 100,000 digits ?! I know int type is limited and I am not allowed to use int64 so, I want to know what the solution is?!

C++: Change of base function (i.e. hex to octal, decimal, etc.) - Output slightly off for hex values

I need to create a generic function that changes from any starting base, to any final base. I have everything down, except my original function took (and takes) an int value for the number that it converts to another base. I decided to just overload the function. I am Ok with changing between every base, but am slightly off when using my new function to take in a string hex value.
The code below should output 1235 for both functions. It does for the first one, but for the second, I am currently getting 1347. Decimal to Hex works fine - It's just the overloaded function (Hex to anything else) that is slightly off.
Thanks.
#include <iostream>
#include <stack>
#include <string>
#include <cmath>
using namespace std;
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num);
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);
int main()
{
stack<int> myStack;
string hexNum = "4D3";
switchBasesFunction(myStack, 8, 10, 2323);
cout << endl << endl;
switchBasesFunction(myStack, 16, 10, hexNum);
return 0;
}
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num)
{
int totalVal = 0;
string s = to_string(num);
for (int i = 0; i < s.length(); i++)
{
myStack.push(s.at(i) - '0');
}
int k = 0;
while (myStack.size() > 0)
{
totalVal += (myStack.top() * pow(startBase, k++));
myStack.pop();
}
string s1;
while (totalVal > 0)
{
int temp = totalVal % finalBase;
totalVal = totalVal / finalBase;
char c;
if (temp < 10)
{
c = temp + '0';
s1 += c;
}
else
{
c = temp - 10 + 'A';
s1 += c;
}
}
for (int i = s1.length() - 1; i >= 0; i--)
{
cout << s1[i];
}
cout << endl << endl;
}
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s)
{
int totalVal = 0;
for (int i = 0; i < s.length(); i++)
{
myStack.push(s.at(i) - '0');
}
int k = 0;
while (myStack.size() > 0)
{
totalVal += (myStack.top() * pow(startBase, k++));
myStack.pop();
}
string s1;
while (totalVal > 0)
{
int temp = totalVal % finalBase;
totalVal = totalVal / finalBase;
char c;
if (temp < 10)
{
c = temp + '0';
s1 += c;
}
else
{
c = temp - 10 + 'A';
s1 += c;
}
}
for (int i = s1.length() - 1; i >= 0; i--)
{
cout << s1[i];
}
cout << endl << endl;
}
Sorry, but I'm having issues understanding your code, so I thought I'd simplify it.
Here's the algorithm / code (untested):
void convert_to_base(const std::string& original_value,
unsigned int original_base,
std::string& final_value_str,
unsigned int final_base)
{
static const std::string digit_str =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ((original_base > digit_str.length()) || (final_base > digit_str.length())
{
std::cerr << "Base value exceeds limit of " << digit_str.length() << ".\n";
return;
}
// Parse string from right to left, smallest value to largest.
// Convert to decimal.
unsigned int original_number = 0;
unsigned int digit_value = 0;
int index = 0;
for (index = original_value.length(); index > 0; --index)
{
std::string::size_type posn = digit_str.find(original_value[index];
if (posn == std::string::npos)
{
cerr << "unsupported digit encountered: " << original_value[index] << ".\n";
return;
}
digit_value = posn;
original_number = original_number * original_base + digit_value;
}
// Convert to a string of digits in the final base.
while (original_number != 0)
{
digit_value = original_number % final_base;
final_value_str.insert(0, 1, digit_str[digit_value]);
original_number = original_number / final_base;
}
}
*Warning: code not tested via compiler.**