Need Help in displaying the right output.

Hi, I need a little bit of 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 :)
Last edited on
You don't need to say std:: in front of things if you've said "using namespace std"
You don't need the cin.ignore() after the initial getline() (getline will read the newline).
You do need it after reading the filename. Or better yet, read the filename with getline, too.
Try 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    string filename;
    cout << "File name: ";
    getline(cin, filename);

    ifstream fin(filename);
    if (!fin) {
        cerr << "Can't open " << filename << '\n';
        return 1;
    }

    string star;
    cout << "Enter star name: ";
    getline(cin, star);

    string line;
    bool found = false;
    while (getline(fin, line)) {
        if (line.find(star) != line.npos) {
            cout << line << '\n';
            found = true;
            break;
        }
    }

    if (!found)
        cout << "Star \"" << star << "\" not found.\n";
}

Topic archived. No new replies allowed.