-2

I am looking to change this from struct to classes and use a header file to hold the class.

What would you suggest in way of changing it over. The code all works. There is no problem there. just a simple change over question.

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct printype
{
    char dots[8][15];
    int unknown15;          //  can have values of 0..127
    string serial11_14;     //  8 characters 00000000...99999999
    int year8;              //  without century, 0..99
    int month7;             //  1..12
    int day6;               //  1..31
    int hour5;              //  0..23
    int minute2;            //  0..59
};                              
int getunknown15(); // prototypes       
string getserial11_14();    
int getyear8();                 
int getmonth7();            
int getday6();              
int gethour5();                 
int getminute2();   
int setunknown15(int);          //------------------------- protos
string setserial11_14(string);  
int setyear8(int);              
int setmonth7(int);             
int setday6(int);               
int sethour5(int);              
int setminute2(int);            
// int array and display info for array
void setup(char[8][15]);
// display array
void darray(char[8][15]);
//  displays printer information
void dpinfo(printype);
// set parrity
void spar(char[8][15]); 
// fill array
void ftarray(printype &);       //----------------------end protos
//-------------------------------------


void main ()
{
    printype pt;
    pt.unknown15=getunknown15();
    pt.unknown15=setunknown15(pt.unknown15);
    pt.serial11_14=getserial11_14();
    pt.serial11_14=setserial11_14(pt.serial11_14);
    pt.year8=getyear8();
    pt.year8=setyear8(pt.year8);
    pt.month7=getmonth7();
    pt.month7=setmonth7(pt.month7);
    pt.day6=getday6();
    pt.day6=setday6(pt.day6);
    pt.hour5=gethour5();
    pt.hour5=sethour5(pt.hour5);
    pt.minute2=getminute2();
    pt.minute2=setminute2(pt.minute2);
    cout <<"-----------------------------------------------------"<<endl;
    cout <<" Lets Get Started"<<endl;
    cout <<"-----------------------------------------------------"<<endl;
    setup(pt.dots);             // sets up the array 
    dpinfo(pt);                 // prints out the final array
    ftarray(pt);
    spar(pt.dots);
    darray(pt.dots);
}


int getunknown15()
{
    printype tem;
    cout <<"-----------------------------------------------------"<<endl;
    cout <<" Enter the Unkown Variable (0-127):  ";
    cin  >>tem.unknown15;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.unknown15;
}
string getserial11_14()//------------------------------------------ starts the get information sets
{
    printype tem;
    cout <<" Enter the Serial Variable (8char long):  ";
    cin  >>tem.serial11_14;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.serial11_14;
}
int getyear8()
{
    printype tem;
    cout <<" Enter the Year Variable (2char long):  ";
    cin  >>tem.year8;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.year8;
}
int getmonth7()
{
    printype tem;
    cout <<" Enter the Month Variable (2char long):  ";
    cin  >>tem.month7;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.month7;
}
int getday6()
{
    printype tem;
    cout <<" Enter the Day Variable (2char long):  ";
    cin  >>tem.day6;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.day6;
}
int gethour5()
{
    printype tem;
    cout <<" Enter the Hour Variable (2char long):  ";
    cin  >>tem.hour5;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.hour5;
}
int getminute2()
{
    printype tem;
    cout <<" Enter the Minute Variable (2char long):  ";
    cin  >>tem.minute2;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.minute2;
}
//-----------------------------------------------------------put functions (adds info to the array)

