In: Computer Science
Purpose
To develop C++ code that incorporates: (1) generalized conversion from one number base to another; (2) string character extraction and insertion; and (3) a loop trifecta (while, for, and do loops). After this exercise you should better understand the difference between integer and string versions of a number, and the difference between integer and printable character versions of a single digit. Conversions between string and integer representations of a number are needed since you can't perform arithmetic directly on the string version of a number, and you can't index the printable digits directly with the integer version of a number.
What to do
Create a new Visual Studio console project named assignment06. Write C++ code inside the main() function that first prompts for two integers in the range 2 to 10. The first value is the base for numbers entered on the console (ibase), and the second value is the base for numbers displayed on the console (obase). Like the Linux bc program, your program allows the ibase and obase to be independently set. For example, ibase can be 2 and obase can be 10, or vice versa. The ibase and obase can be the same (not very useful but good for testing), or one could be 5 (the valid digits are 0 to 4) and the other could be 8 (the valid digits are 0 to 7).
While the ibase and obase can be read into integer variables, the number to be converted from the ibase to the obase should first be read in as a string, converted to an integer according to the ibase, and then be converted to a output string according to the obase. While the ibase and obase values are checked for validity, the number's input string does not have to be checked for validity.
Algorithm
Here is an appropriate algorithm expressed in pseudocode.
prompt for ibase and obase if (ibase and obase are valid) { declare a string named input prompt for input while (input != "x") { int value = 0 for (i from 0 to (input.length() - 1)) { convert the input[i] printable character to an integer named digit_int value = (value * ibase) + digit_int } declare a string named output do { int remainder = value % obase convert the integer remainder to a printable character named digit_char prepend digit_char to output value = value / obase } while (value != 0) display ibase, input, obase, output prompt for a string named input } } else { display a message indicating that ibase and/or obase is invalid }
Converting the algorithm to C++
Pseudocode does not need to precisely specify how an operation is performed in a particular programming language. Examples in the above pseudocode are:
here is what i have done so far but im sure there are some mistakes. plz help me complete it
#include <iostream>
#include <cstring>
int main()
{
int ibase, obase;
std::cout << "enter the value for the base of the input
followed by the value of the base for the output";
std::cin >> ibase >> obase;
if (ibase <= 10 && ibase >= 2 && obase <=
10 && obase >= 2) {
std::string input;
std::cout << "enter a interger that you want to
convert";
std::cin >> input;
while (input != "x") {
int value = 0;
int n;
n = size(input);
for (int i = 0; i < n; i++) {
int digit_int;
digit_int = input[i];
}
}
}
}
#include <iostream>
#include <cstring>
int main()
{
int ibase, obase;
std::cout << "enter the value for the base of the input
followed by the value of the base for the output\n";
std::cin >> ibase >> obase;
if (ibase <= 10 && ibase >= 2 && obase <=
10 && obase >= 2) {
std::string input;
std::cout << "enter a interger that you want to
convert\n";
std::cin >> input;
while (input != "x") {
int value = 0;
int n;
n = input.length();
int digit_int;
for (int i = 0; i < n; i++) {
digit_int = (int)input[i] - '0';
value=(value*ibase) + digit_int;
}
std::string output;
do{
int remainde = value % obase;
char digit_char = (char)remainde + '0';
output.insert(0, 1,digit_char);
value = value/obase;
}
while(value!=0);
std::cout <<ibase<<" "<<input<<"
"<<obase<<" "<<output;
std::cout << "\nenter a interger that you want to
convert\n";
std::cin >> input;
}
}
else
{
std::cout<<"Invalid";
}
}
I did code just as things were mentioned in the pseudocode and things that you have researched. One mistake that i saw was size(input). It will be input.length() to find the length of the string.
Hope all problems are solved by this code. For any doubts or questions comment below.