im new NEED HELP! im begging you

hence the title so bear with me.

my project is to take four numbers that are stored in an file nums.txt and add them all together. show the sum. subtract them from one another, show the diff. multiply them all together, show the product. divide them by one another, show the quotient. show the numbers of the sum diff product and quotient in ascending order as well as descending order, all in an output file " results.txt "


i really really need help because i keep getting errors and i cant build any of it. even on the basic steps. this is what i have so far and i already have LOTS OF ERRORS!


// Labfour.cpp : main project file.

#include "stdafx.h"
#include <fstream>
#include <cstdlib>

using namespace System;


#define inFile "nums.txt"
#define outFile "restults.txt"

//functions used

float computeSum(ifstream&, ofstream&);
float computeDif(ifstream&, ofstream&);
float computePro(ifstream&, ofstream&);
float computeQuo(ifstream&, ofstream&);
int ascend();
int descend();

const SIZE = 4;

int main()
{
ifstream ins; //ins is an input stream
ofstream outs; //outs is an output stream

float n1; //input: first number from file
float n2; //input: second number from file
float n3; //input: third number from file
float n4; //input: fourth number from file
float sum; //output: sum
float dif; //output: difference
float pro; //output: product
float quo; //output: quotient




//open input and output file, exit if failure
ins.open("nums.txt");
if (ins.fail())
{
cerr << "**ERROR OPENING FILE FOR INPUT**" << endl;
return EXIT_FAILURE;
}
outs.open("results.txt");
if(outs.fail())
{
cerr << "** ERROR OPENING FILE FOR OUTPUT**" << endl;
return EXIT_FAILURE;
}


sum = computeSum(ins,outs)
dif = computeDiff(ins,outs)
pro = computePro(ins,outs)
quo = computeQuo(ins,outs)





ins.close();
outs.close();

return 0;
}

float computeSum(ifstream& ins, ofstream outs)
{
float n1; //input: first number from file
float n2; //input: second number from file
float n3; //input: third number from file
float n4; //input: fourth number from file

sum = n1 + n2 + n3 + n4

return sum;
}
float computeDif(ifstream& ins, ofstream outs)
{
float n1; //input: first number from file
float n2; //input: second number from file
float n3; //input: third number from file
float n4; //input: fourth number from file

dif = n1 - n2 - n3 - n4

return dif;
}
float computePro(ifstream& ins, ofstream outs)
{
float n1; //input: first number from file
float n2; //input: second number from file
float n3; //input: third number from file
float n4; //input: fourth number from file

pro = n1* n2* n3 * n4

return pro;
}
float computeQuo(ifstream& ins, ofstream outs)
{
float n1; //input: first number from file
float n2; //input: second number from file
float n3; //input: third number from file
float n4; //input: fourth number from file

quo = n1 / n2 / n3 / n4

return quo;
}

Poor planning on your part does not constitute an emergency on my part.

Help me hep you, please post the error log, it might help those of us without compilers handy.
1. Start a new Blank project in VC++ (not a new console project), copy the code in, and remove the #include "stdafx.h" (it is evil!)

2. Change using namespace System; to using namespace std; (are you used to Java or C#?)

3. Change #include <cstdlib> to #include <iostream>

Try again, and copy and paste the errors you get.
Last edited on
In every function, you are returning undeclared variables. You declare float sum in main, but that only has scope in main, so in function computeSum(ifstream& ins, ofstream outs) the variable sum doesn't exist. Declare sum inside the function as well (it will be a seperate variable with the same name, you can choose to rename it to avoid confusion).
im c++ and do i need to include <iostream> even if im taking from a file?
No, but it's better than including cstdlib, because that is C and not C++ (you just stated you wanted to use C++, right?)
Yes thats correct. i just dont understand how the <iostream> would put them in the array.

and do you think it would be good to load them into an array, then preform all of the necessary operations on them, and then show the results in an output window?
You don't have to, but you can while you are working on the project, then when you are done you can remove the cout statements and turn it in.
how should the statements look for loading the array?

say the array is called nums, and the file im taking from is nums.txt (MAX_SIZE = 4)
is

int n=0

while (nums.txt != eof. && nums[] <= MAX_SIZE)
cin >> nums[n]
n++
Topic archived. No new replies allowed.