Need help

Hi, Basically I have a file that has numbers in and i need them to be read into my code ( which i thought i had done correctly) but it doesnt seem to be happening. The numbers are displayed in 2 collumns. Once read they need to be used in the calculation and outputted onto the screen I am having alot of difficulty with this can any one help? The numbers in the file go like this
4
3 2
5 7

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

ifstream in_file;
ofstream myfile;

int rm = 500;

myfile.open("Results.txt");
in_file.open("account", ios::out);

while(!in_file.eof())
{
string x;
float numberneeded = 0;
float a = 0, b = 0;

std::getline(in_file,x);
in_file >> numberneeded;

float fullamount = 0;

for(int i=0; i < numberneeded; i++)
{
float payslip;
in_file >> a >> b;
payslip = a*b;
fullamount = payslip;
cout >> fullamount;


}
in_file.ignore();


int exit;

system("pause");
return 0;
}
For one thing your code has 3 { and only 2 }.

Don't think you need this line
#include "stdafx.h"

This looks wrong to me
cout >> fullamount;

This appears to have no purpose
int rm = 500;

Another line with no purpose.
fullamount = payslip;

You don't need both of these, use one or the other
1
2
std::getline(in_file,x);
in_file >> numberneeded;


If i'm guessing right, this line needs to be before your while loop.
in_file >> numberneeded;

So I'm guessing that the first number is the number of remaining numbers in the file. Don't know why you would have that but it's your homework I guess you have a reason.

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

//#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream in_file;
ofstream myfile;
myfile.open("Results.txt");
in_file.open("account", ios::out);
//string x;
//float fullamount = 0;
float numberneeded = 0;
float a = 0, b = 0;
// int rm = 500;
float payslip=0;
in_file >> numberneeded;

while(!in_file.eof())
{ 
//std::getline(in_file,x);
//cout << numberneeded << endl;
		
//	for(int i=1; i < numberneeded; i++)
//		{
//		float payslip=0;
		in_file >> a >> b;
		payslip = a*b;
//		fullamount = payslip;
		cout << payslip << endl;
}
// in_file.ignore();
// int exit;
// system("pause");

return 0;
}


You really need to learn to trouble shoot your own code.

Next time think about it first, then write it. For the most part you had it right but you had several lines that simply wasn't needed and I think that came from writing the code without thinking it though.
Last edited on
Topic archived. No new replies allowed.