-1

In this program fill 1D array and... there is no problem with cout in it when we want to print result to file this error happens:

terminate called after throwing an instance of 'std::bad_cast'
  what():  std::bad_cast

Here's the code:

#include <iostream>
#include<cmath>
#include<fstream>

using namespace std;

int main()
{
    ofstream ozon("real.txt");
    double E[100],H[100];
    double a,b;
    a=0.5,b=0.5; 
    int step,l=20;

    cout<<"please Enter step: ";
    cin>>step;

    for(int k=-10;k<=l;k++)
    {
        E[k]=0 , H[k]=0;
    } 

    for(int s=1;s<=step;s++)
    {   
        int roze=0;
        for(int m=0;m<=l;m++)
        {
            E[m]=20*sin(0.314*m);

            for(int n=1;n<=l;n++) 
            {
                float k;
                k= b*( (H[n-1])-(H[n])) ;
                if(abs(k)<0.00001 || abs(k)>10000) k=roze;
                E[n] = E[n] +k ;
            }

            for(int n=1;n<=l;n++) 
            {
                float s;
                s= a*((E[n])-(E[n+1]));             
                if(abs(s)<0.00001 || abs(s)>10000) s=roze;
                H[n] = H[n] + s ;               
            }

        }

    }

    float x,y;
    for(int n=0;n<=20;n++)
    {
        x = E[n] ;
        y = H[n] ;
        cout<<n<<"   "<<x<<"   "<<y<<endl;  
        ozon<<n<<"   "<<x<<"   "<<y<<endl;
    }

    return 0;
}
Shiro
  • 2,610
  • 2
  • 20
  • 36
  • 3
    `int k=-10;`, `E[k]=0 , H[k]=0;`, WAT??? – NathanOliver May 25 '17 at 15:46
  • How did you manage to get a `std::bad_cast` without any `dynamic_cast`s? – DeiDei May 25 '17 at 15:48
  • Sound like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver May 25 '17 at 15:48
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ May 25 '17 at 15:49
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ May 25 '17 at 15:49
  • Why do you have several places where you are forcing a conversion from double to float? I made a few simple changes and your code works. That couldn't have been the only/real error message that you got. What compiler are you using? There are too many WHYs to be listed here. – Dr t May 25 '17 at 15:56

1 Answers1

3

Your code invokes Undefined Behavior here:

for(int k=-10;k<=l;k++)
{
    E[k]=0 , H[k]=0;
} 

Array indexing starts from zero.

gsamaras
  • 71,951
  • 46
  • 188
  • 305