i have a code with algo that fits my problem, but i get this error message when i compile it.. #8 floating point exception [usually caused by accessing memory out of bounds]. who knows what's this?
here is my code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");
    int a;
    fin>>a;
    string S[a];
    int b[a];
    int totalmoney,totalperson,moneyperperson;
    for(int i=0;i<a;i++)
    {
		fin>>S[i];
		b[i]=0;
	}
    
	string buffer;
    for(int l=0;l<a;l++)
	{
		fin>>buffer;
		for(int i=0;i<a;i++)
		{
			if(buffer==S[i])
			{
				fin>>totalmoney;
				fin>>totalperson;
				moneyperperson=totalmoney/totalperson;
				b[i]=b[i]-totalmoney+(totalmoney%totalperson);
				for(int j=1;j<=totalperson;j++)
				{
					fin>>buffer;
					for(int k=0;k<a;k++)
					{
						if(buffer==S[k])
						b[k]+=moneyperperson;
					}
				}
				break;
			}
		}
	}
        
    for(int i=0;i<a;i++)
    fout<<S[i]<<" "<<b[i]<<endl;
    return 0;
}

your haven't initialized the variable "a". And it has to be const, when using it to determine the size of an array.

const int A = 10;
string str[A]; //good
int b = 3;
string str[b]; //error

Edited 7 Years Ago by firstPerson: n/a

You can't do this :

int a;
cin >> a;
string str[a]; //error a is not const

Edited 7 Years Ago by firstPerson: n/a

oh right, i have changed that, so my code now is

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");
    int a;
    fin>>a;
    string S[10];
    int b[10];
    int totalmoney,totalperson,moneyperperson;
    for(int i=0;i<a;i++)
    {
		fin>>S[i];
		b[i]=0;
	}
    
	string buffer;
    for(int l=0;l<a;l++)
	{
		fin>>buffer;
		for(int i=0;i<a;i++)
		{
			if(buffer==S[i])
			{
				fin>>totalmoney;
				fin>>totalperson;
				moneyperperson=totalmoney/totalperson;
				b[i]=b[i]-totalmoney+(totalmoney%totalperson);
				for(int j=1;j<=totalperson;j++)
				{
					fin>>buffer;
					for(int k=0;k<a;k++)
					{
						if(buffer==S[k])
						b[k]+=moneyperperson;
					}
				}
				break;
			}
		}
	}
        
    for(int i=0;i<a;i++)
		fout<<S[i]<<" "<<b[i]<<endl;
    return 0;
}

but i still have the same warning message..

you really don't need to declare the size of the string when you create it. it is good practice to do so but the string will automatically re size itself when it needs to do so. this will make a little performance hit though.

no, i didn't mean to declare the string size.. i made an array of string..

ohh sorry guys, i forgot the exception handling.. there is div by zero here,, in line 31.. thx everyone..

This question has already been answered. Start a new discussion instead.