int setunknown15(int tem)
{
    printype pp;


    if (tem>127||tem<0)
    {
        cout << "Error" << endl;
        return 0;
    }
    else
    {
        pp.unknown15 = tem;
        return pp.unknown15;
    }
}
string setserial11_14(string tem)
{
    printype pp;
    if(tem.size() !=8)
    {
        cout <<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        for (int i = 0; i < 8; i++)
        {
            if(!isdigit(tem.at(i)))
            {
                cout<<"nope.jpg"<<endl;
                return 0;
            }
        }
        pp.serial11_14=tem;
        return pp.serial11_14;
    }
}
int setyear8(int tem)
{
    printype pp;
    if(tem>99||tem<0)
    {
        cout<<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        pp.year8=tem;
        return pp.year8;
    }
}
int setmonth7(int tem)
{
    printype pp;
    if(tem>12||tem<1)
    {
        cout<<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        pp.month7=tem;
        return pp.month7;
    }
}
int setday6(int tem)
{
    printype pp;
    if(tem>31||tem<1)
    {
        cout<<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        pp.day6=tem;
        return pp.day6;
    }
}int sethour5(int tem)
{
    printype pp;
    if(tem>23||tem<0)
    {
        cout<<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        pp.hour5=tem;
        return pp.hour5;
    }
}
int setminute2(int tem)
{
    printype pp;
    if(tem>59||tem<0)
    {
        cout<<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        pp.minute2=tem;
        return pp.minute2;
    }
}
void setup(char tem[8][15])
{
    for (int x=0;x<8;x++)// set to all blanks
    {
        for (int y=0;y<15;y++)
        {
            tem[x][y]=' ';
        }
    }
}
void darray(char tem[8][15])
{
    for (int x=0;x<8;x++)// set to all blanks
    {
        cout <<"\t-------------------------------"<<endl;
        cout <<"\t|";
        for (int y=0;y<15;y++)
        {

            cout << tem[x][y];
            cout<<"|";
        }
        cout <<"\n";
    }
    cout <<"\t-------------------------------"<<endl;
}
void dpinfo(printype mc)
{
    cout <<"The unknown is:\t"<<mc.unknown15<<"\nThe String is:\t"<<mc.serial11_14<<"\n Time:\n\n Year: 20"<<mc.year8<<" month: "<<mc.month7<<"\n day: "<<mc.day6<<" hour: "<<mc.hour5<<"\n minute: "<<mc.minute2<<endl;
}
void spar(char tem[8][15])
{
    int count=0;
    for (int x=0;x<7;x++)
    {
        for (int y=0;y<15;y++)
        {
            if(tem[x][y]=='*')
            {
                count+=1;
            }
        }
        if(count%2==0)
        {
            tem[x][0]='*';
        }
    }
    count=0;
    for (int a=0;a<7;a++)
    {
        for (int z=0;z<7;z++)
        {

        }
    }
}
void ftarray(printype &pt)
{
    int tem=0;
    for (int x=1;x<15;x++)
    {
        switch(x)
        {
            case 1:
            {
                tem=pt.minute2;
                break;
            }
            case 4:
            {
                tem=pt.hour5;
                break;
            }
            case 5:
            {
                tem=pt.day6;
                break;
            }
            case 6:
            {
                tem=pt.month7;
                break;
            }
            case 7:
            {
                tem=pt.year8;
                break;
            }
            case 9:
            {
                for (int j = 1; j < 8; j++)
                    {
                        pt.dots[j][x] = '*';
                    }
            }
            case 10:
            {
                tem=atoi(pt.serial11_14.substr(6,2).c_str());
                break;
            }
            case 11:
            {
                tem=atoi(pt.serial11_14.substr(4,2).c_str());
                break;
            }
            case 12:
            {
                tem=atoi(pt.serial11_14.substr(2,2).c_str());
                break;
            }
            case 13:
            {
                tem=atoi(pt.serial11_14.substr(0,2).c_str());
                break;
            }
            case 14:
            {
                tem=pt.unknown15;
                break;
            }
        }
        if(x==1||x==4||x==5||x==6||x==7||x==10||x==11||x==12||x==13||x==14)
        {
            if (tem>=64)
            {
                pt.dots[1][x]='*';
                tem-=64;
            }
            if (tem>=32)
            {
                pt.dots[2][x]='*';
                tem-=32;
            }
            if (tem>=16)
            {
                pt.dots[3][x]='*';
                tem-=16;
            }
            if (tem>=8)
            {
                pt.dots[4][x]='*';
                tem-=8;
            }
            if (tem>=4)
            {
                pt.dots[5][x]='*';
                tem-=4;
            }
            if (tem>=2)
            {
                pt.dots[6][x]='*';
                tem-=2;
            }
            if (tem>=1)
            {
                pt.dots[7][x]='*';
                tem-=1;

            }
        }
    }
}
David Alber
  • 17,624
  • 6
  • 65
  • 71
  • 1
    You should describe your puzzle, not paste so many many codes – Michael Bai Dec 12 '11 at 07:11
  • its a printer data chart. parity is odd on all rows. it takes the data you put in and write it out in the columns 64 32 16 8 4 2 1 is the row worth top to bottom. if a number is 37 then it goes second row then 5th then last row. its supposed to simulate a printer output data sheet. normally a really rudimentary sheet. all of this is normally printed with really faint yellow ink. – Benjamin McDaniel Dec 12 '11 at 12:11

