The Partridge Family were neither partridges nor a family. Discuss.
-
Yumtard
- Posts: 575
- Joined: January 19th, 2017, 10:28 pm
- Location: Idiot from northern Europe
Post
by Yumtard » June 26th, 2017, 3:50 pm
Almost works now, but there's a bug and also the code is a mess...
Gonna try to fix it a bit later and then move on to the hard part (load and save)
Code: Select all
class UserEntry
{
public:
void Add(char* name_in, int val)
{
for (int i = 0; *name_in != 0; name_in++, i++)
{
name[i] = *name_in;
}
name[13] = 0;
value = val;
}
void Print()
{
chili::print(name);
chili::print(" |");
for (int i = 0; i < value; i++)
{
_putch('=');
}
_putch('\n');
}
private:
int value;
char name[13];
};
int main()
{
char choice;
int curEntry = 0;
static constexpr int maxEntry = 100;
UserEntry entry[maxEntry];
int value;
static constexpr int nameMaxSize = 12;
static constexpr int valMaxSize = 8;
char nameBuf[nameMaxSize];
char valBuf[valMaxSize];
do
{
chili::print("(l)oad, (s)ave, (a)dd (q)uit or (p)rint?\n\n");
choice = _getch();
switch (choice)
{
case 'l':
chili::print("Enter filename: ");
break;
case 's':
chili::print("Enter filename: ");
break;
case 'a':
chili::print("\nEnter name: ");
chili::read(nameBuf, nameMaxSize);
chili::print("\nEnter a value: ");
chili::read(valBuf, valMaxSize);
value = chili::str2int(valBuf);
entry[curEntry].Add(nameBuf, value);
curEntry++;
chili::print("\n\n");
break;
case 'p':
chili::print("\tBeautiful Chart Bitches!\n");
chili::print("\t------------------------\n");
for (int i = 0; i < curEntry; i++)
{
entry[i].Print();
}
break;
case 'q':
break;
default:
break;
}
} while (choice != 'q');
while( !_kbhit() );
return 0;
}
-
albinopapa
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Post
by albinopapa » June 26th, 2017, 4:44 pm
I didn't follow along with the previous videos so I don't have all the utility functions. I did however try this homework using the STL, just to see what it'd look like. Most everything in the Database and Entry classes, were needed, so the only real savings were the utility functions prior to this lesson.
Chili really knows what he's doing, for all you following along, keep following.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
-
Yumtard
- Posts: 575
- Joined: January 19th, 2017, 10:28 pm
- Location: Idiot from northern Europe
Post
by Yumtard » June 26th, 2017, 10:24 pm
yo it works! (kinda)
I'm more confused now than when I started tho and the code loks like garbage
edit: my prints aren't aligned tho
-
Yumtard
- Posts: 575
- Joined: January 19th, 2017, 10:28 pm
- Location: Idiot from northern Europe
Post
by Yumtard » June 26th, 2017, 11:08 pm
Code: Select all
#include <conio.h>
#include <fstream>
namespace chili
{
void print(const char* s)
{
for (; *s != 0; s++)
{
_putch(*s);
}
}
void read(char* buf, int maxSize)
{
const char* const pEnd = buf + maxSize;
for (char c = _getch(); c != 13 && (buf + 1 < pEnd); c = _getch(), buf++)
{
_putch(c);
*buf = c;
}
*buf = 0;
}
int str2int(const char* s)
{
// scan to start point
const char* p = s;
for (; *p >= '0' && *p <= '9'; p++);
p--;
int val = 0;
int place = 1;
// convert place values and accumulate
for (; p >= s; p--)
{
val += (*p - '0') * place;
place *= 10;
}
return val;
}
int fib(int n)
{
if (n < 2)
{
return n;
}
return fib(n - 1) + fib(n - 2);
}
void strrev(char* pl)
{
// scan to start point
char* pr = pl;
for (; *pr != 0; pr++);
pr--;
for (; pl < pr; pl++, pr--)
{
const char temp = *pl;
*pl = *pr;
*pr = temp;
}
}
void int2str(int val, char* buf, int size)
{
char* const pStart = buf;
char* const pEnd = buf + size;
for (; val > 0 && (buf + 1 < pEnd); val /= 10, buf++)
{
*buf = '0' + val % 10;
}
*buf = 0;
strrev(pStart);
}
}
void Load(char* fileName)
{
std::ifstream in(fileName);
}
class UserEntry
{
public:
void Add(char* name_in, int val)
{
for (int i = 0; *name_in != 0; name_in++, i++)
{
name[i] = *name_in;
endName = i + 1;
name[endName] = 0;
}
value = val;
}
void Print()
{
chili::print(name);
/*
//align
for (int i = endName; i < 12; i++)
{
_putch(' ');
}
*/
chili::print("|");
for (int i = 0; i < value; i++)
{
_putch('=');
}
_putch('\n');
}
void Save(char* fileName, std::ofstream& Out)
{
Out.write(name, sizeof(name));
Out.write(reinterpret_cast<char*>(&value), sizeof(value));
}
void Load(char* fileName, std::ifstream& In)
{
In.read(name, sizeof(name));
In.read(reinterpret_cast<char*>(&value), sizeof(value));
}
private:
int value;
char name[12];
int endName;
};
int main()
{
char choice;
int curEntry = 0;
static constexpr int maxEntry = 100;
UserEntry entry[maxEntry];
int value;
static constexpr int nameMaxSize = 12;
static constexpr int valMaxSize = 8;
char nameBuf[nameMaxSize];
char valBuf[valMaxSize];
char fileBuf[10];
std::ifstream in;
std::ofstream out;
char buf[256];
do
{
chili::print("(l)oad, (s)ave, (a)dd (q)uit or (p)rint?\n\n");
choice = _getch();
switch (choice)
{
case 'l':
chili::print("Enter filename: ");
chili::read(buf, 256);
curEntry = 0,
in = std::ifstream(buf, std::ios::binary);
in.read(reinterpret_cast<char*>(&curEntry), sizeof(int));
for (int i = 0; i < curEntry; i++)
{
entry[i].Load(buf, in);
}
break;
case 's':
chili::print("Enter filename: ");
chili::read(fileBuf, 10);
out = std::ofstream(fileBuf,std::ios::binary);
out.write(reinterpret_cast<char*>(&curEntry), sizeof(int));
for (int i = 0; i < curEntry; i++)
{
entry[i].Save(fileBuf, out);
}
break;
case 'a':
chili::print("\nEnter name: ");
chili::read(nameBuf, nameMaxSize);
chili::print("\nEnter a value: ");
chili::read(valBuf, valMaxSize);
value = chili::str2int(valBuf);
entry[curEntry].Add(nameBuf, value);
curEntry++;
chili::print("\n\n");
break;
case 'p':
chili::print("\tBeautiful Chart Bitches!\n");
chili::print("\t------------------------\n");
for (int i = 0; i < curEntry; i++)
{
entry[i].Print();
}
break;
case 'q':
break;
default:
break;
}
} while (choice != 'q');
while( !_kbhit() );
return 0;
}
-
albinopapa
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Post
by albinopapa » June 27th, 2017, 2:28 am
Just FYI, when your doing a constexpr outside of a class declaration, you don't have to have static before it.
instead of:
Code: Select all
main()
{
static constexpr int nameMaxSize = 12;
static constexpr int valMaxSize = 8;
}
you can just do
Code: Select all
main()
{
constexpr int nameMaxSize = 12;
constexpr int valMaxSize = 8;
}
The constexpr class members are static because it wouldn't make sense to have literals like 12 and 8 be take up memory for each instance of an object you create from that class. The compiler will just replace the var name with the constant expression like 12 or 8 for instance.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
-
Yumtard
- Posts: 575
- Joined: January 19th, 2017, 10:28 pm
- Location: Idiot from northern Europe
Post
by Yumtard » June 27th, 2017, 12:00 pm
^ interesting, didn't know that
Today I might try to clean up the code/make it better and then do the actual solution... Or just do the actual solution right away.
-
chili
- Site Admin
- Posts: 3948
- Joined: December 31st, 2011, 4:53 pm
- Location: Japan
-
Contact:
Post
by chili » June 28th, 2017, 4:21 am
Good stuff yumtard. Lot's of comments I want to give but it will have to wait until I got some time. In the meanwhile, keep it up bro.
Chili
-
Yumtard
- Posts: 575
- Joined: January 19th, 2017, 10:28 pm
- Location: Idiot from northern Europe
Post
by Yumtard » June 28th, 2017, 10:18 am
-
ceofil
- Posts: 39
- Joined: April 13th, 2017, 8:35 pm
Post
by ceofil » June 28th, 2017, 10:29 am
-
Zedtho
- Posts: 189
- Joined: February 14th, 2017, 7:32 pm
Post
by Zedtho » June 28th, 2017, 1:16 pm
Holy shit! That's freaking awesome! Congratz!