These are my instrunctions for the program: Your program should use stars.dat as
ID: 3673103 • Letter: T
Question
These are my instrunctions for the program: Your program should use stars.dat as a data file for input and output. The solution program uses solution_stars.dat as a data file. stars.dat is formatted to contain information as follows, separated by commas: star name,star's constellation,star's apparent magnitude,star's mass, star's diameter,next star's name, next star's constellation.... I will always give you a file that contains information in these 5 value pieces, so a given star will always be followed by a value for constellation, apparent magnitude, mass and then diameter. Despite the fact that some of these values are numeric, it would probably be easiest to use strings as variables as you will not need to do any math. You do not have to test or worry about what happens if you start with a badly formed file. You DO have to ensure that you don't write to the file in such a way that it becomes malformed. This program will, on open, display a list of names gathered from a CSV file. The user will then select a name from that list, or "Add" or "Quit". The names may have spaces in them. If Quit is selected the program will end. If a name is selected, then details about that star will be printed, and then the menu will be reprinted. If Add is selected the program will prompt the user to enter information about a new star. Note! You must prohibit the user from entering the star's name as Add Quit or any name that is already in the file as a star name. Also! The star information must be added to the file, so that it will persist and be present the next time the program is run!. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is what I have so far ( I think all I need to do is write the functions, which is where Im having issues):
-> I need help writing the functions: PrintMenu, IsStarNameValid, AddStarToFileFromUserInput and PrintStarFacts.
using namespace std const int STAR FACTS = 5 const int PREF WIDTH = 23 10 void PrintMenu( string fileName) bool IsStarNameValid(string fileName, string starName) void AddStarToFileFromUserInput (string fileName) void PrintStarFacts(string fileName, string starName) 12 13 14 15 16 17 18 19 20 21 int main bool runP rogram = true; string fileName string fileName"stars. dat" "s string userSelection; while( runProgram) PrintMenu( fileName) coutExplanation / Answer
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int STAR_FACTS = 5;
const int PREF_WIDTH = 23;
void PrintMenu(std::string fileName);
bool IsStarNameValid(std::string fileName, std::string starName);
void AddStarToFileFromUserInput(std::string fileName);
void PrintStarFacts(std::string fileName, std::string starName);
int main(){
std::string name;
bool runProgram = true;
std::string fileName = "stars.dat";
std::string userSelection;
while (runProgram){
system("cls");
PrintMenu(fileName);
cout << "Enter Star Name, or Add, or Quit: ";
getline(std::cin, userSelection);
if (userSelection == "Add"){
AddStarToFileFromUserInput("stars.dat"); //"solution_stars.dat"
}
else
if (userSelection == "Quit"){
runProgram = false;
}
else{
if (IsStarNameValid(fileName, userSelection)){
PrintStarFacts(fileName, userSelection);
}
else{
cout << "Invalid Star Name ";
}
}
}
system("pause");
return 0;
}
void PrintMenu(std::string fileName){
cout << "Welcome the Program Star" << endl;
cout << endl;
cout << "Select a name from that list " << endl;
cout << "'Add' add new Star" << endl;
cout << "'Quit' quit the program" << endl;
cout << endl;
}
bool IsStarNameValid(std::string fileName, std::string starName){
bool b=true;
char *name = new char[starName.length() + 1]; // or
std::strcpy(name, starName.c_str());
char line[256];
ifstream fe("star.dat");
while(!fe.eof()) {
fe.getline(line, 128);
if (strstr(line, name)!=NULL){
b=false;
break;
}
}
fe.close();
return b;
}
void AddStarToFileFromUserInput(std::string fileName){
ofstream fs("stars.dat");
std::string starName, starConstellation, starMagnitude, starMass, starDiameter;
while (true){
cout << "Enter Star's Name: ";
getline(std::cin, starName);
if (starName != "Add" && starName != "Quit")
break;
}
cout << "Enter Star's Constellation: ";
getline(std::cin, starConstellation);
cout << "Enter Star's Magnitude: ";
getline(std::cin, starMagnitude);
cout << "Enter Star's Mass: ";
getline(std::cin, starMass);
cout << "Enter Star's Diameter: ";
getline(std::cin, starDiameter);
// output file:
fs << starName + ", " + starConstellation+", " + starMagnitude + ", " + starMass + ", " + starDiameter << endl;
// close the file
fs.close();
cout << "Press a key to continue";
getline(std::cin, starDiameter);
}
void PrintStarFacts(std::string fileName, std::string starName){
std::string finality;
char line[256];
ifstream fe("star.dat");
cout << "All Stars:" << endl;
while(!fe.eof()) {
fe.getline(line, 128);
cout << line << endl;
}
fe.close();
cout << "Press a key to continue";
getline(std::cin, finality);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.