I need a bit of help on my program:
#include <iostream>
#include<conio.h>
using namespace std;
class Position {
public :
int line;
int column;
Position(int,int);
Position();
};
Position::Position(int n, int m) : line{n},column{m}{}
class Board {
private :
int** tab;
int nbline;
int nbcolumn;
public :
Board(int, int);
void setValue(Position&, int);
int getValue(Position&);
int getNbline();
int getNbcolumn();
};
class Play {
private :
// Play m_instance;
void moves(Board, Position&); // quand le joueur joue
// void moves(Board, Position); // quand l'IA joue. Mettre une énum pour direction,
bool wincondition(Board);
Play& operator=(const Play&);
public :
// static Play& Instance();
};
Board::Board(int m, int n) : tab{new int*[m]}, nbline{m}, nbcolumn{n}{
int x(0);
for (int i = 0; i < m; ++i){
tab[i] = new int[n];
for(int j = 0; j < n; ++j) {
tab[i][j] = x; x++;}}
}
void Board::setValue(Position& p, int value) { tab[p.line][p.column] = value; }
int Board::getValue(Position& p) { return tab[p.line][p.column]; }
int Board::getNbline() { return nbline; }
int Board::getNbcolumn() { return nbcolumn; }
void Play::moves(Board tab, Position& blank) {
/* int c = getch() ;
Position tmp;
if(c==0 || c==224) {c = getch();
switch(c){
case 75 : //left
if(blank.column-1>=0) {
tmp.column = blank.column;
tmp.line = blank.line;
tab.setValue(blank,tab.getValue(tmp));
blank.column++;
tab.setValue(blank, 0);
}
break;
case 72 : // haut
if(blank.line+1<=0) {
tmp.column = blank.column+1;
tmp.line = blank.line;
tab.setValue(blank,tab.getValue(tmp));
blank.column++;
tab.setValue(blank, 0);
}
break;
case 77 ://droit
if(blank.column+1<=tab.getNbcolumn()) {
tmp.column = blank.column;
tmp.line = blank.line;
tab.setValue(blank,tab.getValue(tmp));
blank.column--;
tab.setValue(blank, 0);
}
break;
case 80 : //bas
if(blank.line+1<=tab.getNbline()) {
tmp.column = blank.column+1;
tmp.line = blank.line;
tab.setValue(blank,tab.getValue(tmp));
blank.column++;
tab.setValue(blank, 0);
}
break;
default : cout << "\n ERROR " << endl; break; // erreur
}
}*/
}
int main()
{
int lines, columns;
cout << "Enter number of lines" << endl;
cin >> lines;
cout << "Enter number of columns" << endl;
cin >> columns;
Board tab(lines,columns);
Position pos(lines,columns);
Position& p = pos;
for (int i = 0; i<lines;i++)
{
for(int j = 0; j<columns;j++)
{
cout << tab.getValue(p) << " ";
if (i == lines) { cout << endl;}
}
}
return 0;
}
When I call getValue at line 139, I get a segmentation fault. Get value is defined at line 57. When executing getValue, both p.line and p.column got the right values caught at the beginning of main function.
The program got no errors, only 2 warnings because I don't use Play::moves arguments (because currently between /* */, waiting for tests). I use Code::Blocks with -Wall -Wextra -pedantic -std=c++11.
I really see no reason for a segmentation fault. Did I miss something?
You are calling get with a position that is set to the size of your board. Since arrays are 0 index based the size of your array is actually one past the end of the array.
const int size = 100
int arr[size]; //0, 1, 2, ... 98, 99
arr[size]; // fail arr is 0-99 and size is 100
Related
I am writing a code using classes and am getting the wrong output, this is my function definitions:
void PrintCard(int c)
{
int Rank = c%13;
int Suit = c/13;
const char NameSuit[5] = "SCDH";
const char NameRank[14] = "23456789XJQKA";
cout << NameRank[Rank] << NameSuit[Suit];
}
CardSet::CardSet()
{
Card = NULL;
nCards = 0;
}
CardSet::CardSet(int c)
{
Card = new int[c];
for(int i = 0; i > c; i++)
{
Card[i] = (i % 52);
}
}
CardSet::~CardSet()
{
delete[] Card;
}
bool CardSet::IsEmpty() const
{
return nCards == 0;
}
void CardSet::Print() const
{
for(int i=0; i > nCards; i++)
{
PrintCard(i);
}
}
int CardSet::Size() const
{
return nCards;
}
This is my main
cout << "Testing constructors, Print(), Size() & IsEmpty():" << endl;
CardSet CardSet1; // empty cCardSet
CardSet CardSet2(12); // CardSet with 12 cards
if(CardSet1.IsEmpty()) cout<<"CardSet1 is empty"<<endl;
else cout<<"CardSet1 has "<< CardSet1.Size() <<" cards" << endl;
if(CardSet2.IsEmpty()) cout<<"CardSet2 is empty"<<endl;
else cout<<"CardSet2 has "<< CardSet2.Size() <<" cards" << endl;
cout << "Printout of CardSet1: ";
CardSet1.Print();
cout << "Printout of CardSet2: ";
CardSet2.Print();
cout << endl;
when i am compiling i am getting the correct value (0) for cardset1 however for cardset2 instead of outputting a value of 12, which is what should be the output i am getting very high numbers that are changing each time i compile. i think something is wrong with my for loops or memory allocation.
this is also what the class definition looks like:
class CardSet
{
public:
CardSet();
CardSet(int);
~CardSet();
int Size() const;
bool IsEmpty() const;
void Shuffle();
int Deal();
void Deal(int,CardSet&,CardSet&);
void Deal(int,CardSet&,CardSet&,CardSet&,CardSet&);
void AddCard(int);
void MergeShuffle(CardSet&);
void Print() const;
private:
int* Card;
int nCards;
};
any help would be greatly appreciated !!
Cheers
In CardSet::CardSet change this
for(int i = 0; i > c; i++)
to this
for (int i = 0; i < c; i++)
Also in CardSet::Print change this
for(int i=0; i > nCards; i++)
To this:
for (int i = 0; i < nCards; i++)
Finally, add nCards = c; to CardSet::CardSet.
void CardSet::Print() const
{
for(int i=0; i > nCards; i++)
{
PrintCard(i);
}
}
must be
void CardSet::Print() const
{
for(int i=0; i < nCards; i++)
{
PrintCard(i);
}
}
to correct the end test, and you have the same problem in CardSet::CardSet(int c) which must be
CardSet::CardSet(int c)
{
nCards = c;
Card = new int[c];
for(int i = 0; i < c; i++)
{
Card[i] = (i % 52);
}
}
where nCards must also be set.
In a for the test indicates if the loop continues, not if it ends
for (inits; test; changes) ...
is equivalent to
init;
while (test) {
...
changes;
}
Out of that there is no separator in PrintCard doing cout << NameRank[Rank] << NameSuit[Suit]; so may be you also need to add something like a space in Print :
void CardSet::Print() const
{
for(int i=0; i < nCards; i++)
{
PrintCard(i);
cout << ' ';
}
}
or in PrintCard to also separate the two fields like
cout << NameRank[Rank] << ' ' << NameSuit[Suit] << endl;
Note you can simplify
const char NameSuit[5] = "SCDH";
const char NameRank[14] = "23456789XJQKA";
cout << NameRank[Rank] << NameSuit[Suit];
to be
cout << "23456789XJQKA"[Rank] << "SCDH"[Suit];
Or if you really want to have the arrays I encourage you to not give a size, that avoid problems if you change the literal string and forget to also change the size, so
const char NameSuit[] = "SCDH";
const char NameRank[] = "23456789XJQKA";
For instance having :
#include <iostream>
using namespace std;
class CardSet
{
public:
CardSet();
CardSet(int);
~CardSet();
int Size() const;
bool IsEmpty() const;
void Shuffle();
int Deal();
void Deal(int,CardSet&,CardSet&);
void Deal(int,CardSet&,CardSet&,CardSet&,CardSet&);
void AddCard(int);
void MergeShuffle(CardSet&);
void Print() const;
private:
int* Card;
int nCards;
};
void PrintCard(int c)
{
int Rank = c%13;
int Suit = c/13;
cout << "23456789XJQKA"[Rank] << ' ' << "SCDH"[Suit] << endl;
}
CardSet::CardSet()
{
Card = NULL;
nCards = 0;
}
CardSet::CardSet(int c)
{
nCards = c;
Card = new int[c];
for(int i = 0; i < c; i++)
{
Card[i] = (i % 52);
}
}
CardSet::~CardSet()
{
delete[] Card;
}
bool CardSet::IsEmpty() const
{
return nCards == 0;
}
void CardSet::Print() const
{
for(int i=0; i < nCards; i++)
{
PrintCard(i);
}
}
int CardSet::Size() const
{
return nCards;
}
int main(void)
{
CardSet cs(5);
cs.Print();
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra c.cc
pi#raspberrypi:/tmp $ ./a.out
2 S
3 S
4 S
5 S
6 S
pi#raspberrypi:/tmp $
You should review it (the loop)
void CardSet::Print() const
{
for(int i=0; i > nCards; i++)//## reconsider it
{
PrintCard(i);
}
}
I have Multidimensional array in my struct i want to work with this array using few methods, i think i should use double pointer but i have no idea how to do it
struct generator {
char r[26][max];
void set();
void display();
};
void generator::set() {
char *tab = new char[max];
int k = 0;
cin >> tab;
while (tab[k] != '\0') {
r[0][k] = tab[k];
k++;
}
void generator::display(){
cout << r[0][1]; // should display first letter of string
}
in
cout << r[0][1]; // should display first letter of string
the comment is wrong, that displays the second letter, first letter is at index 0
Adding enough definitions and missing '}' to compile and execute :
#include <iostream>
using namespace std;
#define max 10
struct generator {
char r[26][max];
void set();
void display();
};
void generator::set() {
char *tab = new char[max];
int k = 0;
cin >> tab;
while (tab[k] != '\0') {
r[0][k] = tab[k];
k++;
}
}
void generator::display(){
cout << r[0][1]; // should display first letter of string
}
int main()
{
generator g;
g.set();
g.display();
cout << endl;
}
Compilation and execution :
/tmp % g++ -pedantic -Wall -Wextra g.cc
vxl15036 /tmp % ./a.out
aze
z
I'm working on an assignment where I should write an encoding and decoding application for the Huffman algorithm, based on a priority queue. We have to read a file, count the frequencies of the letters and then start the algorithm. I have the following problem:
My counting function works fine but it stores the frequency of every letter in an array - even if it's zero. But if I want to use that array to build my min heap I get major problems because of the zeros. Therefore I need to find a way to 'eliminate' them. I can't just skip them because then the min heap algorithm doesn't work anymore (wrong neighbours). So I wanted to transfer all non-zero entries in a vector and use the vector instead of the array. But there I always get an error that tells me that there's a problem with the vector size. I don't really know how to deal with that problem. (My min heap still uses the array because I can't even transfer the entries in a vector).
(Please ignore the main I was just trying stuff there!)
using namespace std;
struct huffman_node
{ char data;
int frequency;
bool vector;
huffman_node *left;
huffman_node *right;
};
void swap_huffman_nodes(huffman_node &a, huffman_node &b)
{ char store_data = a.data;
int store_frequency = a.frequency;
a.data = b.data;
a.frequency=b.frequency;
b.data = store_data;
b.frequency = store_frequency;
huffman_node *store_left = a.left;
huffman_node *store_right= a.right;
a.left = b.left;
a.right = b.right;
b.left = store_left;
b.right = store_right;
}
void print_node (huffman_node a)
{ cout << a.data << a.frequency << endl;
}
string line;
huffman_node Table[52];
vector <huffman_node> non_zero;
void build_table()
{ for (int i=1; i<27; i++)
{ Table[i].data = (char) (i+64);
Table[i].left = NULL;
Table[i].right = NULL;
}
for (int i=27; i<53; i++)
{ Table[i].data = (char) (i+70);
Table[i].left = NULL;
Table[i].right = NULL;
}
}
int counter =0;
void count(){
ifstream yourfile ("example.txt");
if (yourfile.is_open())
{
while ( getline (yourfile,line) )
{
/*cout << line << '\n'; */
unsigned long z=line.length();
int i=0;
while ( i < z)
{ /* cout << line[i] << endl; */
for (int j=65; j<91; j++)
{ if ((int) line[i] == j)
{ int k=-64+j;
Table[k].frequency++;
}
}
for (int j=97; j<123; j++)
{ if ((int) line[i] == j)
{ int k=-70+j;
Table[k].frequency++;
}
}
i++;
}
}
for (int i=1; i<53; i++)
{ if (Table[i].frequency!=0)
{ non_zero.push_back(Table[i]);
counter ++;
}
}
yourfile.close();
}
else cout << "Unable to open file";
}
class heap{
public:
void buildheap()
{
for (int i=1; i<53; i++)
{reheap(i);
};
}
void reheap(int new_index)
{ int parent_index = new_index/2;
while (parent_index > 0 && Table[parent_index].frequency > Table[new_index].frequency)
{ swap_huffman_nodes(Table[parent_index], Table[new_index]);
parent_index=parent_index/2;
new_index=new_index/2;
}
};
void delete_root()
{ int non_null_entries=0;
for (int i=1; i<53; i++)
{ if (Table[i].frequency!=-1) {non_null_entries++;};
}
swap_huffman_nodes(Table[1],Table[non_null_entries]);
Table[non_null_entries].frequency=-1;
non_null_entries--;
rebuild_heap_root_deletion(1, non_null_entries);
}
void rebuild_heap_root_deletion(int new_root,int non_null_entries){
int n;
if (2 * new_root > non_null_entries){
return;
}
if (2 * new_root + 1 <= non_null_entries
&& Table[2*new_root+1].frequency < Table[2*new_root].frequency){
n = 2 * new_root + 1;
} else {
n = 2 * new_root;
}
if (Table[new_root].frequency > Table[n].frequency){
swap_huffman_nodes(Table[new_root], Table[n]);
rebuild_heap_root_deletion(n, non_null_entries);
}
}
void add_element(huffman_node new_heap_element)
{ for (int i=52; i>0;i-- )
{ if (Table[i].frequency==-1 && Table[i-1].frequency!=-1)
{ Table[i]=new_heap_element;
reheap(i);
break;
}
}
}
void print_Table()
{
for (int i=1; i<53; i++)
{ /*if (Table[i].frequency != -1) */
cout << Table[i].frequency << " , " << Table[i].data << endl;
}
}
bool empty_heap() // a heap is empty here if there are only "invalid huffman nodes" in it except the first one that contains all information.
{ for (int i=2; i < 53; i++)
{ if (Table[i].frequency!=-1)
{ return false;}
}
return true;
}
};
int main(){
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "Flori ist ein Koala.";
myfile << "";
myfile.close();
}
else cout << "Unable to open file";
build_table();
count();
heap allan;
cout << "\n";
allan.buildheap();
allan.print_Table();
int i=0;
/*while(i<500)
{
huffman_node base_1 = Table[1];
allan.delete_root();
huffman_node base_2 = Table[1];
allan.delete_root();
huffman_node parent;
parent.data = '/';
parent.frequency = base_1.frequency + base_2.frequency;
parent.left = &base_1;
parent.right = &base_2;
allan.add_element(parent);
i++;
}
return 0;
}
I've got a problem with creating an array of strings inside the object. I don't know how to patch it around so I'm asking for help. Here lies the problem:
main.h:
#pragma once
#include <iostream>
#include <conio.h>
#include <string>
class tab2D {
protected:
int width;
int height;
string **sTab;
int **iTab;
public:
tab2D();
tab2D(int x, int y, char c);
~tab2D();
tab2D(tab2D&t);
};
class chess: public tab2D {
public:
chess(int x, int y);
~chess();
chess(chess&c);
void init();
bool ifMove();
void show();
};
class matrix: public tab2D {
public:
matrix(int x, int y);
~matrix();
matrix(matrix&m);
};
The compiler says: syntax error : missing ';' before '*' about the line
string **sTab;
I assume that I can't make the dynamic array of strings and it makes further problems with processing this array.. Can you help me? :)
*UPDATE 1*Thanks, I forgot to add line
using namespace std;
Now it works, but I've got another problem.
#include "main.h"
using namespace std;
////// Konstruktor, konstruktor kopiujący oraz destruktor //////
chess::chess(int x = 8, int y = 8) : tab2D(x, y, 'c') {
init();
};
chess::chess(chess&c) {
chess(c.width, c.height);
};
chess::~chess() {
};
////// Metody //////
////// Uzupełnianie kolorów pól oraz rozstawianie figur i pionków //////
void chess::init() {
/// kolory pól: 0 - biały, 1 - czarny///
int last = 0;
for(int i = 0; i < this->height; ++i) {
for(int j=0; j < this->width; ++j) {
if(last = 0) {
this->sTab[i][j] = "1";
last = 1;
}
else if(last = 1) {
this->sTab[i][j] = "0";
last = 0;
}
}
if(last = 0)
last = 1;
else if(last = 1)
last = 0;
};
/// rozstawienie pionków ///
for(int i = 0; i < this->width; ++i) {
sTab[1][i] = sTab[1][i] + "0";
sTab[6][i] = sTab[6][i] + "a";
};
};
////// Wyświetlenie szachownicy //////
void chess::show() {
for(int i = 0; i < (this->height + 1); ++i) {
for(int j=0; j < (this->width + 1); ++j) {
if(i == 0 && j == 0)
cout << " ";
else if (i != 0 && j == 0) {
switch (i) {
case 1:
cout << "A ";
break;
case 2:
cout << "B ";
break;
case 3:
cout << "C ";
break;
case 4:
cout << "D ";
break;
case 5:
cout << "E ";
break;
case 6:
cout << "F ";
break;
case 7:
cout << "G ";
break;
case 8:
cout << "H ";
break;
default:
break;
}
}
else if (i == 0 && j != 0) {
cout << j << " ";
}
else {
cout << this->sTab[i-1][j-1] << " ";
}
}
cout << endl;
};
};
When I run the program, there is a breakpoint in the line
this->sTab[i][j] = "0";
I assume there is something wrong with making the array of strings but I don't understand why exactly there is a breakpoint and can't debug it.
UPDATE 2
Here is the code for tab.cpp:
#include "main.h"
using namespace std;
////// Konstruktor domyślny, konstruktor, konstruktor kopiujący oraz destruktor //////
tab2D::tab2D() {
};
tab2D::tab2D(int x, int y, char c) {
this->width = x;
this->height = y;
if (c == 'm') {
this->iTab = new int*[this->width];
for(int i=0;i<this->height;++i)
this->iTab[i] = new int[this->width];
}
else if (c == 'c') {
this->sTab = new string*[this->width];
for(int i=0;i<this->height;++i)
this->sTab[i] = new string[this->width];
}
else {
}
};
tab2D::tab2D(tab2D&t) {
tab2D(t.width, t.height, 't');
};
tab2D::~tab2D() {
for(int i=0;i<height;++i)
delete [] iTab[i];
delete [] iTab;
for(int i=0;i<height;++i)
delete [] sTab[i];
delete [] sTab;
};
You need to qualify names from the standard library:
std::string **sTab;
^^^^^
If you're doing what I think you're doing and allocating things with new, then you should consider using std::vector to deal with the quagmire of memory management issues you're about to encounter. If you really want to juggle pointers yourself for some reason, don't forget the Rule of Three.
UPDATE Your new problem might be because the copy constructor is horribly broken:
chess::chess(chess&c) {
chess(c.width, c.height);
};
This creates and destroys a temporary object, but doesn't initialise the object being constructed, leaving it in an invalid state. You probably don't want to declare a copy-constructor at all, as long as the base class is correctly copyable. If you did need one, it should should be more like:
chess::chess(chess const & c) : // 'const' so constant objects can be copied
tab2D(c) // copy the base-class subobject
{
// do whatever else needs doing
}
Alternatively, the new problem might be due to errors in the tab2D constuctors which you haven't shown us. The best way to track it down is to step through the program with a debugger, checking that everything is correctly initialised before use.
UPDATE Probably, the runtime error is caused by allocating the wrong number of pointers. You want
iTab = new int*[height]; // Not width
and likewise for sTab.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
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;
}
}
}
}
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.
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.