Program won't run properly

Hey, I wrote this program with the help of a few people. I build the program and I get 3 warnings and no errors. All of the read "warning C4305: 'argument' truncation from double to float. I don't think this is the exact problem though. I would think that this program should run normally but the answers might be off. When I run the program and the black screen thing pops up, it shows nothing, just the blinking white thing at the top. I'm thinking it may be my computer and not the program but at the same time it works for everything else just not this one. So I'm thinking it may be the program or it is the computer that I'm using. Can anyone help me out, perhaps tell me if there is something wrong with my program that is causing it to just sit there without outputting anything. Thanks for the help.

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

class SalesRecord
{
private:
float *sales_amount;
int number_of_sales;

public:
SalesRecord(int howmanysales);
SalesRecord( const SalesRecord& othersales );
~SalesRecord();
SalesRecord& operator= ( const SalesRecord& othersales ) ;
void StoreSale(float newsales, int index);
float FindSale(int index);


};

Main.cpp //::According to the compiler, this is where my problem lies yet everything looks good to me. Would this cause the program to not work at all?

[code]#include<iostream>
#include "salesrecord.h"
using namespace std;
int main( )
{
SalesRecord s1(4), s2(3);
s1.StoreSale(2.4, 0); s1.StoreSale(8.7, 1);
s1.StoreSale(6.3, 2); s1.StoreSale(3.5, 3);
s2=s1;
s1.StoreSale(3.5, 0);
cout << s2.FindSale(0)<<endl;
cout << s2.FindSale(1)<<endl;
cout << s2.FindSale(2)<<endl;
cout << s2.FindSale(3)<<endl;
}


Salesrecord.cpp

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
#include<iostream>
[code]#include "salesrecord.h"
using namespace std;

[code]SalesRecord::SalesRecord(int howmanysales)
{
int i;
if (howmanysales<1)
{
cerr << "invalid size: ";
exit(1);
}
sales_amount=new float[howmanysales];
int number_of_sales=howmanysales;

for (i=0; i<number_of_sales; i++)
{
sales_amount[i]=0;
}
}
SalesRecord::SalesRecord( const SalesRecord& othersales )
{
number_of_sales = othersales.number_of_sales ;


sales_amount = new float[number_of_sales] ;
for( int i=0 ; i<number_of_sales ; ++i )
sales_amount[i] = othersales.sales_amount[i] ;
}


SalesRecord::~SalesRecord()
{
delete[] sales_amount;
}


void SalesRecord::StoreSale(float newsales, int index)
{
sales_amount[index]=newsales;
}

float SalesRecord::FindSale(int index)
{
return sales_amount[index];
}
SalesRecord& SalesRecord::operator= ( const SalesRecord& othersales )
{

if( sales_amount != othersales.sales_amount )
{
delete [] sales_amount ;

sales_amount = new float[number_of_sales] ;
for( int i=0 ; i<number_of_sales ; ++i )
sales_amount[i] = othersales.sales_amount[i] ;
}
return *this;
}
At first glance your assignment operator is incorrect and I think it is used in a different way then you were expecting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SalesRecord& SalesRecord::operator= ( const SalesRecord& othersales )
{
    if( this !=&othersales)
   {
        if(number_of_sales != othersales.number_of_sales)
        {
             delete [] sales_amount ; //Always initialize your pointers with 0.
             number_of_sales = othersales.number_of_sales;
             sales_amount = new float[number_of_sales] ;
        }
        for( int i=0 ; i<number_of_sales ; ++i )
           sales_amount[i] = othersales.sales_amount[i] ;
    }
    return *this;
}


Fix this part and lets try this again, it doesn't answer your question.

Also inside your constructor you declare and initialize a local variable int int number_of_sales=howmanysales; which takes scope precedence over your member variable of the same class. Meaning, salesrecord::number_of_sales is actually never being set. Fix your assignment operator and fix this and you should be good to go.
Last edited on
Topic archived. No new replies allowed.