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;
}

Recommended Answers

All 9 Replies

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

i did, in line 10..

You can't do this :

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

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..

oh sorry well you can do that on the heap then like

//...
int a;
fin >> a;
string * S = new string[a];
//...

[deleted - rereading again more closely]

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.