Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a C++ program #include <iostream> #include <iomanip> #include <fstream>

ID: 3678919 • 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

Assignment.cpp


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#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 infile;
   infile.open(fileName.c_str());

   string line;
   string str;
   int pos;

   cout << " --Star Menu--" << endl;

   getline(infile, line);
   while(infile)
   {
       pos = line.find(',');
       str = line.substr(0, pos);
       cout << str << endl;

       getline(infile, line);
   }
   infile.close();
}

bool IsStarNameValid(string fileName, string starName)
{
   ifstream infile;
   infile.open(fileName.c_str());

   string line;
   string str;
   int pos;
   bool found = false;


   getline(infile, line);
   while(infile && !found)
   {
       pos = line.find(',');
       str = line.substr(0, pos);

       if(strcmp(str.c_str(), starName.c_str()) == 0)
       {
           found = true;
       }

       getline(infile, line);
   }

   infile.close();

   return found;
}

void AddStarToFileFromUserInput(string fileName)
{
   ofstream outfile;
   outfile.open(fileName.c_str(), std::ios_base::app);

   string name;
   string constellation;
   string magnitude;
   string mass;
   string diameter;

   cout << "Enter New Star Name: ";
   getline(cin, name);

   cout << "Enter Star Constellation: ";
   getline(cin, constellation);

   cout << "Enter Star Apparent Magnitude: ";
   getline(cin, magnitude);

   cout << "Enter Star Mass: ";
   getline(cin, mass);

   cout << "Enter Star Diameter: ";
   getline(cin, diameter);

   outfile << endl << name << "," << constellation << "," << magnitude << "," << mass << "," << diameter << ",";

   outfile.close();
}

void PrintStarFacts(string fileName, string starName)
{
   ifstream infile;
   infile.open(fileName.c_str());

   string line;
   int pos;
   bool found = false;

   string name;
string constellation;
string magnitude;
string mass;
string diameter;

   getline(infile, line);
   while(infile && !found)
   {
       pos = line.find(',');
       name = line.substr(0, pos);

       if(strcmp(name.c_str(), starName.c_str()) == 0)
       {
           found = true;

           line = line.substr(pos + 1);

           pos = line.find(',');
           constellation = line.substr(0, pos);
           line = line.substr(pos + 1);


           pos = line.find(',');
           magnitude = line.substr(0, pos);
           line = line.substr(pos + 1);

           pos = line.find(',');
           mass = line.substr(0, pos);
           line = line.substr(pos + 1);


           pos = line.find(',');
           diameter = line.substr(0, pos);
           line = line.substr(pos + 1);

           cout << left << setw(30) << "Star Name: " << left << setw(20) << name << endl;
           cout << left << setw(30) << "Constellation: " << left << setw(20) << constellation << endl;
           cout << left << setw(30) << "Apparent Magnitude: " << left << setw(20) << magnitude << endl;
           cout << left << setw(30) << "Mass: " << left << setw(20) << mass << endl;
           cout << left << setw(30) << "Diameter: " << left << setw(20) << diameter << endl;
       }

       getline(infile, line);
   }
   infile.close();
}


Intput file: stars.dat

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,


Output on console:


--Star Menu--
Sirius
UY Scuti
Arcturus
2MASS J0523-1402
AH Scorpii
DiCaprio
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

--Star Menu--
Sirius
UY Scuti
Arcturus
2MASS J0523-1402
AH Scorpii
DiCaprio
Cool
Enter Star Name, or Add, or Quit:
Sirius
Star Name: Sirius
Constellation: Canis Major   
Apparent Magnitude: 8.44
Mass: 2.02
Diameter: 1.711   

--Star Menu--
Sirius
UY Scuti
Arcturus
2MASS J0523-1402
AH Scorpii
DiCaprio
Cool
Enter Star Name, or Add, or Quit:
Quit

Final input file: stars.dat

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,
Cool,lol,my,2,3,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote