Question:
what is the problem with this code?
Taha
2011-06-22 13:09:55 UTC
it gets the input from file gift.in
input format is like this
first line has a number(noppl) which indicates number of people in a group of friends who are going to give each other some gifts.
next noppl lines contain the name of those people
next line contains name of one person followed by a line which contains number of people he is going to buy gift for ("nofriend") and the maximum total amount of money he is going to spend.(money that a person gives or receives must be an integer)
next nofriend lines contain the name of people who are receiving the gifts
next line contains another gift giver name and this goes on until all the names have come.
a sample list is this:
4
david
johnson
charley
nicole
charley
2 67
nicole
johnson
johnson
1 150
nicole
david
3 221
nicole
johnson
charley
nicole
3 5
charley
johnson
david

output is a list of names followed by amount of profit the gift giving has made for them(value of gifts received - value of gifts given)
whenever I run this program it goes non-responding what is the problem?
Also I tried to initialize the array account to 0 but I got an error saying I can't do that because it is variable sized. why is that?




#include
#include
#include
#include
#define maxnlen 14
using namespace std;
int main(){

ifstream fin("gift.in");
ofstream fout("gift.out");
int noppl=7;
if(fin==NULL||fout==NULL)cout<<"can't";
fin>>noppl;
char *pplname[noppl+1];
for(int i=0;i<=noppl-1;i++){
pplname[i]=new char[maxnlen+1];
fin.getline(pplname[i],maxnlen);
}
int account[noppl+1];
char *currentperson;
int nofriend, money;
for(int y=1;y<=noppl;y++){
delete [] currentperson;
currentperson=new char[maxnlen+1];
fin.getline(currentperson,maxnlen);
fin>>nofriend>>money;
int moneyg=int(money/nofriend);
money=moneyg*nofriend;
// this part is to get the number of current person:
int tt=1;
int ttt=0;
while(tt){
if (!strcmp(pplname[ttt],currentperson))
tt=0;
ttt++;
}
ttt--;
//ttt is the number of person
account[ttt]-=money;
for(int j=0; j<=nofriend-1;j++){
delete [] currentperson;
currentperson= new char[maxnlen+1];
fin.getline(currentperson,maxnlen);
// this part is to get the number of current person:
tt=1;
ttt=0;
while(tt){
if (!strcmp(pplname[ttt],currentperson))
tt=0;
ttt++;
}
ttt--;
//**** is the number of person
account[ttt]+=moneyg;
}


}
for(int u=0;u<=noppl-1;u++){
fout<
}

fin.close();
fout.close();
return 0;

}
Four answers:
cja
2011-06-22 13:57:14 UTC
You need to dynamically allocate the pplname and account arrays. I made some changes to get your code to compile and run; it's a start:



[ omitted #includes, etc. so it'll fit ]



int main(int argc, char *argv[]) {

    ifstream fin("gift.in");

    ofstream fout("gift.out");

    int noppl, *account, nofriend, money, moneyg, tt, ttt;

    char *currentperson;



    assert((fout != NULL) && (fin != NULL));

    getInt(fin, &noppl);

    char **pplname = new char*[noppl+1];

    for (int i = 0; i <= noppl - 1; i++) {

        pplname[i] = new char[maxnlen+1];

        fin.getline(pplname[i],maxnlen);

    }

    account = new int[noppl+1];

    for (int y = 1; y <= noppl; y++) {

        currentperson = new char[maxnlen+1];

        fin.getline(currentperson,maxnlen);

        getInt(fin, &nofriend, &money);

        moneyg = int(money / nofriend);

        money = moneyg * nofriend;

        tt = 1, ttt = 0;

        while(tt) { if (!strcmp(pplname[ttt],currentperson)) tt = 0; ttt++; }

        account[--ttt] -= money;

        delete [] currentperson;

        for (int j = 0; j <= nofriend-1;j++){

            currentperson = new char[maxnlen+1];

            fin.getline(currentperson, maxnlen);

            tt = 1, ttt = 0;

            while (tt) { if (!strcmp(pplname[ttt],currentperson)) tt = 0; ttt++; }

            account[--ttt] += moneyg;

            delete [] currentperson;

        }

    }

    for (int u = 0; u <= noppl - 1; u++) {

        fout << pplname[u] << " " << account[u] << endl;

    }

    delete [] pplname;

    delete [] account;

    fin.close();

    fout.close();

    return 0;

}



void getInt(ifstream &in, int *n1, int *n2) {

    stringstream ss;

    string line;



    getline(in,line); ss.str(line);

    assert(ss >> *n1);

    if (n2 != NULL) assert(ss >> *n2);

}
Uzair
2011-06-22 20:52:53 UTC
problem might be with this statement.

char *pplname[noppl+1];



This array of pointers should be dynamic because noppl is a variable. I wonder why compiler haven't given you the compilation error. Change it to this

char **pplname = new char*[noppl+1];
flyingtiggeruk
2011-06-22 20:46:26 UTC
first off I'd use cout<< statements through the program to find out how far it was getting before it gives up and then you can take it from there. It's not the easiest to follow, to be honest, so nothing obvious is leaping out.
anonymous
2011-06-22 20:12:29 UTC
Hi: I am coding to output the result to the tmp file in order to send

to the client. The following is the code I write to output the system

call to a tmp file "tmp.txt". However, when running it, it generate the

message like " Segmentation fault (core dumped)"



Really confused by that . Any comments is welcomed.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...