I am very (very) new to programming, and today I (semi-)successfully wrote my first program that performs very basic things.
My hope is that I can receive some constructive criticism around how I structured it all and if I am doing things in the most efficient and proper way possible for what I am trying to achieve.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
void calculator();
void birthcal();
void convert();
void readrecorddata();
void recorddata();
void readdata();
int main()
{
for ( int choice = 0;; )
{
cout << "Please choose one of the following:\n1. Common Calculator\n2. Birth Date Calculator\n3. Convert units of measurement\n4. Read/ Record data to file\n5. Convert Celsius to Fahrenheit\n\nChoice: ";
cin >> choice;
cout << endl;
if (choice == 1)
{
calculator();
break;
}
if (choice == 2)
{
birthcal();
break;
}
if (choice == 3)
{
convert();
break;
}
if (choice == 4)
{
readrecorddata();
break;
}
if (choice == 5)
{
temperature();
break;
}
else
{
cout << "You have made a mistake, please try again. \n";
cin.clear();
cin.get();
system("PAUSE");
continue;
}
return 0;
}
}
void temperature()
{
int celsius;
cout << "Enter the temperature in Celsius: ";
cin >> celsius;
int factor;
factor = 212 - 32;
int fahrenheit;
fahrenheit = factor * celsius/100 + 32;
cout << "Fahrenheit value is: ";
cout << fahrenheit << endl;
system("PAUSE");
}
void calculator()
{
int number1, number2, result;
for ( ;; )
{
cout << "Welcome to Calculator" << endl;
cout << "Valid operators are +, -, x, /" << endl;
cout << "Please enter an equation: ";
char sign(1);
cin >> number1 >> sign >> number2;
if (sign == '+')
{
result = number1 + number2;
break;
}
if (sign == '-')
{
result = number1 - number2;
break;
}
if (sign == 'x')
{
result = number1 * number2;
break;
}
if (sign == '/')
{
result = number1 / number2;
break;
}
else
{
cout << "You have made a mistake, please try again. \n";
cin.clear();
cin.get();
system("PAUSE");
continue;
}
}
}
void birthcal()
{
int age, birthdate, year;
year = 2014;
for ( ;; )
{
cout << "Welcome to the Birth Year calculator" << endl;
cout << "Please enter your age to determine your birth year: ";
cin >> age;
if ( age > 0 && age < 200 )
{
birthdate = year - age;
cout << "\nYou were likely born in the year " << birthdate << " or " << birthdate-- << endl;
break;
}
else
{
cout << "Invalid age, please try again \n" << endl;
cin.clear();
cin.get();
system("PAUSE");
continue;
}
}
}
void convert()
{
int convertchoice;
for ( convertchoice = 0;; )
{
cout << "Welcome to the Units Converter \nPlease pick:\n\n1.Centimeters to Inches \n2.Inches to Centimeters\n\nChoice: ";
cin >> convertchoice;
if (convertchoice == 1)
{
int cm, in, factor = 2.54;
cout << "Please enter number in Centimeters: ";
cin >> cm;
in = cm / factor;
cout << "The result is: " << in << endl;
system("PAUSE");
break;
}
if (convertchoice == 2)
{
int cm, in, factor = 2.54;
cout << "Please enter the number in Inches: ";
cin >> in;
cm = in * factor;
cout << "The result is: " << cm << endl;
break;
}
else
{
cout << "You have made a mistake, please try again. \n";
cin.clear();
cin.get();
system("PAUSE");
continue;
}
}
}
void readrecorddata()
{
for ( int choice = 0;; )
{
ofstream myfile;
cout << "Welcome to Read/ Record Data \n Please select: \n\n1. Record Data\n\n2. Read Data\n\nChoice: ";
cin >> choice;
if (choice == 1)
{
recorddata();
break;
}
if (choice == 2)
{
readdata();
break;
}
else
{
cout << "You have made a mistake, please try again. \n";
cin.clear();
cin.get();
system("PAUSE");
continue;
}
}}
void recorddata()
{
char name[10], location[15], colour[10], fruit[10];
int age;
cout << " - Recording Data - \n\n";
ofstream myfile;
myfile.open ("info.txt");
if (myfile.is_open())
{
cout << "Your Name:\n\n";
cin.getline (name, 10);
cin >> name;
myfile << "Name: " << name << endl;
cout << "\nYour Age:\n\n";
cin >> age;
myfile << "Age: " << age << endl;
cout << "\nCountry:\n\n";
cin.getline(location, 15);
cin >> location;
myfile << "Location: " << location << endl;
cout << "\nYour favourite colour:\n\n";
cin.getline(colour, 10);
cin >> colour;
myfile << "Favourite Colour: " << colour << endl;
cout << "\nYour favourite fruit:\n\n";
cin.getline(fruit, 10);
cin >> fruit;
myfile << "Favourite Fruit: " << fruit << endl;
cout << "/n/nThank You for entering the information";
system("PAUSE");
}
}
void readdata()
{
cout << " - Reading Data - \nNow we will read data from the file.\n\n";
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line))
{
cout << line << '\n';
}
}
}