This is a C++ program #include <iostream> #include <iomanip> #include <fstream>
ID: 3679129 • Letter: T
Question
This is a C++ program
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int STAR_FACTS = 5;
const int PREF_WIDTH = 23;
int input[5];
void PrintMenu(string fileName);
bool IsStarNameValid(string fileName, string starName);
void AddStarToFileFromUserInput(string fileName);
void PrintStarFacts(string fileName, string starName);
//Do not change anything below this line
//you will lose points. That includes these comments.
//EXACTMATCH
int main(){
bool runProgram = true;
string fileName = "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
stars.dat CSV file :
Sirius,Canis Major,8.44,2.02,1.711,UY Scuti,Scutum,9.0,8,1708,Arcturus,Bootes,-0.05,1.08,25.4,2MASS J0523-1402,Lepus,21.05,0.08,0.086,AH Scorpii,Scorpius,1.5,1.5,1411,DiCaprio,No Oscar,1.2,45.2,1,
The output should look something like this:
--Star Menu--
Sirius
UY Scuti
Arcturus
2MASS J0523-1402
AH Scorpii
DiCaprio
Enter Star Name, or Add, or Quit:
Sirius
Star Name: Sirius
Constellation: Canis Major
Apparent Magnitude: 8.44
Mass: 2.02
Diameter: 1.711
(reprint star menu)
Enter Star Name, or Add, or Quit:
Add
Enter New Star Name:Cool
Enter Star Constellation:lol
Enter Star Apparent Magnitude:my
Enter Star Mass:2
Enter Star Diameter:3
(reprint star menu but add star 'Cool' at the bottom of the lis)
Enter Star Name, or Add, or Quit:
Cool
Star Name: Cool
Constellation: lol
Apparent Magnitude: my
Mass: 2
Diameter: 3
(Reprint star Menu)
Enter Star Name, or Add, or Quit:
Mode
Invalid Star Name
(Reprint Menu)
Enter Star Name, or Add, or Quit:
Quit
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int STAR_FACTS = 5;
const int PREF_WIDTH = 23;
int input[5];
void PrintMenu(string fileName);
bool IsStarNameValid(string fileName, string starName);
void AddStarToFileFromUserInput(string fileName);
void PrintStarFacts(string fileName, string starName);
//Do not change anything below this line
//you will lose points. That includes these comments.
//EXACTMATCH
int main(){
bool runProgram = true;
string fileName = "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 ";
}
}
}
}
void PrintMenu(string fileName){
ifstream file;
//open file
file.open (fileName);
//if file cannot be opened exit
if (!file.is_open()) return;
string word;
int c=1;
while (file)
{
//get a word from file
getline(file,word,’,’);
//Print only star names
if(c==1) {
cout<< word << ' ';
}
c++;
if(c==5)
c=1;
}
//close file
file.close();
}
bool IsStarNameValid(string fileName, string starName){
ifstream file;
//open file
file.open (fileName);
//if file cannot be opened exit
if (!file.is_open()) return;
string word;
while (file){
//get a word from file
getline(file,word,’,’);
//if starname matches with a word in the file, return true
if(strcmp(word,starName)==0)
return true;
}
return false;
}
void AddStarToFileFromUserInput(string fileName)
{
string word;
ifstream file;
file.open (fileName);
//get the new details
cout << ”Enter New Star Name: ”;
cin>>word;
//append the word to file
file<<word;
//add comma after every word
file<<”,”;
//clear the content of the string word to reuse
word.clear();
cout << ”Enter Star Constellation: ”;
cin>>word;
file<<word;
file<<”,”;
word.clear();
cout << ”Enter Star Apparent Magnitude: ”;
cin>>word;
file<<word;
word.clear();
cout << ”Enter Star Mass: ”;
cin>>word;
file<<word;
file<<”,”;
word.clear();
cout << ”Enter Star Diameter: ”;
cin>>word;
file<<word;
file<<”,”;
word.clear();
file.close();
}
void PrintStarFacts(string fileName, string starName){
ifstream file;
file.open (fileName);
if (!file.is_open())
return;
string word;
while (file){
getline(file,word,’,’);
//if a word matches with star name
//print the star name and its 4 adjacent words , its facts.
if(strcmp(word,starName)==0)
{
cout<<”Star Name:”<<word<<” ”;
word.clear();
getLine(file,word,’,’);
cout<<”Constellation:”<<word<<” ”;
word.clear();
getLine(file,word,’,’);
cout<<”Apparent Magnitude:”<<word<<” ”;
word.clear();
getLine(file,word,’,’);
cout<<”Mass:”<<word<<” ”;
word.clear();
getLine(file,word,’,’);
cout<<”Diameter:”<<word<<” ”;
return;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.