PDA

Click to See Complete Forum and Search --> : I dont know whats wrong!


SilverSprite
Mar 2nd, 2003, 11:47 AM
Hey all, i'm having trouble with a problem. I don't know whats wrong with my logic. If anyone could help me, it would be appreciated. Well the problem is in the file attached. The following is my code. Could someone please point out whats wrong?

#include <fstream.h>
#include <string>
#include <math.h>
#include <stdlib.h>

int main()
{
ifstream fin ("gift1.in");
ofstream fout ("gift1.out");
int NP;
fin >> NP;
int splitamount, splitpeople, original[NP];
int gain[NP];
string names [NP];
string host, person;

for (int i=0; i<NP; i++)
{
fin >> names[i];
gain[i]=0;
}

for (int j=0; j<NP; j++)
{
fin >> host >> splitamount >> splitpeople;
for (int i=0; i<NP; i++)
if (names[i] == host)
{
original [i] = splitamount;
if (splitpeople != 0)
gain[j] += (splitamount % splitpeople);
else
gain[j] = splitamount;
}
for (int i=0; i<splitpeople; i++)
{
fin >> person;
for (int k=0; k<NP; k++)
if (names[k] == person)
gain[k] += static_cast<int>(floor(splitamount/splitpeople));
}
}
for (int i=0; i<NP; i++)
fout << names[i] << " " << gain[i]-original[i] << endl;

return 0;
}

CornedBee
Mar 2nd, 2003, 04:49 PM
I'm too tired for logic, so here is my standard advice:
First, use tags around your code to preserve indentation.

Second, you're using the deprecated fstream.h instead of fstream, but at the same time the new string, which makes it worse as the two are incompatible.

Third, you're declaring a dynamic array on the stack which is not possible in C++. Actually more than one array.

SilverSprite
Mar 2nd, 2003, 05:00 PM
#include <fstream.h>
#include <string>
#include <math.h>
#include <stdlib.h>

int main()
{
ifstream fin ("gift1.in");
ofstream fout ("gift1.out");
int NP;
fin >> NP;
int splitamount, splitpeople, original[NP];
int gain[NP];
string names [NP];
string host, person;

for (int i=0; i<NP; i++)
{
fin >> names[i];
gain[i]=0;
}

for (int j=0; j<NP; j++)
{
fin >> host >> splitamount >> splitpeople;
for (int i=0; i<NP; i++)
if (names[i] == host)
{
original [i] = splitamount;
if (splitpeople != 0)
gain[j] += (splitamount % splitpeople);
else
gain[j] = splitamount;
}
for (int i=0; i<splitpeople; i++)
{
fin >> person;
for (int k=0; k<NP; k++)
if (names[k] == person)
gain[k] += static_cast<int>(floor(splitamount/splitpeople));
}
}
for (int i=0; i<NP; i++)
fout << names[i] << " " << gain[i]-original[i] << endl;
return 0;
}


Ok i used the code tags, i changed fstream.h to fstream, but i have no idea what you mean by declaring a dynamic array? All the same with those changes, i got the same results:(

Jop
Mar 2nd, 2003, 06:50 PM
The includes should look like this:


#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>

using namespace std;


And what CB means is how you try to use a variable to specify the dimensions of the array:

this for example is wrong:

int gain[NP];


You should use the keyword new to allocate a dynamic array.. the new keyword returns a pointer, this is no problem (even good) since you can use pointers to access arrays.

so if you wanted to allocate an dynamic array you use the keyword new to get a pointer to the first item in the allocated space. Be sure to delete the pointer too when you're done with it.

int *i = new int[NP];
// do stuff
delete [] i;


Here's a little example I quickly wrote:
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


int main(){
int i;
cout << "Type a nr" << endl;
cin >> i;
int *j = new int[i];

for(int x=0;x<i;x++)
j[x] = x;
for(int x=0;x<i;x++)
cout << j[x];

delete [] j;
system("pause");
}


With this info you should be able to figure it out... good luck!