How to display output from the file and modify it

This is the content in the file:

Acrux~Alpha 1 Crucis~HR 4730~HD 108248~320.00
Achird~Eta Cassiopeiae~HR 219~HD 4614~19.41
Ain Al Rami~Nu 1 Sagittarii~HR 7116~HD 174974~1850.59

I want to display it as output like:
Name~Proper Name~HR number~HD number~distance

so it will be like
Name: Acrux Proper Name: Alpha 1 Crucis HR number: HR 4730 HD number: HD 108248 distance: 320.00

How do I write in C++ code where it will give me desired output. As you can see in the file it is separated by "~" so it means I have to tell the struct method or else what is before 1st ~ and what is before 2nd ~.
any help?
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
#include <iostream>
#include <iomanip>
#include <regex>
#include <string>
#include <sstream>

struct planet
{
    std::string short_name ;
    std::string full_name ;
    int hr_number ;
    int hd_number ;
    double distance ;
};

std::ostream& pretty_print( std::ostream& stm, const planet& p )
{
    return stm << "name: " << std::quoted(p.short_name)
               << "  full name: " << std::quoted(p.full_name)
               << "  hr number: " << p.hr_number
               << "  hd number: " << p.hd_number
               << "  distance: " << std::fixed << std::setprecision(2) << p.distance << '\n' ;
}

std::istream& get_planet( std::istream& stm, planet& p )
{
    if( std::string line ; std::getline( stm, line ) ) // C++17 (initialiser in if statement)
    {
        // https://en.cppreference.com/w/cpp/regex
        // https://regexper.com/#%5E%28%5B%5E~%5D%2B%29%5C~%28%5B%5E~%5D%2B%29%5C~HR%5Cs*%28%5Cd%2B%29%5C~HD%5Cs*%28%5Cd%2B%29%5C~%28%5Cd%2B%5C.%5Cd*%29%24
        static const std::regex planet_re( R"(^([^~]+)\~([^~]+)\~HR\s*(\d+)\~HD\s*(\d+)\~(\d+\.\d*)$)" ) ;
        
        // https://en.cppreference.com/w/cpp/regex/regex_match
        // https://en.cppreference.com/w/cpp/regex/match_results
        std::smatch results ;
        if( std::regex_match( line, results, planet_re ) )
            p = { results[1], results[2], std::stoi(results[3]), std::stoi(results[4]), std::stod(results[5]) } ;
        else
            stm.setstate(stm.failbit) ;
    }

    return stm ;
}

int main()
{
    std::istringstream file( "Acrux~Alpha 1 Crucis~HR 4730~HD 108248~320.00\n"
                             "Achird~Eta Cassiopeiae~HR 219~HD 4614~19.41\n"
                             "Ain Al Rami~Nu 1 Sagittarii~HR 7116~HD 174974~1850.59\n\n" ) ;

    planet p ;
    while( get_planet( file, p ) ) pretty_print( std::cout, p ) ;
}

http://coliru.stacked-crooked.com/a/08fac4c56b6260b6
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;


struct Star
{
   string name;
   string properName;
   string HRnumber;
   string HDnumber;
   double distance;
};


istream &operator >> ( istream &strm, Star &star )
{
   const char SPACER = '~';
   string line, temp;

   getline( strm, line );
   stringstream ss( line );
   getline( ss, star.name      , SPACER );
   getline( ss, star.properName, SPACER );
   getline( ss, star.HRnumber  , SPACER );
   getline( ss, star.HDnumber  , SPACER );
   getline( ss, temp );   stringstream( temp ) >> star.distance;    // clear the line
   return strm;
}


ostream &operator << ( ostream &strm, const Star &star )
{
   strm << "Name: "        << setw( 15 ) << left << star.name       << "  ";
   strm << "Proper name: " << setw( 20 ) << left << star.properName << "  ";
   strm << "HR number: "   << setw( 12 ) << left << star.HRnumber   << "  ";
   strm << "HD number: "   << setw( 12 ) << left << star.HDnumber   << "  ";
   strm << "distance: "    << setw( 12 ) << left << star.distance   << '\n';
   return strm;
}


int main()
{
// ifstream in ( "starCatalogue.txt" );
// ofstream out( "starCatalogue.out" );
   stringstream in( "Acrux~Alpha 1 Crucis~HR 4730~HD 108248~320.00         \n"
                    "Achird~Eta Cassiopeiae~HR 219~HD 4614~19.41           \n"
                    "Ain Al Rami~Nu 1 Sagittarii~HR 7116~HD 174974~1850.59 \n" );
   ostream &out = cout;

   Star star;
   while( in >> star ) out << star;
}

