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
//
// 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;
}
usingnamespace 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)
{
usingnamespace 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.
//
// 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;
}
usingnamespace 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.