HELP WITH C++!!!!PLEASE HELP Your program should use stars.dat as a data file fo
ID: 3678954 • Letter: H
Question
HELP WITH C++!!!!PLEASE HELP 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!.
#include #include #include using namespace std; const int STAR FACTS = 5; const int PREF WIDTH = 23; void PrintMenu(string fileName); bool IsStarNameValid(string fileName, string starName); void AddStarToFileFromUserInput(string fileName); void PrintStarFacts (string fileName, string starName); int main()1 boo runProgram = true; string fileNamestars.dat"; string userSelection; while(runProgram){ PrintMenu(fileName); coutExplanation / Answer
Solution: See the code below:
-----------------------------------------
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <sstream>
using namespace std;
const int STAR_FACTS=5;
const int PREF_WIDTH=23;
void PrintMenu(string fileName)
{
string line;
ifstream file(fileName.c_str());
if(file.is_open())
{
while(getline(file,line))
{
istringstream iss(line);
string name;
while(getline(iss,name,','))
{
cout<<name<<endl;
break;
}
}
}
else
cout<<"File can not be opened."<<endl;
}
bool IsStarNameValid(string fileName,string starName)
{
string line;
ifstream file(fileName.c_str());
if(file.is_open())
{
while(getline(file,line))
{
istringstream iss(line);
string name;
while(getline(iss,name,','))
{
if(name==starName)
return true;
}
}
file.close();
}
else
cout<<"File can not be opened."<<endl;
return false;
}
void AddStarToFileFromUserInput(string fileName)
{
string starName,constellationName,magnitude,mass,diameter;
cout<<"Enter star name:";
//cin.ignore();
getline(cin,starName);
cout<<"Enter star's constellation name:";
getline(cin,constellationName);
cout<<"Enter star's magnitude:";
cin>>magnitude;
cout<<"Enter star's mass:";
cin>>mass;
cout<<"Enter star's diameter:";
cin>>diameter;
string starFacts=starName+","+constellationName+","+magnitude+","+mass+","+diameter;
ofstream file(fileName.c_str(),std::ios_base::app);
if(file.is_open())
{
file<<starFacts;
file.close();
}
else
cout<<"File can not be opened."<<endl;
}
void PrintStarFacts(string fileName,string starName)
{
string line;
ifstream file(fileName.c_str());
if(file.is_open())
{
while(getline(file,line))
{
istringstream iss(line);
string name;
while(getline(iss,name,','))
{
if(name==starName)
{
cout<<"Star facts:"<<line<<endl;
}
}
}
file.close();
}
else
cout<<"File can not be opened."<<endl;
}
int main() {
bool runProgram=true;
string fileName="./src/stars.dat";
string userSelection;
while(runProgram)
{
PrintMenu(fileName);
cout<<"Enter Star Name, or Add or Quit: ";
getline(cin,userSelection);
if(userSelection=="Add")
{
AddStarToFileFromUserInput(fileName);
}
else if(userSelection=="Quit")
{
runProgram=false;
}
else
{
if(IsStarNameValid(fileName,userSelection))
{
PrintStarFacts(fileName,userSelection);
}
else
{
cout<<"Invalid Star Name ";
}
}
}
}//EXACTMATCH
-----------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.