-1

I'm making a project from my finals on cafe, in which I need user to select drinks from a made file drinks which has drinks in list like: 1. Coca-Cola $5 2. Pepsi $8 now I want user to press 1 if he or she wants to buy coca-cola and then the 1. coca-cola $5 copied to his file

I have tried all possible codes I could this is the closest code I could get

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
main()
{
    ofstream outFile("userfile2.txt");
    string line;

    ifstream inFile("Drinks.txt");
    int count;
    cin>>count;
    while(getline(inFile, line))
    { 
       if(count>0 && count<2)
       {
         outFile << line <<endl;
       }
       count++;
     }

     outFile.close();
     inFile.close();
}

I expect that I can copy any drink the user wants from the drinks file to the user file has the user press 1 2 or 3 numbers from the list of drink shown to him at the time of code running.

bruno
  • 32,421
  • 7
  • 25
  • 37
Zero001
  • 31
  • 9
  • your description is not clear enough, what do you want if the input number is 2 ? to copy the line #2 of Drinks.txt and only that one ? – bruno Dec 29 '18 at 10:00
  • yes, and if the input is #3 i want to copy only that one – Zero001 Dec 29 '18 at 10:04
  • my codes work fine till this point, which i have psoted – Zero001 Dec 29 '18 at 10:05
  • it copies #1 but i have tried different steps after this but all of them go wrong for #2 or #3 inputs from user – Zero001 Dec 29 '18 at 10:07
  • Sir , I may not know well the rules of the site well yet, but I seriously need help with this code because it is part of my full 300+ project and it needs to be submitted on Tuesday or ill fail. I will be very thankful to anyone if they can just even give me a little hint. – Zero001 Dec 29 '18 at 10:11
  • 2
    @Zero001 _"I seriously need help with this code because it is part of my full 300+ project and it needs to be submitted on Tuesday or ill fail."_ That's not very good criteria to ask questions at Stack Overflow. – πάντα ῥεῖ Dec 29 '18 at 10:25

1 Answers1

1

it is very simple

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

int main()
{
  ofstream outFile("userfile2.txt");
  string line;
  ifstream inFile("Drinks.txt");
  int count;
  cin>>count;
  while(getline(inFile, line))
  { 
    if (--count == 0)
    {
        outFile << line <<endl;
        break;
    }
  }

  outFile.close(); // not mandatory, done automatically by the destructor
  inFile.close(); // not mandatory, done automatically by the destructor
}
bruno
  • 32,421
  • 7
  • 25
  • 37