1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

7 segment display

Discussion in 'C++' started by hobbyist, Feb 12, 2012.

  1. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I've been playing around with the following code to produce a simulated 7 segment display in text based programs... perhaps games with a score board. If anyone sees anything troublesome, please point it out.

    If only I understood threading. :D

    Code:
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    
    typedef struct { std::vector<std::string> digit; } _7seg;
    
    int getNumSegments();
    void setSegment(_7seg *, const std::string &);
    void showSegment(_7seg *, const int &);
    
    int main() {
    
       _7seg *display = NULL;
       int i, num_segments = getNumSegments();
       display = new _7seg[num_segments]; // dynamically allocate an array of structs
    
       std::srand((unsigned)std::time(NULL)); // initialize the random seed generator
    
       if(display) {
    
         for(i=0; i < num_segments; ++i)
            for(int j=0; j < 5; ++j)
               display[i].digit.push_back("...");  // initialize the vector for testing
    
         std::ostringstream os;  // create a ostringstream obj for storing
         os.clear();             // numbers in a string obj
         os.str("");
    
         for(i=0; i < num_segments; ++i) // generate a "random" series of digits
            os << std::rand() % 10;      // ostringstream is similar to Cs sprintf function
    
         std::string ran_num(os.str());
    
         std::cout << "\nthe random number is " << ran_num << "\n\n";
    
         setSegment(display, ran_num);
         showSegment(display, num_segments);
    
         for(i=0; i < num_segments; ++i) // clear the vectors of data
            display[i].digit.clear();    // before freeing the dynamic
                                         // structure
         delete [] display;
      }
    }
    
    int getNumSegments() {
    
       /* purpose: request and verify that user input is
                   valid within the range of 2 to 9.
                   a stringstream object is used to avoid
                   potential stream errors on "bad" input */
    
       int ndisp;
    
       std::string input;
       std::stringstream ss; // create a stringstream obj to convert a string to an integer
                             // avoids stream errors by bad user input
       do {
    
          ss.clear(); // reset any eof bit
          ss.str(""); // reset any string converted
    
          std::cout << "Enter number of segments(2 - 9): ";
          std::getline(std::cin, input);
    
          ss << input;
    
      } while(!(ss >> ndisp) || (ndisp < 2 || ndisp > 9));
    
      return ndisp;
    }
    
    void setSegment(_7seg *tmp, const std::string &the_digits) {
    
    /*  ###   #  ###  ###  # #  ###  ###  ###  ###  ###
        # #   #    #    #  # #  #    #      #  # #  # #
        # #   #  ###  ###  ###  ###  ###   #   ###  ###
        # #   #  #      #    #    #  # #  #    # #    #
        ###   #  ###  ###    #  ###  ###  #    ###    #
    
        purpose: parse a string representing each digit in a
                 7 segment display and store the conversion in each row
                 of the vector "digit"
    */
    
       for(int i=0; i < the_digits.size(); ++i) {
    
          char c = the_digits.at(i);
    
          tmp[i].digit[0] = (c == '1' ? "  #" : c == '4' ? "# #" : "###");
          tmp[i].digit[1] = (c == '1' || c == '2' || c == '3' || c == '7' ? "  #" :
                             c == '5' || c == '6' ? "#  " : "# #");
          tmp[i].digit[2] = (c == '1' ? "  #" : c == '7' ? " # " : c == '0' ? "# #" : "###");
          tmp[i].digit[3] = (c == '0' || c == '6' || c == '8' ? "# #" :
                             c == '2' || c == '7' ? "#  " : "  #");
          tmp[i].digit[4] = (c == '7' ? "#  " : c == '1' || c == '4' || c == '9' ? "  #" : "###");
       }
    }
    
    void showSegment(_7seg *tmp, const int &nsize) {
    
       /* purpose: output each digit of the user defined
                   7 segment display */
    
       for(int i=0; i < 5; ++i) {
          for(int j=0; j < nsize; ++j)
             std::cout << tmp[j].digit[i] << " ";
    
          std::cout.put('\n');
       }
    }
     

Share This Page