<fstream>

Hello , i am new on this <fstream>, i want to make a program which will asks the exact key word from the user , if user types it wrong ,it should give error.What is my mistake here. Thank you,

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

int main()
{
cout << "Please enter a filename for the Songs file :" << endl;
fstream filesng;
filesng.open("songs.txt");
if (filesng.is_open())
{
filesng << "Please enter a filename for Votes file :" << endl;
fstream filevt;
filevt.open("Votes.txt");
if (filevt.is_open())
{
filevt << "Please enter a filename for Output file :" << endl;
fstream fileop;
fileop.open("output.txt");
if (fileop.is_open())
{
fileop << "Results are in the file : output.txt" << endl;
}
else
{
cout << "Cannot open the Output file !" << endl;
}
}
else
{
cout << "Cannot open the Votes file !" << endl;
}
}
else
{
cout << " Cannot open the Songs file !" << endl;
}
return 0;
cin.get();
cin.ignore();
}
Last edited on
i want to make a program which will asks the exact key word from the user , if user types it wrong ,it should give error.

Then you will need to get the user input.
1
2
3
    string keyword;
    cout << "Please enter the keyword\n";
    cin >> keyword;


Currently you are sending the prompts to the file:
 
    filesng << "Please enter a filename for Votes file :" << endl;
Presumably you intend it to be displayed for the user to read and respond to?
 
    cout << "Please enter a filename for Votes file :" << endl;
Also , you don't indicate whether the first two files are to be used for input or output? If the file is for input it should already exist. In that case use ifstream.

Maybe something like 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    fstream filesng("songs.txt");
    if (!filesng)
    {
        cout << "Error: could not open songs file";
        return 1;
    }
    
    cout << "Please enter a filename for Votes file :" << endl;
    string votesname;
    cin >> votesname;
    
    fstream filevt(votesname);
    if (!filevt)
    {
        cout << "Cannot open the Votes file " << votesname << endl;
        return 1;
    }

    cout << "Please enter a filename for Output file :" << endl;
    string outname;
    cin >> outname;
    
    ofstream fileop(outname);
    if (!fileop)
    {
        cout << "Cannot open the Output file " << outname << endl;
        return 1;
    }
    

    cout << "Results are in the file : " << outname << endl;


}
Last edited on
Hello again and Thank you!
i tried to make what you mentioned but i am still having troubles about start working the program, Concept of the program , it should open "songs.txt" file , if user types the exact "songs.txt".



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

int main()
{
string songfile;
cout << "Please enter a filename for the Songs file :" << endl;
cin >> songfile;
fstream filesng;
filesng.open("songs.txt");
if (filesng.is_open())
{
string votefile;
cout << "Please enter a filename for Votes\n file :" << endl;
cin >> votefile;
fstream filevt;
filevt.open("Votes.txt");
if (filevt.is_open())
{
string outfile;
cout << "Please enter a filename for Output\n file :" << endl;
cin >> outfile;
fstream fileop;
fileop.open("output.txt");
if (fileop.is_open())
{
cout << "Results are in the file : output.txt" << endl;
}
else
{
cout << "Cannot open the Output file !" << endl;
}
}
else
{
cout << "Cannot open the Votes file !" << endl;
}
}
else
{
cout << " Cannot open the Songs file !" << endl;
}
return 0;
cin.get();
cin.ignore();
}
Chervil , Thank you for everything ,
This helped a lot ,
Regards ,
I understand a lot

Concept of the program , it should open "songs.txt" file , if user types the exact "songs.txt".


If the user must enter "songs.txt" then test it directly:
1
2
3
4
5
6
7
8
9
10
11
    string songfile;
    cout << "Please enter a filename for the Songs file :" << endl;
    cin >> songfile;
    if (songfile != "songs.txt")
    {
        cout << "Sorry, that name is incorrect\n";
    }
    else
    {
        // etc.
    }


Last edited on
Topic archived. No new replies allowed.