Code: Select all
#include "stdafx.h"
#include <conio.h>
#include <fstream>
//Global variables
int index = 0;
int nameIndex = 0;
int ageIndex = 0;
//Names and ages
char names[10][10];
char ages[10][5];
namespace tomato
{
void print(char* readChar)
{
for (; *readChar != 0; readChar++)
{
_putch(*readChar);
}
}
void read(char* readChar)
{
for (char c = _getch(); c != 13; c = _getch(), readChar++)
{
_putch(c);
*readChar = c;
}
*readChar = 0;
}
int str2int(char* pLeft)
{
char* pRight = pLeft;
for (; *pRight >= '0' && *pRight <= '9'; pRight++);
pRight--;
int sum = 0;
int multiplier = 1;
for (; pLeft <= pRight; pRight--)
{
sum += (*pRight - '0') * multiplier;
multiplier *= 10;
}
return sum;
}
void strrev(char* pl)
{
char* pr = pl;
for (; *pr != 0; pr++);
pr--;
for (; pl < pr; pl++, pr--)
{
char temp = *pl;
*pl = *pr;
*pr = temp;
}
}
void int2str(char* buf, int val, int size)
{
char* const bufStart = buf;
char* pEnd = buf + size;
for (; (buf + 1) < pEnd && val > 0; val /= 10, buf++)
{
*buf = '0' + val % 10;
}
*buf = 0;
strrev(bufStart);
}
}
using namespace tomato;
void Load()
{
//Ask the user to enter the file they want to load. Has to enter whole filename + extension
char load[15];
print("Enter the file you want to load: ");
read(load);
print("\n");
std::ifstream in(load);
//Put characters from file to screen
for(char c = in.get(); in.good(); c = in.get())
{
_putch(c);
}
}
void Save()
{
//Ask the user to enter the name of the file they want to save to. Has to enter whole filename + extension
print("Enter the name of the file you want to save to: ");
char save[15];
read(save);
std::ofstream out(save);
//Print names and ages in pair by global index
for (int i = 0; i < index; i++)
{
//Print the names to the save file
for (int j = 0; j < sizeof(names[i]); j++)
{
out.put(names[i][j]);
}
out.put(32);
//Print the ages to the save file
for (int k = 0; k < sizeof(ages[i]); k++)
{
out.put(ages[i][k]);
}
out.put(10);
}
}
void Add()
{
//Reset the nameIndex and ageIndex every time we add new names and ages
nameIndex = 0;
ageIndex = 0;
//Add name
char addName[10];
print("Enter a name: ");
read(addName);
//Copy the added name char by char into the global name array
for ( ; addName[nameIndex] != 0; nameIndex++)
{
names[index][nameIndex] = addName[nameIndex];
}
print("\n");
//Add age
char addAge[4];
print("Enter age: ");
read(addAge);
//Copy the added age char by char into the global age array
for (; addAge[ageIndex] != 0; ageIndex++)
{
ages[index][ageIndex] = addAge[ageIndex];
}
//Increase index of name and age by one
index++;
}
void Quit()
{
}
void Print()
{
//Print the names and ages
for (int i = 0; i < index; i++)
{
print(names[i]);
print(" ");
print(ages[i]);
print("\n");
}
}
void Menu()
{
print("\n(l)oad (s)ave (a)dd (q)uit (p)rint\n");
//Get input from user and take action on it
char input = _getch();
switch (input)
{
case 'l':
Load();
Menu();
break;
case 's':
Save();
Menu();
break;
case 'a':
Add();
Menu();
break;
case 'q':
print("Press any key to quit...");
break;
case 'p':
Print();
Menu();
break;
default:
print("Invalid input");
}
}
int main()
{
Menu();
while (!_kbhit());
return 0;
}