Hey new to coding.

Hey guys,

I'm very new to coding and over the last few dasy I have been wacking my head agains t a brick wall. The problem I'm having is I have been given a .txt file to incorporate in to the code im writing and I need to be able to get indervidual lines from that .txt file out instead of displaying the entire file.

GO
Property 1 60 5 0
Property 2 80 10 0
Penalty
Property 3 100 15 1
Property 4 120 15 1
Jail
Property 5 140 20 2
Property 6 160 20 2
Property 7 180 25 3
Property 8 180 25 3
Property 9 200 25 3
Free Parking
Property 10 220 30 4
Property 11 240 30 4
Bonus
Property 12 260 35 5
Property 13 280 35 5
Go to Jail
Property 14 300 45 6
Property 15 320 45 6
Property 16 400 50 7
Property 17 420 50 7
Property 18 440 50 7

This is the information within the . txt file and what I want to do is call a single indervidual line from an ifelse statement. say if they land on property12 the ifelse statement calls that line from the .txt file?

Any help would be most appreciated.

Here is the code I have been trying to adapt:

#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("names.txt");
if (myfile.is_open())
{
while (myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";
system("pause");
return 0;
}


Thanks guys
Well, you have it sort of right. Instead of couting out the file, store it into an std::vector, then you can use .at() in order to access a specific line. There is no way to simply "read" a specific line of text since it basically a large line with a couple of "newline" characters that text editors interpret.
How about if I use some code like this?

int main ()
{
int number, rent, cost;
string line;
ifstream myfile ("names.txt");

string name;
cin >> name;

if (name == "property")

{
int number;
int rent;
int cost;
cin >> number;
cin >> rent;
}

system("pause");
return 0;
}

As in all honest im not sure if I understand what you mean??
Topic archived. No new replies allowed.