1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* Name
Assignment:
Date Written:
Course:
Program:a02.cpp
Purpose: to code a program to calculate warp speed
Sources:
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstring>
const int S_SIZE = 256;
const int PSIZE = 20;
using namespace std;
struct planet {
int currentSystem;
char name[S_SIZE];
double distance; /*planet struct*/
double travelDays;
};
int loadDataFromFile(planet planet[], char fName[])
{
char fileName[256];
ifstream inputFile;
int count = 0;
int currentSystem = 0;
cout << "Please inculde the file extension also ie:(.txt)" << endl;
cout << "What is the file name you would like to open?: ";
cin.get(fileName, S_SIZE);
strcpy(fName, fileName);
inputFile.open(fileName);
if (!inputFile.is_open()) //Failed to open OH NOES!
return currentSystem; // end of the function
while (!inputFile.eof())
{
inputFile.get(planet[currentSystem].name, S_SIZE, '\t'); // Read the system name
inputFile >> planet[currentSystem].distance;
inputFile.ignore();
currentSystem++;
}
inputFile.close();
return currentSystem;
}
int LoadDataFromFile(planet planet[]);
bool searchForASystem(char* filename, planet planet[])
{
char path[S_SIZE];
char systemName[S_SIZE];
int currentSystem = 0;
cout << "test";
cout << "Please inculde the file extension also ie:(.txt)" << endl;
cout << "What is the file name you would like to open?: ";
cin.get(path, S_SIZE);
cin.ignore();
ifstream file(path);
if (file.is_open())
{
cout << "File has been opened!";
cout << "Please enter the system you would like to find: ";
cin.get(systemName, S_SIZE);
cin.ignore();
if (systemName == planet[currentSystem].name)
{
cout << "\nSystem number: " << currentSystem << endl;
cout << "Name: " << planet[currentSystem].name << endl;
cout << "Distance from star: " << planet[currentSystem].distance << " " << "million miles" << endl;
planet[currentSystem].travelDays = planet[currentSystem].distance / pow(4, 3) * 365;
cout << "\nTravel time in warp 4: " << planet[currentSystem].travelDays << "Days";
}
}
else
{
cout << "Error: File could not be found.";
}
return 0;
}
int main()
{
ofstream outputFile;
planet planet[PSIZE];
char fileName[256];
int planetNum = 0;
char choice;
int currentSystem = 0;
int searchSystem = 0;
int count = 0;
cout << fixed << setprecision(2) << showpoint;
currentSystem = loadDataFromFile(planet, fileName);
if (currentSystem == 0)
{
cout << " Sorry Couldn't load any data." << endl;
system("pause");
return 0;
}
cout << "systems read: " << currentSystem << endl;
fstream file(fileName, fstream::app);
do {
cout << "\n**********************************" << endl;
cout << "Welcome to Chad's system map" << endl;
cout << "Here are your following options." << endl;
cout << "a. Add a system" << endl;
cout << "s. select a system" << endl; /*Main Menu*/
cout << "l. list all system" << endl;
cout << "q. To quit the system." << endl;
cout << "Please use all lower case during the program" << endl;
cout << "**********************************" << endl;
cout << "please enter your choice: ";
cin >> choice;
cin.ignore(); // Added ignore, to get rid of '\n' stuck in the buffer.
switch (choice) {
case 'a':
case 'A':
if (currentSystem < PSIZE)
{
cout << "Add a system to the database." << endl; /*Adding a planet*/
cout << "\n Please enter the name of the system: ";
cin.get(planet[currentSystem].name, S_SIZE);
cout << "\n Please enter the planets distance from its star in millions of miles: ";
cin >> planet[currentSystem].distance;
file << '\n' << planet[currentSystem].name << '\t' << planet[currentSystem].distance;
cin.ignore();
currentSystem++;
break;
}
else
cout << "Sorry, no more room left in the system.";
break;
case 's':
case 'S':
if (currentSystem > 0)
{
searchForASystem(fileName, planet);
break;
}
else
cout << "sorry no Systems have been added in the database yet" << endl;
break;
case 'l':
case 'L':
if (currentSystem > 0) {
cout << "List the systems." << endl;
for (int i = 0; i < currentSystem; i++) /*list all of the systems in the database*/
{
cout << "\nSystem number: " << i << endl;
cout << "Name: " << planet[i].name << endl;
cout << "Distance from star: " << planet[i].distance << " " << "million miles" << endl;
planet[i].travelDays = planet[i].distance / pow(4, 3) * 365;
cout << "\nTravel time in warp 4: " << planet[i].travelDays << "Days";
count++;
}
}
else
cout << "No systems have been added to the database" << endl;
break;
case 'q':
case 'Q':
cout << "Thank you for using Chad's system database." << endl;
cout << "Enjoy the rest of your day." << endl;
file.close();
system("pause");
return 0;
break;
}
} while (choice != 'q');
}
|