Hi,

I am having trouble with skipping lines in a file and inputting the value it reads into variable inside the program.

Data Inside the txt file i am trying to read:

00112
yourname
5874.87
176.07
3456.98

what i want to do is take the last 3 numbers and input each into a variable

here is what i have so far:

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

void search(char& accntt, char& passt, string accnt, string pass);
void get_bal(double& savbal, double& chekbal, double& cdbal);


int main()
{
	char accntt, passt;
	double savbal, chekbal, cdbal;
	string accnt, pass;

	cout << "Please enter your account number.\n";
	cin >> accnt;
	cout << "Please enter your password.\n";
	cin >> pass;
	
	search(accntt, passt, accnt, pass);
	if(accntt == 't' && passt == 't')
	{
		get_bal(savbal, chekbal, cdbal);
	}

	//cout <<"Data 0:" << data[0] << endl << "Data 1:" << data[1] << endl;

	cout << passt << endl << accntt << endl;
	//cout << "sav:" << savbal << endl << "chek:" << chekbal << endl << "cd:" << cdbal << endl;

	return 0;
}

void search(char& accntt, char& passt, string accnt, string pass)
{
		
	string data[3];
	ifstream infile;

	infile.open("Accnt.txt");
	if (infile.fail( ))
	{
		cout << "File open failed.\n";
		exit(1);
	}

    int linenum=0; 
	string line;
    
	while (linenum < 2)// || (data[linenum] != accnt))  //while the end of file is NOT reached
	{
		getline (infile,line); //get one line from the file
		data[linenum] = line;
		cout << data[linenum] << endl; //and output it
		linenum++;
	}

	infile.close(); //closing the file

	if(data[0] == accnt)
		{
			accntt = 't';
		}
		else{}

	if(data[1] == pass)
		{
			passt = 't';
		}
}

void get_bal(double& savbal, double& chekbal, double& cdbal)
{
	ifstream infile;

	infile.open("Accnt.txt");

	if (infile.fail( ))
	{
		cout << "File open failed.\n";
		exit(1);
	}

	for(int i=0; i<6; i++)
	{
		if(i == 2)
		{
			infile >> savbal;
		}
	}

	cout<<savbal;

	infile.close();
	cout << "Sucess.\n";
}

Any help would be much appreciated.
thank you in advance

after line 86 you need to actually read a line from the file -- you can't just simply ignore it.

string line;
for(i = 0; i < 6; ++i)
{
    getline(infile,line);
    if( i > 1)
    {
          // convert to number, you can use stringstream class to do that.
    }
}

thank you for the quick reply. I will try it out and if i need more help i will post agian.

Hi again

I was wondering if it is possible to convert part of an array into a numerical value?

like for example in the above code in the first function i only read the first 2 lines into the array, but what if i read the while file into the array? Then i converted arrayname[2] to a numerical value, i tried aoti but it gives me erros

if arrayname[2] is a C style string then atox would be doable.

if arrayname[2] is an STL string then a C style string could be obtained from an STL string using the c_str() string class method. Alternatively, you could try Ancient Dragons recommendation and try using a stringstream object to convert an STL string to a numerical value

thank you lerner

i looked it up and the c_str() worked perfectly. Thank you again both of you.

Im a student, just starting out with this stuff so you helped me out a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.