2 Answers2

5

In C++, struct and class is the same thing, other than the default access level. All you need to do is replace

struct printype
{
//...
};

with

class printype
{
public:
//...
};

You can also probably replace the functions that take a printype as argument by value with member functions:

void dpinfo(printype);

becomes

class printype
{
public:
//....
    void dpinfo();
};

and will operate on this rather than the parameter.

Methods that return an object printype can become constructors.

I suspect however you have several issues with your code:

int getunknown15()
{
    printype tem;
    cout <<"-----------------------------------------------------"<<endl;
    cout <<" Enter the Unkown Variable (0-127):  ";
    cin  >>tem.unknown15;
    cout <<"-----------------------------------------------------"<<endl;
    return tem.unknown15;
}

Here, for example, you don't need the tem variable... it does nothing. You can directly read an int an return that, tem will be destroyed on function exit anyway.

Maybe you're looking for a member function, for example:

int printype::setyear8(int tem)
{
    if(tem>99||tem<0)
    {
        cout<<"nope.jpg"<<endl;
        return 0;
    }
    else
    {
        this->year8=tem;
        return this->year8;
    }
}

This way the object is modified and you don't lose changes. However, your coding style suggests little to no knowledge of oop. I suggest reading a good book before continuing, as I doubt that, as it is, the answer will make much sense to you.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

First of all, a struct and a class are the same in C++, only difference is that a struct is default public and a class is default private. All you do is change struct to class

Looking at your code, however, I'm assuming you are converting from C to C++ and want to add your functions as methods to the class. To do this, you simply add the prototypes to the class declaration and then add the class space to the function implementations. You should also add any relevant constructors and a destructor in necessary

In a h file

class Printype {
    char **dots;
    int unknown15;          //  can have values of 0..127
    string serial11_14;     //  8 characters 00000000...99999999
    int year8;              //  without century, 0..99
    int month7;             //  1..12
    int day6;               //  1..31
    int hour5;              //  0..23
    int minute2;            //  0..59

    //Constructors
    Printype();                                        //Default
    Printype(const Printype &cpy);                     //Copy
    Printype(int yr, int mo, int d, int hr, int min);  //Sample initializer

    //Destrutor
    ~Printype();

    //Methods (only gonna do 2 for example)
    int getYear();
    void setYear(int y);

};

In a cpp or cc file

#include "printype.h"

Printype::Printype() {

    dots = new char*[8];
    for(int i=0;i<8;++i) {
        dots[i] = new char[15];
    }

    unknown15 = 0; serial11_14 = ""; year8 = 0;
    month7 = 0; day6 = 0; hour5 = 0;minute2 = 0;
}

Printype::Printype(const Printype &cpy) {
    //Deep copy all values from cpy to this
}

Printype::Printype() {

    dots = new char*[8];
    for(int i=0;i<8;++i) {
        dots[i] = new char[15];
    }

    unknown15 = 0; serial11_14 = ""; year8 = yr;
    month7 = mo; day6 = d; hour5 = hr;minute2 = min;
}

//Destructor (free allocated memory)
Printype::~Printype() {
    for(int i=0;i<8;++i) {
        delete[] dots[i]; dots[i] = NULL;
    }
    delete[] dots; dots = NULL;
}

//Methods
int Printype::getYear() {
   return year8;
}

void Printype::setYear(int y) {
    year8 = y;
} 

You should also implement an = operator. Hope this gets you started and answers your question though. You should really read up on some general OOP in addition to C++ syntax, patterns, etc. A lot of this will likely look foreign and not make much sense, and it will take to long to explain in this forum. There are tons of resources online regarding this material so you should try to read up on it.

jyore
  • 4,715
  • 2
  • 21
  • 26
  • Thank you all, i again am amazed with the amount of feed back i get here. It is by far one of the most active and productive sites i have ever found. – Benjamin McDaniel Dec 12 '11 at 09:12