Name: Acrux            Proper name: Alpha 1 Crucis        HR number: HR 4730       HD number: HD 108248     distance: 320         
Name: Achird           Proper name: Eta Cassiopeiae       HR number: HR 219        HD number: HD 4614       distance: 19.41       
Name: Ain Al Rami      Proper name: Nu 1 Sagittarii       HR number: HR 7116       HD number: HD 174974     distance: 1850.59 
Last edited on
@lastchance,
Thank you for your code. it works but I'm currently facing problem with file. As I have to ask user input for the file.
Thank you for your code. it works but I'm currently facing problem with file. As I have to ask user input for the file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
// ifstream in ( "starCatalogue.txt" );
// ofstream out( "starCatalogue.out" );
// stringstream in( "Acrux~Alpha 1 Crucis~HR 4730~HD 108248~320.00         \n"
//                  "Achird~Eta Cassiopeiae~HR 219~HD 4614~19.41           \n"
//                  "Ain Al Rami~Nu 1 Sagittarii~HR 7116~HD 174974~1850.59 \n" );
   string filename;
   cout << "What filename? ";   cin >> filename;
   ifstream in( filename );

   ostream &out = cout;

   Star star;
   while( in >> star ) out << star;
}
Thanks, I have sent you a PM to get a bit more help. Much appreciate it.
Please use the forum: I don't give private help.
Hi, thanks for helping me in the code. I need a little bit of more guidance.

I have to write a program and the sample runs is like this:
> g++ -o star stars.cpp -std=c++17
> star
Enter file name >> starData.txt
Enter star proper name >> Alpha Cephei
Star: proper name: Alpha Cephei distance: 49.05 light years HD num: HD 203280
HR num HR 8162 common name: Alderaimin
> star
Enter file name >> starData.txt
Enter star proper name >> Alpha Centuri
No star with the proper name "Alpha Centuri" was found
> star
Enter file name >> e.dat
The file "e.dat" does not exist



my code is this:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    std::string str ("~");
    string fileName;
    std::cout << "Enter file name >> ";
    std::cin >> fileName;
    std::ifstream fin;
    std::string line;
    fin.open(fileName.c_str());

    bool foundIt = false;

    if (fin.is_open())
    {

       std::string starProperName;
       std::cout << "Enter star proper name >> ";
       std::getline(cin, starProperName);
       std::cin.ignore();

       while (std::getline(fin,line))
       {
          std::size_t i = line.find(starProperName);
          if (i != std::string::npos)
          {
             std::cout << line << "\n";

             foundIt = true;
          }
          else
          {
             cout << "No star with the proper name " << "\"" << starProperName << "\"" << " was found " << endl;


my output is this:
35> vi stars.cpp
36> g++ -o stars stars.cpp
37> stars
Enter file name >> a.dat
Enter star proper name >> Alpha 1 Crucis
Acamar~Theta 1 Eridani~HR 897~HD 18622~163.08
Achernar~Alpha Eridani~HR 472~HD 10144~139.10
Achird~Eta Cassiopeiae~HR 219~HD 4614~19.41
Acrux~Alpha 1 Crucis~HR 4730~HD 108248~320.00
Acubens~Alpha Cancri~HR 3572~HD 76756~191.86
Adhara~Epsilon Canis Majoris~HR 2618~HD 52089~430.00
Adhafera~Zeta Leonis~HR 4031~HD 89025~271.80
Adhil~Xi Andromedae~HR 390~HD 8207~217.44
Agena~Beta Centauri~HR 5267~HD 122451~390.00
Ain Al Rami~Nu 1 Sagittarii~HR 7116~HD 174974~1850.59
Ain~Epsilon Tauri~HR 1409~HD 28305~147.59
Almaaz~Epsilon Aurigae~HR 1605~HD 31964~2000.88
Al Kalb Al Rai~Rho 2 Cephei~HR 8591~HD 213798~245.56
Al Minliar Al Asad~Kappa Leonis~HR 3731~HD 81146~210.65
Al Minliar Al Shuja~Sigma Hydrae~HR 3418~HD 73471~370.93
Aladfar~Eta Lyrae~HR 7298~HD 180163~1390.77
Al Athfar~Mu Lyrae~HR 6903~HD 169702~439.02
Al Baldah~Pi Sagittarii~HR 7264~HD 178524~510.67
Al Bali~Epsilon Aquarii~HR 7950~HD 198001~203.85
Albireo~Beta 1 Cygni~HR 7417~HD 183912~430.34
Alchita~Al Minliar Al Ghurab~HR 4623~HD 105452~49.77
Alcor~80 Ursae Majoris~HR 5062~HD 116842~81.7
Alcyone~Eta Tauri~HR 1165~HD 23630~136.76
Aldebaran~Alpha Tauri~HR 1457~HD 29139~65.23
Alderaimin~Alpha Cephei~HR 8162~HD 203280~49.05
Aldhibah~Zeta Draconis~HR 6396~HD 155763~330.81
Alfecca Meridiana~Alpha Coronae Austrini~HR 7254~HD 178253~125.73
Alfirk~Beta Cephei~HR 8238~HD 205021~690.50
Algenib~Gamma Pegasi~HR 39~HD 886~390.18
Algieba~Gamma 1 Leonis~HR 4057~HD 89484~130.47
Algol~Beta Persei~HR 936~HD 19356~92.95
Algorab~Delta Corvi~HR 4757~HD 108767~86.96
Alhena~Gamma Geminorum~HR 2421~HD 47105~109.09



It should display according to the sample run.
Much appreciate your help. thanks :)
I think you would be well advised to create a struct Star to hold all the properties of a star.
Then create a vector<Star> and read all your star data into it.

Then you can compare successive inputs from the user with the relevant property of your stars.
Topic archived. No new replies allowed.