Copy only line user wants from a full file into other file

I am writing a c++ code for my cafe project, which READ from a full file(list of drinks 10)
1.Coca 60
2.pepsi 70
3.Banana_Shake 80
4.7up 70
5.Red_bull 90
6.Spirte 60
7.Mango_shake 80
8.pineapple_juice 200
now I ask the user to choice from this list. IF he enters 8 I want my code to copy (8.pineapple_juice 200) into his file which is also made using my code.
MY BELOW CODE COPIES BUT NOT AS I MENTION ABOVE,this was the closest I could go.So can anyone guide me what should I can do ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{ string user;
	cout<<"enter your name to create your \n";
	getline(cin,user);
	ofstream outFile(user.c_str(),ios::app);  //user named file
	string line;
	ifstream inFile("drinks.txt"); //file with drinks name and 
                                             //number(labels)
	int count;
    cout<<"Enter your choices:\n";
    cin>>count;
    while(count >0)
   { 
	getline(inFile, line);
	outFile << line <<endl;
	--count;
   }
}
Last edited on
Hello Zero001,

Your code poses more questions than answers.

What IDE are you using and what C++ standard is the compiler using?

Why "cstring" and not "string"?

Why use a file and not an array or vector?

"outFile" uses a cstring for the file name, but "inFile" uses a string for the file name. From C++11 on a std::string will work.

What does the input file look like? You are asking everyone to guess what is in this file.

If yo use something like this in your code:
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
const std::string DRINKNAMES[]
{
	"",
	"Coca Cola",
	"Pepsi",
	"Banana_Shake",
	"7up",
	"Red_bull",
	"Spirte",
	"Mango_shake",
	"pineapple_juice"
};

constexpr int DRINKPRICES[]
{
	0,
	60,
	70,
	80,
	70,
	90,
	60,
	80,
	200
};

After line 19 you need an if statement to compare what is in the file to the choice that is made. For now thinking:
1
2
if (line == DRINKNAME[count])
    outFile << line <<endl;

The problem here is that I am not sure what "line" will contain, so this may have to be adjusted. This way you only output a match and not the whole file.

It is best to stay away from global variables like "string user". This is best defined inside "main" where it is used.

With more information you can get a better response to your question.

Hope this helps,

Andy

Edit:
Last edited on
Your code just needs a little ‘tweaking’.
What it does is: read ‘count’ lines and write them all into the ‘username’ file.
What you want it to do is: read ‘count - 1’ lines and discard them. Later, read another line and write it (and only it) into the ‘username’ file.

In addition, I’d suggest:
- using <string> instead of <cstring> (as Handy Andy said)
- check if your files have been correctly opened before using them (and avoid the .c_str() method)
- in general, avoid using std::cin and std::getline() in the same code
- in general, avoid global variables.

- - -
Edit
I'd also add an extension to the 'username' file:
user += ".txt";
Last edited on
Topic archived. No new replies allowed.