Problem with error message / if else statements

I am writing a program to determine company's weekly payroll. Before I move on to the next step, I must fix a problem I am having.

The error message still displays even if the user types the file name properly and it opens the file.

How do I fix 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*Program to determine company's weekly payroll*/

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void OpenTheFile()
{
ifstream inputFile;
string filename;
char info;
int number;

//Opens the file
inputFile.open("Lab2part2.txt");
cout << "File opened successfully.\n";

//If file opens successfully, process it.
if (inputFile)
{
    //Displays info from file.
    while (inputFile >> info)
    {
        cout << info << endl;
    }
}
else
{
    //Display error message.
    cout << "Error opening the file.\n";
}
}

void NameTheFile()
{

ifstream inputFile;
string filename;

//Asks user for file name.

cout << "What is the file name?\n.";
cin  >> filename;

    if (filename == "Lab2part2.txt")
    {
        cout << "Opening the file...\n";
        OpenTheFile();
    }

    else (filename != "Lab2part2.txt");
    {
        //Why does this message also display after file opens?
        cout << "Error opening the file.\n";
    }
}

void GetTheData()
{
    //statement
    //statement
    //statement
}

void CalculateTheTotalPay()
{

    //statement
    //statement
    //statement
}

void PrintTheData()
{
    //statement
    //statement
    //statement
}

int main()
{
    //Main to call other functions
    cout << "Welcome.\n";
    NameTheFile();

    return 0;
}
line 52 else (filename != "Lab2part2.txt"); <- remove the semicolon here
EDIT:

If I do that, I get a compiler error message.

58 Expected ';' before '{'
Last edited on
ah, one more thing.

If you keep the condition, it needs to be else if.
else if (filename != "Lab2part2.txt")

or it can just be else without the condition, since if it fails the if part, it has to be not equal to that specific file name.

else

Okay, I removed the condition and it worked!
Topic archived. No new replies allowed.