// Muhammad Rafiy Athalla   A19EC0267
// Nibras Hanif Kusuma      A19EC0274
// assignment 2


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#define MAX 100
using namespace std;

void displayMenu();
void countWords();
void calcElecRest();
void calc_Q(float i, float &q);

void displayMenu()
{
	int choice;
	
	cout << "::MID TERM TEST SYSTEM MENU::"<<endl;
	cout << "   (1) Words count calculator"<<endl;
	cout << "   (2) Electric charge calculator"<<endl;
	cout << "   (3) Quit"<<endl;
	
	cout << "Your choice >> ";
	cin >> choice;
	cin.ignore();
	
	if(choice!=1&&choice!=2)
	{
		exit;
	}
	do
	{
		if(choice == 1)
		{
			countWords();
		}
		else if(choice == 2)
		{
			calcElecRest();
		}
		
		cout << "::MID TERM TEST SYSTEM MENU::"<<endl;
		cout << "   (1) Words count calculator"<<endl;
		cout << "   (2) Electric charge calculator"<<endl;
		cout << "   (3) Quit"<<endl;
	
		cout << "Your choice >> ";
		cin >> choice;
		cin.ignore();
	
		if(choice!=1&&choice!=2)
		{
			exit;
		}
	}
	while (choice == 1 || choice == 2);
	
}


void countWords()
{
	char str[MAX];
	int numWords=1, i=0, j=0;
	
	cout << "\n\n::WORDS COUNT CALCULATOR::" <<endl<<endl;
	
	cout << "Enter a string: ";
	cin.getline(str, MAX);
	
	for(i=0;str[i]; i++)
	{
		if(isupper(str[i]))
		{
			str[i]=tolower(str[i]);
		}
	}

    cout << "Words " << numWords << " << ";
    for(i=0; str[i] != '\0'; i++)
    {
        if(isspace(str[i]))
        {
            numWords++;
        	cout << "\n";
            cout << "Words " << numWords << " << ";
		}
		else if(isalpha(str[i]))
		{
			cout << str[i];
		}
    }
	
	cout << endl << endl;
	
	
	
}

void calcElecRest()
{
	float P, V, I, Q=0;
	
	
	cout << "\n\n::ELECTRIC CHARGES CALCULATOR::"<<endl<<endl;
	cout << "Please enter the power in Watts"<<"\nand the voltage in kilovolts\n";
	
	cout << "    Power (P)    = ";
	cin >> P;
	cout << "    Voltage (kV) = ";
	cin >> V;
	
	while(P<0 || V<0)
	{
		cout << "Invalid input. Please try again !" <<endl;
		cout << "    Power (P)    = ";
		cin >> P;
		cout << "    Voltage (kV) = ";
		cin >> V;
	}
	
	V *=1000;
	I=P/V;

	calc_Q(I, Q);
	
	cout << "Power, P = "<<P<<" watts"<<endl;
	cout << "Voltage, V = "<<V<<" volts"<<endl;
	cout << "Electric current, I = "<<I<< " amperes"<<endl;
	cout << "Electric charge, Q = "<<Q<< " coulombs"<<endl<<endl<<endl;
	
	
	
}

void calc_Q(float i, float &q)
{
	float t, tb;
	
	cout << "\n\nEnter the time of current flow, t in minutes =  ";
	cin >> t;
	
	tb=t*60;
	
	q= i*tb;
}

int main()
{
	displayMenu();
	
	return 0;
}
