How to print commas ONLY if the user input is in the thousands
Nov 5, 2013 at 5:05pm UTC
Hi, I am in a first year C++ class. I am writing a program that is supposed to contain a flexible, general algorithm for base conversions. I am satisfied with my algorithm, but I am having difficulty meeting all of my instructors criteria. I need to print a comma in my decimal output if the number to be converted is in the ten thousands. Below is my code, and I have taken out the user prompt and just hard set a value to be converted in the main function (the maximum value)
for clarity, here is my instructor's criteria:
http://mylinux.langara.bc.ca/~peterbaker/Courses/CpSc1155-IntroCpp/CPSC-1155-Cpp-PB-Lab7.pdf
NOTE: I am only working on the converter part of this assignment, I still have a lot more work to do, obviously.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <iostream>
using namespace std;
int converter (int userInput) {
int qtnt;
int dat = userInput;
int hexData [4];
int binData [16];
int octData [ 6 ];
int decData [ 5 ];
while (dat != -1) {// While loop: Runs conversion statements
if (dat == -1) {// If statement: Exits if input it negative 1
return 0;// Ends program
}
if (dat >= 0 && dat <= 65535) { // If statement: executes conversion statements if user input is valid
qtnt = dat; // set quotient to user input
for (int counter = 0; counter < 5; counter++) { // populate decimal array
decData[counter] = qtnt % 10;
qtnt = qtnt / 10;
}
cout << "In Decimal: " ;
for (int counter = 4; counter >= 0; counter--) { // // prints most significant bit first
if (decData[counter] == 0) { // eliminate leading zeros
cout <<"" ;
}
else {
///////////////////////////// (figure out how to print comma) <-----------------------------------
cout << decData[counter];//print current digit
}
}
cout << endl;
qtnt=dat;//reset qtnt to user input
for (int counter = 0; counter < 4; counter++) {// populate hex array
hexData[counter] = qtnt % 16;
qtnt = qtnt / 16;
}
cout <<"In Hexidecimal: " ;
for (int counter = 3; counter >= 0; counter--) {// prints most significant bit first
if (hexData[counter] == 0) {//eliminate leading zeros
cout <<"" ;
}
else if (hexData[counter] > 9) {//print ascii characters for hex digits over 9
char hex = (char )hexData[counter] + 55;
cout << hex;
}
else { //print current hex digit less than 9
cout << hexData[counter];
}
}
qtnt= dat;// reset qtnt to user input
for (int counter = 0; counter < 16; counter++) {// populate binary array
binData[counter] = qtnt % 2;
qtnt = qtnt / 2;
}
cout << endl;
cout << "In Binary: " ;
for (int counter = 15; counter >= 0; counter--) {// prints most significant bit first
if (counter < 15 && counter % 4 == 3) {// print dashes for every 4 bits
cout << "-" ;
}
cout << binData[counter];//print current binary digit
}
cout << endl;
qtnt= dat;
for (int counter = 0; counter < 6; counter++) { //populate octal array
octData[counter] = qtnt % 8;
qtnt = qtnt / 8;
}
cout << "In Octal: " ;
for (int counter = 5; counter >= 0; counter--) {// prints most significant bit first
if (octData[counter] == 0) {//eliminate leading zeros
cout <<"" ;
}
else {
cout << octData[counter];
}
}
cout << endl;
return 0;
} // end if statement
} // end while loop
} // end converter function
int main(){
int input = 65535;// this would be "cin" on final program, preceded by a prompt
converter (input);
return 0;
}
thank you very much for any suggestions.
Topic archived. No new replies allowed.