Demo entry 6636092

Base Code For App

   

Submitted by Dan Celestin on Aug 23, 2017 at 03:45
Language: C++. Code size: 4.1 kB.

// DaFoRa.cpp : Defines the entry point for the console application.
// The AI living in 500 B.C.

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <string>
#include <fstream>
#include <ctime>
#include <Windows.h>
using namespace std;


int main()
{
	ifstream GetName ("More Data/UserName.txt", ifstream::in);
	string InfileName;
	getline(GetName, InfileName);
	GetName.close();

	ifstream GetAge("More Data/UserAge.txt", ifstream::in);
	string InfileAge;
	getline(GetAge, InfileAge);
	GetAge.close();

	if (InfileName != "" || InfileAge != "") {

		cout << "I recognize you! You are " << InfileName << "! I also happen to know that you are " << InfileAge << " years old!" << endl;

	}
	else {

		//Preps the UserName file for saving data
		fstream LoadName;
		LoadName.open("More Data/UserName.txt");

		// DaFoRa greets and asks your name and saves it to file
		cout << "Hello there, I am DaFoRa. What is your name?" << endl;
		char UserName[65];
		cin.getline(UserName, 65);
		LoadName << UserName;
		LoadName.close();


		// Preps the UserAge file for saving data
		fstream LoadAge;
		LoadAge.open("More Data/UserAge.txt");

		// It then ask your age and saves it to file
		cout << "Hello " << UserName << ", and how old are you?" << endl;
		char UserAge[6];
		cin.getline(UserAge, 7);
		LoadAge << UserAge;
		LoadAge.close();


		// Then concluding Statement and AI task
		cout << "Ahh, " << UserAge << ", that's a nice age to be. Isn't it, " << UserName << "?" << endl;
	};
	
	// Any AI function must be in this loop below
	for (;;) {
		
		// This will recall the name from file
		ifstream GetName;
		GetName.open("More Data/UserName.txt", ifstream::in);
		string UserName;
		getline (GetName,UserName);

		// Now Starts The Assitant part (it's looped)
		cout << "Ok, now tell me what you want me to do for you today, " << UserName << ". I can do all 4 basic operations, and I can tell time! Type close if you want me to go away :(. Thats all I can do for now!" << endl;
		string UserTask;
		getline (cin, UserTask);
		transform (UserTask.begin(), UserTask.end(), UserTask.begin(), ::tolower);

		// Addition Code
		if (UserTask == "add" || (UserTask == "addition")) {
			cout << "Now tell me the two numbers you'd like to add." << endl;
			int a;
			int b;
			cin >> a >> b;
			cout << "The answer is: " << a + b << endl;
			cin.ignore();
			
		}

		// Subtraction Code
		else if (UserTask == "subtract" || ( UserTask == "subtraction")) {
			cout << "Now tell me the two numbers you'd like to subtract." << endl;
			int a;
			cin >> a;
			int b;
			cin >> b;
			cout << "The answer is: " << a - b << endl;
			cin.ignore();

		}

		// Multiplication Code
		else if (UserTask == "multiply" || (UserTask == "multiplication")) {
			cout << "Now tell me the two numbers you'd like to multiply." << endl;
			int a;
			cin >> a;
			int b;
			cin >> b;
			cout << "The answer is: " << a * b << endl;
			cin.ignore();
		}

		// Division Code
		else if (UserTask == "divide" || (UserTask == "division")) {
			cout << "Now tell me the two numbers you'd like to divide." << endl;
			int a;
			cin >> a;
			int b;
			cin >> b;
			cout << "The answer is: " << a / b << endl;
			cin.ignore();
		}

		//This closes the program on enter/return key press
		else if (UserTask == "close") {
			cout << "It was nice talking to you in this demo, " << UserName << ". Now press the enter key and I'll whisk away!" << endl;
			cin.ignore();
			cout << "Adios, " << UserName << "." << endl;
			Sleep(1000);
			return 0;
		}

		//Allows DaFoRa to relay the current system time and date
		else if (UserTask == "time") {
			time_t now = time(0);
			char* dt = ctime(&now);
			cout << "The current system time is: "<< dt << endl;
		}

		// User didn't eneter a function DaFoRa could do
		else {
			cout << "I don't understand that yet!" << endl;
			Sleep(1000);
		};


		
	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).