Please Please Please Please Help!

closed account (EC54GNh0)
I have to create a program that will take a data file named current.txt as an input and the produced output will be saved into another file. I am almost done with the first part however, it's not working. I am not sure if I am opening the file properly. And the second part, I don't even know how to do it. Please please please please, if you guys could help me or even give me a clue. I am really begging at this moment. Please.

There was a given resistance for me to put into an array and I have to use the data file current.txt in my current in order to calculate power.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
cout << "Let us calculate power." <<endl;

int resistance[5]= {16, 27, 39, 56, 81};
int current[5];
int power[5]= {0};
int i = 0;

fstream inFile ;
inFile.open("current.txt");

if (inFile.fail())
{
cout << "File not found. Error 404." <<endl;
}

while (!inFile.eof())
{
inFile >> current[i];

for (int i = 0; i < 5; i++)
{
cin >> current[i];
power[i] = (resistance[i] *pow(current[i],2));
}

cout <<""<<endl;
cout <<" Here are your results:" <<endl;
cout <<" RESISTANCE CURRENT POWER" <<endl;
cout <<" ---------- ------- -----" <<endl;

for (int i = 0; i < 5; i++)
{
cout <<setw(13)
<< resistance[i]
<< setw(13)
<< current [i]
<< setw(13)
<< power[i]
<< endl;
}

cout << " ---------- ------- -----" <<endl;
cout << "TOTAL:"
<< setw(8)
<< resistance[0]+resistance[1]+resistance[2]+resistance[3]+resistance[4]
<< setw(13)
<< current[0]+current[1]+current[2]+current[3]+current[4]
<< setw(12)
<< power[0]+power[1]+power[2]+power[3]+power[4] <<endl;
cout <<""<<endl;
exit (1);
}

}
First, use code tags:
http://www.cplusplus.com/articles/z13hAqkS/

Second, could you be more descriptive than "not working"? Tell us what it is a doing and what you were expecting it to do.
Last edited on
closed account (EC54GNh0)
So the prompt was to redo a program that we did in the past.
The initial prompt was to take 5 "current" inputs from the user then use those numbers to calculate.
This was the program I created:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
cout << "Welcome! Let us calculate power." <<endl;
cout << "Please provide 5 numerical values for current:" <<endl;
cout << "" <<endl;

double resistance[5]= {16, 27, 39, 56, 81};
double current[5]= {0};
double power[5]= {0};

for (int i = 0; i < 5; i++)
{
cout << "Enter current number " << i+1 << ":" <<endl;
cin >> current[i] ;

power[i] = (resistance[i] *pow(current[i],2));
}

cout <<""<<endl;
cout <<" Here are your results:" <<endl;
cout <<" RESISTANCE CURRENT POWER" <<endl;
cout <<" ---------- ------- -----" <<endl;

for (int i = 0; i < 5; i++)
{
cout <<setw(13)
<< resistance[i]
<< setw(13)
<< current [i]
<< setw(13)
<< power[i]
<< endl;
}

cout << " ---------- ------- -----" <<endl;
cout << "TOTAL:"
<< setw(8)
<< resistance[0]+resistance[1]+resistance[2]+resistance[3]+resistance[4]
<< setw(13)
<< current[0]+current[1]+current[2]+current[3]+current[4]
<< setw(12)
<< power[0]+power[1]+power[2]+power[3]+power[4] <<endl;
cout <<""<<endl;
}


This one worked!
This time, instead of asking for user's input, we use a data file that our teacher gave us as the input/ cin <<. He named it as current.txt. So my problem is it's not taking the current.txt input and calculating it! It just waits and I don't know how to solve the problem.

In addition, instead of having the "result" display into the screen, he wants it saved into another file. This one, I am not really sure how to do. PLEASE HELP!
Ok. I would suggest looking up fstream and how to use it. Once you have opened the file, it is quite easy to get the data from it; it is identical to how cin works.
closed account (EC54GNh0)
Thank you so much :)!! IT worked!
What kind of thread is this,
why you change all to :), = ="
@ terapaht: This is actually common. The OP is probably trying to hide the tracks of getting help on their homework from any school admins that might search for their test questions on forums like these. It's a shame because it ruins the thread for any future searches. 8^{
Last edited on
I always report this when it happens. It's an abuse of the forum, and of the goodwill of the people who post here.
We really need to add something that forbids people below a certain post count from editing their post.
Last edited on
Yeah, that would be a great idea!
Or just have some revision history. And the ability for certain users to edit others' posts. This crap is just ridiculous.
Might just be an honest misunderstanding from a newbie too. There's really nothing about the help given here that would break any school rules.

I frequent other sites where they limit editing until a member establishes themselves. Other places where many people immediately quote newbie posts so they can't edit the question away.

In any case, hopefully the OP will see these posts and choose to not do it again...

Almost forgot the obligatory link for new users: http://www.cplusplus.com/forum/beginner/1/
Last edited on
Topic archived. No new replies allowed.