Page 44 of 68

Re: Noob learns to code in 3 months

Posted: June 26th, 2017, 3:50 pm
by Yumtard
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;
}

Re: Noob learns to code in 3 months

Posted: June 26th, 2017, 4:44 pm
by albinopapa
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.

Re: Noob learns to code in 3 months

Posted: June 26th, 2017, 10:24 pm
by Yumtard
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

Re: Noob learns to code in 3 months

Posted: June 26th, 2017, 11:08 pm
by Yumtard

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;
}

Re: Noob learns to code in 3 months

Posted: June 27th, 2017, 2:28 am
by albinopapa
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.

Re: Noob learns to code in 3 months

Posted: June 27th, 2017, 12:00 pm
by Yumtard
^ 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.

Re: Noob learns to code in 3 months

Posted: June 28th, 2017, 4:21 am
by chili
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.

Re: Noob learns to code in 3 months

Posted: June 28th, 2017, 10:18 am
by Yumtard
YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAS

I got accepted to the school!!!!

Image

Image

Image

Image
Image

Image
Image

Re: Noob learns to code in 3 months

Posted: June 28th, 2017, 10:29 am
by ceofil
Congrats man!!! I'm so happy for you :D :D :D :D :D

Re: Noob learns to code in 3 months

Posted: June 28th, 2017, 1:16 pm
by Zedtho
Holy shit! That's freaking awesome! Congratz!