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!
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
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).
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?