Program closing unexpectedly

I'm a little rusty this isn't making sense to me. I've created an array of 5 values, put values in them, and I'm trying to sum them. I used a switch statement because I'm going to be doing other things with them. It compiles fine but it just stops working after I click the button and says it has exited with code 1073740791. Any help with be greatly appreciated.


private: void go(int i, float arr[])
{
float tmp = 0;
switch (i)
{
case 0:
for(int j=0; j!=5; j++)
tmp += arr[j];
total->Text = tmp.ToString();
break;
}
Wait a minute... this shouldn't even compile, unless this is C# or some non-standard C++ variant. floats don't have a ToString() member function because they aren't class-like types... :/

I'd suggest creating a temporary stringstream object with the contents of tmp, and using >> to read that into total->Text, which does the type cast automatically.

-Albatross

Last edited on
closed account (S6k9GNh0)
That's beyond the point! I suggest you run over a tutorial concerning function declaration and perhaps something on primitives in C++.
You are relying the size of arrat arr[] to be 5, as you are running a loop. And , the array might not have enough memory

Put your class over here to get better understanding.


Gorav
http://www.kgsepg.com
closed account (z05DSL3A)
Albatross wrote:
Wait a minute... this shouldn't even compile, unless this is C# or some non-standard C++ variant.

It is probably C++/CLI.

Assuming that is an edited version of the code and is missing a brace because of it:
1
2
3
4
5
6
7
8
9
10
11
12
private: void go(int i, float arr[])
{
    float tmp = 0;
    switch (i)
    {
        case 0:
            for(int j=0; j!=5; j++)
                tmp += arr[j];
            total->Text = tmp.ToString();
            break;
    }
} // <-- missing?? 

It does not look like it should have a problem.

Edit:
oh yes, code 1073740791 (0xc0000409) is an 'unknown software exception'. This can sometimes be caused by a buffer overrun but I would not like to guess at this point. Maybe post more code and give us more info.


Last edited on
I'm not exactly sure what to call it. I made it in visual studio '10 and it's a windows forms application under CLR. Anyway here's the rest of the code. I tried commenting out the go function and doing it inside the button click but I get the same result.
#pragma endregion

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
float collections[5];
float temp = 0;
String^ out;
float::TryParse(collect0->Text, temp);
collections[0] = temp;
float::TryParse(collect1->Text, temp);
collections[1] = temp;
float::TryParse(collect2->Text, temp);
collections[2] = temp;
float::TryParse(collect3->Text, temp);
collections[3] = temp;
float::TryParse(collect4->Text, temp);
collections[4] = temp;
float::TryParse(collect5->Text, temp);
collections[5] = temp;

int tmp = 0;
switch (0)
{
case 0:
for(int j=0; j!=5; j++)
tmp += collections[j];
total->Text = tmp.ToString();
break;
}

}/*
private: void go(int i, float arr[])
{
float tmp = 0;
switch (i)
{
case 0:
for(int j=0; j!=5; j++)
tmp += arr[j];
total->Text = tmp.ToString();
break;
}
}*/
closed account (z05DSL3A)
That is C++/CLI.

The problem is that collections is an array of 5 elements and you are trying to load it with 6 elements.

Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
float collections[6]; /// <----
float temp = 0;
String^ out;
float::TryParse(collect0->Text, temp);
collections[0] = temp;
float::TryParse(collect1->Text, temp);
collections[1] = temp;
float::TryParse(collect2->Text, temp);
collections[2] = temp;
float::TryParse(collect3->Text, temp);
collections[3] = temp;
float::TryParse(collect4->Text, temp);
collections[4] = temp;
float::TryParse(collect5->Text, temp);
collections[5] = temp;

int tmp = 0;
//switch (0)  //switch not needed unless you have other code not shown
//{
//case 0:
for(int j=0; j <6; j++) //<----
tmp += collections[j];
total->Text = tmp.ToString();
//break;
//}

}/*
private: void go(int i, float arr[])
{
float tmp = 0;
switch (i)
{
case 0:
for(int j=0; j!=5; j++)
tmp += arr[j];
total->Text = tmp.ToString();
break;
}
}*/
NB: untested code
Last edited on
Topic archived. No new replies allowed.