I have submitted here my solution to the LCD Diplay programming challege(detailed here) Please review my code and let me know what you think.
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
/*We use this array to store the graphical LCD
representation of each number each digit is stored in an array of size 5 that
represents each of 5 rows of the LCD output. */
const string lcd_digits_images[10][5] = { { " - ","| |"," ","| |"," - " },
{ " "," |"," "," |"," " },
{ " - "," |"," - ","| "," - " },
{ " - "," |"," - "," |"," - " },
{ " ","| |"," - "," |"," " },
{ " - ","| "," - "," |"," - " },
{ " - ","| "," - ","| |"," - " },
{ " - "," |"," "," |"," " },
{ " - ","| |"," - ","| |"," - " },
{ " - ","| |"," - "," |"," - " } };
/*draws any section that could occupy a '-' in the LCD representation of a number.
These are sectons 1, 3 and 5
digits_to_draw represents the digits to be drawn.
rowNo is the section number.
size represents the size parameter mentioned in the spec.
number_of_digits is the number of digits that for the number to be drawn.*/
void draw_horizontal_row(int* digits_to_draw, int rowNo, int size, int number_of_digits ){
for (int i = 0; i < number_of_digits; i++) {
if (lcd_digits_images[digits_to_draw[i]][rowNo].compare(" - ") == 0) {
cout << " ";
for (int j = 0; j < size; j++) {
cout << "-";
}
cout << " ";
if (i < number_of_digits - 1) {
cout << " ";
}
}
else {
for (int i = 0; i < size + 2; i++) {
cout << " ";
}
if (i < number_of_digits - 1) {
cout << " "; //A space follows each digit drawn except the last one.
}
}
}
cout << endl; //After drawing the digits, we add a blank line to seperate our output.
}
/*draws any section that could occupy a '|' in the LCD representation of a number.
These are sectons 2 and 4 of the
digits_to_draw represents the digits to be drawn.
rowNo is the section number.
size represents the size parameter mentioned in the spec.
number_of_digits is the number of digits that for the number to be drawn.*/
void draw_vertical_row(int* digits_to_draw, int rowNo, int size, int number_of_digits) {
for (int i = 0; i < size; i++) {
for (int i = 0; i < number_of_digits; i++) {
if (lcd_digits_images[digits_to_draw[i]][rowNo].compare(" |") == 0) {
for (int i = 0; i < size+1; i++) {
cout << " ";
}
cout << "|";
}
else if (lcd_digits_images[digits_to_draw[i]][rowNo].compare("| ") == 0) {
cout << "|";
for (int i = 0; i < size + 1; i++) {
cout << " ";
}
}
else {
cout << "|";
for (int i = 0; i < size; i++) {
cout << " ";
}
cout << "|";
}
if (i < number_of_digits - 1) {
cout << " ";//A space follows each digit drawn except the last one.
}
}
cout << endl; //After drawing the digits, we add a blank line to seperate our output.
}
}
int main()
{
int size, number;
string number_to_draw;
while (cin >> size >> number_to_draw) {
number = stoi(number_to_draw);
if (number == 0 && size == 0) {
return 0;
}
int* numbers = new int[number_to_draw.length()];
for (int i = 0; i < number_to_draw.length(); i++) {
numbers[i] = number_to_draw.at(i) - '0' ;
}
int rowNo = 0;
draw_horizontal_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_vertical_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_horizontal_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_vertical_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_horizontal_row(numbers, rowNo, size, number_to_draw.length());
cout << endl;
}
}
using namespace std;
as recommended. Don't forget to delete the allocatedint* numbers
before next loop. \$\endgroup\$