Help Compiling

closed account (z8q4izwU)
Hello i need help compiling this program and headder file. Ive been trying to fix it for the past few hours and thinking i just need another fresh set of eyes to look at it. Heres the errors i get.....

3 In file included from New folder/hw4.cpp
In member function `void sales::purchase(int)':
46 sales.h invalid use of member (did you forget the `&' ?)
sales.h In member function `void sales::cashout()':
67 sales.h `number' undeclared (first use this function)
11 hw4.cpp `cds' undeclared (first use this function)
(same line with 'position', 'sell', 'profit'
20 hw4.cpp `number' undeclared (first use this function)


H file

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
#include <iostream>
#include <cstdlib>

using namespace std;

class sales
{
      private: 
               static const double cd_cost = 3.50;
	           static const double cd_price = 9.50;
	           double cash;
	           int cds;
	           double profit;
	           
      public:
               sales ();
	           void print ();
	           void purchase (int number);
	           void sell (int number);
	           void cashout ();
	           
            };
            
      //constructor      
      sales::sales()
      {
          cash = 100;
          cds = 0;
          profit = 0;
                    
                    }
                    
      //print
     void sales::print()
      {
     cout << "\n" << cash << " Cash";
     cout << "\n" << cds << " CDs";
     cout << "\n" << profit << " Profit" << endl;
                    
                    }
                    
                    
      //purchase
      void sales::purchase(int number)
      {
          purchase = cd_cost * cds;
          cash = cash - (cd_cost * number);
           
           
           }
        //sell   
      void sales::sell (int number)
      {
           
           profit = (cd_cost * cds) - (cd_price * cds); 
           
           }
      //cashout     
      void sales::cashout ()
      {
         if(cash <= cd_cost ){

            cout << "\nThere is not enough cash to purchase that many CDs!\n";

        }else{

            cash = cash - (cd_cost * number);

        }
           
           
           }
           
                    


Main

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
#include <cstdlib>
#include <iostream>
#include "sales.h"


using namespace std;

int main(int argc, char *argv[])
{
    char input[1];
    sales cds, purchase, sell, profit; 
    while(1){


        cout << "\Enter b (buy), s (sell), c (cash out), p (print), or q (quit) :";
        cin >> input;
        if(input[0] == 'b'){
            cout << "Enter the number of CDs to buy: ";
            cin >> cds;
            purchase (number);            

        }else if(input[0] == 's'){

            cout << "Enter the number of CDs to sell:  ";
            cin >> cds;
            sell (number);
            cout << "\n" << s.cds << "CDs sold for" << profit;  

        }else if(input[0] == 'c'){

            cout << cds << "CDs purchased for " << profit ;
            

        }else if(input[0] == 'p'){

            cout << "Current Sales Information: ";
            

        }else if(input[0] == 'q'){

            cout << "\nWhat a sell-out!\n";
            break;

        }else{

            cout << "\nThere is not enough cash to purchase that many CDs!\n";
            }
            }
            

    
    system("PAUSE");
    return EXIT_SUCCESS;
}
I wonder why do not you read error messages yourself? Do you want that it will be us how will read error messages instead of you? What does this code means?

1
2
3
      void sales::purchase(int number)
      {
          purchase = cd_cost * cds;
Last edited on
number' undeclared 
Exactly what it says. You didn't bother to declare it anywhere.
invalid use of member (did you forget the `&' ?)
same here actually. Compiler got confused because there was purchase function.
closed account (z8q4izwU)
So i fixed my code working on this for hours and making stupid mistakes but i dont understand this error code. Can anyone explain it?

28 hw4.cpp no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+(+std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"\n")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](cds))), ((const char*)"CDs sold for")) << profit'
no match for 'operator<<'
exactly what it says: you didn't provided overloaded stream output operator << for your class.
Topic archived. No new replies allowed.