exception thrown

im working on a program that just copies info from a text file into an array, the info that in the file is
25.86
59.73
62.73
1000.00
3.28
42.87
19.66
-43.27
19.99
23.55

my code is
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
//  main.cpp
//  STATISTICS
//
//  Created by Home on 3/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <exception> 

//length of the array
namespace array {
    int length = 10;
}

using namespace std;

void readinsales(float sales[], int size);

float largest(float sales[]);

float smallest(float sales[]);

float average(float sales[]);

float total(float sales[]);

void floatingSort(int *array);

int main(){
    float account[10];
    int finder = 0;
    ifstream sales;
    sales.exceptions(ifstream::failbit | ifstream::badbit);
    
    try {
        sales.open("sales.txt");
    }
    catch (ifstream::failure e){
        cout << "File not Found!" << '\n' <<"Press ENTER to exit program";
        cin.ignore();
        return 0;
    }
    
    while (!sales.eof()){
        sales >> account[finder];
        ++finder; 
    }
    sales.close(); 
    
    
    return 0;
}

void floatingSort(int *array)
{
    using namespace array;
    int i,j;
    for(i=0;i<length;i++)
    {
        for(j=0;j<length;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swaps the value 
                array[i]=array[j];
                array[j]=temp;
            }
            
        }
        
    }
    
}

and the output is
terminate called throwing an exception(lldb) 

it says this line threw an exception
sales >> account[finder];
and im not sure why this is happening, help appreciated, thanks in advance.
note that the undefined functions will be used later im just going one step at a time making sure it works @ each step.
When end of file is reached together with eof bit fail bit is also set. As you set exception for fail bit

sales.exceptions(ifstream::failbit | ifstream::badbit); you get exception during execution of

sales >> account[finder];

ok now i dont think the program finds the file im looking for, when i try this
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
//  main.cpp
//  STATISTICS
//
//  Created by Home on 3/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <exception>  
//length of the array

namespace array {
    int length = 10; 
}

using namespace std;

void readinsales(float sales[], int size);

float largest(float sales[]);

float smallest(float sales[]);

float average(float sales[]);

float total(float sales[]);

void organize(float *array);

int main(){
    float account[10];
    ifstream sales;
    sales.exceptions(ifstream::badbit);
    
    try {
        sales.open("storage.rtf");
    }
    catch (ifstream::failure e){
        cout << "File not Found!" << '\n' <<"Press ENTER to exit program";
        cin.ignore();
        return 0;
    }
    
    for(int i = 0; i <= 10 ; i++){
        sales >> account[i];
    }
    
    organize(account);
    cout << account[1] << '\n' << account[2]; 
    
    sales.close(); 
    
    return 0;
}

void organize(float *array){
    
    int i,j;
    for(i=0;i<array::length;i++)
    {
        for(j=0;j<array::length;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swaps the value 
                array[i]=array[j];
                array[j]=temp;
            }
            
        }
        
    }
    
}

and the output is
0
0


the file type changed to rtf because im using a mac, i have defined the file as a header rtf in the c++ directly file for this compiler(xcodes) and i have put the file next to my Cpp file in the folder that it is in, and does not find it.
It depends on what is your current directory.
Topic archived. No new replies allowed.