How to fix the errors with my class functions

My class functions have been giving me errors and I am not sure how to fix it. I'm wondering how to get them to work properly with my main function so my code can compile. My main function code is alright and doesn't have any problems with it. The goal of my code is to do simple transactions and give the user credit card info(Card type, expiration data, and account balance).

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
  
   using namespace std;
  
   class CreditCard
   {
     private:
      string name;
      string card_no;
      int expire_month;
      int expire_year;
      double charge_amt;
      double limit;
      double balance;
      double available_credit();
      string card_type;
      string info();
 
      public:
      CreditCard()
      {
      }
 
      CreditCard(string n, string cn, int em, int ey)
      {
      name = n;
      card_no = cn;
      expire_month = em;
      expire_year = ey;
 
      balance = 0;
      limit = 2000;
      }
 
      void get_card_type()
      {
          if (card_no == "4")
          {
              return "VISA";
          }
          if (card_no == "5")
          {
              return "Mastercard";
          }
      }
      void get_info()
      {
 
      }
 
      bool pre_authorize(int exp_month, int exp_year, double charge_amt)
      {
          if(exp_month)
          {
              exp_month = 12;
          }
          if (exp_year)
         {
              exp_year = 2025;
          }
          return true;
	}
      bool charge(int exp_month, int exp_year, double charge_amt)
      {
              if (pre_authorize = true)
              {
                  charge_amt + balance;
              }
              else return false;
      }
 
 
  };
 
 
  int main()
  {
    CreditCard card("MICKEY MOUSE", "4128002072554673", 12, 2025);
    char input;
    double amount;
    int expmonth, expyear;
 
    do
    {
      cout << "c - Check if a charge will go through" << endl;
      cout << "m - Make a purchase" << endl;
      cout << "s - Show card info" << endl;
      cout << "q - Quit" << endl;
      cout << "Command: " << endl;
      cin >> input;
 
      switch (input)
      {
        case 'c': cout << "Pre-authorize how much? ";
                 cin >> amount;
                 if (card.pre_authorize(12, 2025, amount))
                   cout << "Yes, that will go through" << endl;
                 else
                   cout << "Pre-auth failed" << endl;
                 break;
       case 'm': cout << "Enter expire month: ";
                 cin >> expmonth;
                 cout << "Enter expire year: ";
                 cin >> expyear;
                 cout << "Enter charge amount: ";
                 cin >> amount;
                 if (card.charge(expmonth, expyear, amount))
                   cout << "Approved" << endl;
                 else
                   cout << "Declined" << endl;
                 break;
       case 's': cout << card.get_info() << endl;
                 break;
     }

   } while (input != 'q');

     return 0;
 }
Hello Battlecode06,

Thank you for using code tags. Now if you will post the errors that you are getting. And please the whole error message so everyone will know what is wrong.

Andy
main.cpp:39:22: error: return-statement with a value, in function returning 'void' [-fpermissive]
               return "VISA";
                      ^~~~~~
main.cpp:43:22: error: return-statement with a value, in function returning 'void' [-fpermissive]
               return "Mastercard";
                      ^~~~~~~~~~~~
main.cpp: In member function ‘bool CreditCard::charge(int, int, double)’:
main.cpp:65:19: error: invalid use of member function ‘bool CreditCard::pre_authorize(int, int, double)’ (did you forget the ‘()’ ?)
               if (pre_authorize = true)
                   ^~~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:112:23: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘void’)
        case 's': cout << card.get_info() << endl;
1
2
3
4
5
6
7
8
9
10
11
      void get_card_type()
      {
          if (card_no == "4")
          {
              return "VISA";
          }
          if (card_no == "5")
          {
              return "Mastercard";
          }
      }

The return type of this function is void. That means it doesn't return a value.
If you need to return something, it looks like you should make the return type be 'string'.

1
2
3
4
5
6
7
8
9
string get_card_type()
{
    if (card_no == "4")
        return "VISA";
    if (card_no == "5")
        return "Mastercard";

    return "Unknown";
}


_______________________________

1
2
3
4
5
6
7
8
9
     bool charge(int exp_month, int exp_year, double charge_amt)
      {
              if (pre_authorize = true)
              {
                  charge_amt + balance;
              }
              else return false;
      }
 

(1) = is assignment. You want == (equality test)
(2) if pre_authorize == true, what does this function return?
_______________________________

main.cpp:112:23: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘void’)
        case 's': cout << card.get_info() << endl;

get_info() is also a void function. It does not return anything, so you can't print its value.
Last edited on
Alright thanks for correcting my mistake on the void function. That part should be fixed. All I have left is the info and pre_auth function.
The pre_authorize function is supposed to add a charge to the balance if returned true. In order for it to be true, the exp_month and exp_year must match what is stored in private data. And the charge should be less than the available credit. Then it will return true. Otherwise it will return false.
And I think I have to change get_info() to a string function. But what I'm not sure what to do is how to get the credit card type, expiration date, and account balance in there. I'm using a getter function for this, so the main supposed to automatically pick up what is put inside the function.
Hello Battlecode06,

In addition to what Ganado has said you did not include the header file "<string>". Your IDE may have accepted the definitions using "string", bet the "cout" statement(s) that try to print a "std::string" do not know what to do without the header file.

In the "charge" function '=' means to assign and '==' means to compare. You are trying to assign "true" to a function. Also since the function returns a "bool" all you really need is: if (pre_authorize(... needs parameters)). Doing the compare is just extra unnecessary work. Also I think:charge_amt + balance; should be: charge_amt += balance;. Maybe?

In "main": case 's': cout << card.get_info() << endl;. Again the "get_info" function is a void function. To use it in this line of code the function needs to return something.

Andy
Hello Battlecode06,

Revising my post after I found your responses.

In the "charge" function this: charge_amt += balance; should be changed to: balance += charge_amt;. The order of what is on each side of the (=) makes a difference since it is processed right to left.

Just got the errors fixed and have not had a chance to test it yet.

Andy
Alright thanks Andy for your solutions to my code. I was able to get my code to compile after implementing the changes to my code. The authorization and make a purchase commands work just fine. The get_info() command is only giving me the credit card number. Do you know how to change it so that it outputs the credit card type, expiration date, and account balance? Keep in mind, I did change it from "void" to "string" function.
Hello Battlecode06,

Here are some thing to consider:

Overloaded ctor
1
2
3
4
5
6
7
8
9
10
11
12
CreditCard(string n, string cn, int em, int ey)
{
    name = n;
    card_no = cn;
    expire_month = em;
    expire_year = ey;

    balance = 0.0;
    limit = 2000.0;
    charge_amt = 0.0;  // <--- Added.
    card_type = "UNKNOWN";  // <--- Added.
}

If you are going to give some of the variables a value then give all the variables a value.

You could also write the ctor as:
1
2
3
CreditCard(string n, string cn, int em, int ey) :
    name(n), card_no(cn), expire_month(em), expire_year(ey),
    balance(0.0), limit(2000.0), charge_amt(0.0), card_type("UNKNOWN") {}

Which is considered the better way,

As I scrolled over the "get_card_type" function I realized 2 thing:

First the function does not work as you have it.
Second the overloaded ctor needs to be changed.

You have if (card_no == "4"). To look at it another wayif ("4128002072554673" == "4"). This can never be true.

To do what you need you need this:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string get_card_type()
{
    if (card_no[0] == '4')
    {
        return "VISA";
    }
    else if (card_no[0] == '5')
    {
        return "Mastercard";
    }

    return "UNKNOWN";
}

To compare a single character.

Even this can be shortened to:
1
2
3
4
std::string get_card_type()
{
    return(card_no[0] == '4' ? "VISA" : card_no[0] == '5' ? "MASTERCARD" : "UNKNOWN");
}


Then in the overloaded ctor: card_type(get_card_type()) for the initialization list.

Or in your original code: card_type = get_card_type();

In the "pre_authorize" function your if statements did not work the way you need it to the way it is written. Consider this if statement: if (expire_month == exp_month && expire_year == exp_year). You need to consider what is sent to the function and what it needs to compare to.

Still have the "get_info" function to figure out. Since you are dealing with "double"s you need to include "<iomanip>" with your header files. Then near the beginning of "main" put std::cout << std::fixed << std::setprecision(2);. I put it just before the do/while loop.

Andy
Hello Battlecode06,


The get_info() command is only giving me the credit card number.


You have not posted the updated code for the "get_info" function, so I do not know what you have.

Andy
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
   using namespace std;
 
   class CreditCard
   {
     private:
      string name;
      string card_no;
      int expire_month;
      int expire_year;
      double charge_amt;
      double limit;
      double balance;
      double available_credit();
      string card_type;
      string info();
 
      public:
      CreditCard()
      {
      }
 
      CreditCard(string n, string cn, int em, int ey)
      {
      name = n;
      card_no = cn;
      expire_month = em;
      expire_year = ey;
 
      balance = 0;
      limit = 2000;
      }
 
      string get_card_type()
      {
          if (card_no == "4")
              return "VISA";
 
          if (card_no == "5")
              return "Mastercard";
 
              return "Unknown";
 
      }
      string get_info(string card_type, string exp_month, string exp_year, string balance)
      {
            return card_type;
           // string str1 = to_string(exp_month);
           // string str2 = to_string(exp_year);
           // string str3 = to_string(balance);
      }
 
      bool pre_authorize(int exp_month, int exp_year, double charge_amt)
    {
          if(exp_month)
          {
              exp_month = 12;
          }
          if (exp_year)
         {
              exp_year = 2025;
         }
          return true;
	}
      bool charge(int exp_month, int exp_year, double charge_amt)
      {
              if (pre_authorize(exp_month, exp_year, charge_amt))
              {
                  balance += charge_amt;
              }
              else return false;
      }
 
 
  };
 
 
  int main()
  {
    CreditCard card("MICKEY MOUSE", "4128002072554673", 12, 2025);
    char input;
    double amount;
    int expmonth, expyear;
 
    do
    {
      cout << "c - Check if a charge will go through" << endl;
      cout << "m - Make a purchase" << endl;
      cout << "s - Show card info" << endl;
      cout << "q - Quit" << endl;
      cout << "Command: " << endl;
      cin >> input;
 
      switch (input)
      {
        case 'c': cout << "Pre-authorize how much? ";
                 cin >> amount;
                 if (card.pre_authorize(12, 2025, amount))
                   cout << "Yes, that will go through" << endl;
                 else
                   cout << "Pre-auth failed" << endl;
                 break;
       case 'm': cout << "Enter expire month: ";
                 cin >> expmonth;
                 cout << "Enter expire year: ";
                 cin >> expyear;
                 cout << "Enter charge amount: ";
                 cin >> amount;
                 if (card.charge(expmonth, expyear, amount))
                   cout << "Approved" << endl;
                 else
                   cout << "Declined" << endl;
                 break;
       case 's': cout << card.get_info() << endl;
                 break;
     }
 
   } while (input != 'q');
 
     return 0;
 }
Here's my updated code in the reply above. I tried converting int/double to a string but that failed for me and I was unable to get that to work properly. Maybe I was using to_string() incorrectly.
Last edited on
And I'll make those changes to my string get_card_type() function and the overloaded CreditCard constructor.
Hello Battlecode06,

Looking over your code I realized that the default ctor should initialize at least the numeric variables.

The overloaded ctor should initialize the 2 variables I showed you earlier.

The "get_info" function is a public member function of the class there for it has access to the private variables of the class. Most of the time get function do not take parameters. Also you are trying to make a string out of the wrong variables.

The problem that you are having with the "std::to_string()" function is that you are trying to convert a string into a string. The function only converts numeric variables or values into a string.

I do not know if this is what you want, but I tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string get_info()
{
    std::string info;

    info =
        "\n"
        "         Card type: " + card_type + "\n" + 
        "       Card Number: " + card_no + "\n" + 
        " Eexpiration Month: " +std::to_string(expire_month) + "\n" + 
        "  Eexpiration year: " + std::to_string(expire_year) + "\n";
            
    return info;
}

These variables are from the class and what you are trying to receive in the function parameters should not be there.

This is the output I get with the above code:

 c - Check if a charge will go through
 m - Make a purchase
 s - Show card info
 q - Quit
  Command: s

         Card type: VISA
       Card Number: 4128002072554673
 Eexpiration Month: 12
  Eexpiration year: 2025



While on the subject. Your menu in "main" does not need a "cout" for every line. Also prefer to use the new line(\n) over the function "endl".

1
2
3
4
5
6
7
8
cout <<
    "\n"
    " c - Check if a charge will go through\n"
    " m - Make a purchase\n"
    " s - Show card info\n"
    " q - Quit\n"
    "  Command: ";
cin >> input;

Although this may look like 6 separate strings it is considered just 1 string. The first advantage is that it looks very close to what will be printed on the screes. And it makes changes, additions or deletions much easier.

The second advantage here is the following "cin" statement. This will flush the buffer of any output left before any input is allowed. Notice that the last line does not contain any (\n) or "endl". This put the "cin" on the same line as the prompt. Do notice the space after the (:).

In the "pre_authorize" function you are receiving the correct variables to use, but you are not comparing those variable to the variables in the class. You are returning "true" even if it should be "false". Think about the variables sent to the function and what they should be compared to.

Andy
Alright thanks for the help. I was able to compile the code and it's able to run the expected code after all implemented changes for my constructors and functions.
Based upon previous code, perhaps something like this. Obviously there is work to do with the pre-authorize() function etc.

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>

class CreditCard {
	std::string name;
	std::string card_no;
	int expire_month {};
	int expire_year {};
	double charge_amt {};
	double limit {2000};
	double balance {};

public:
	CreditCard() {}

	CreditCard(const std::string& n, const std::string& cn, int em, int ey) : name(n), card_no(cn), expire_month(em), expire_year(ey) {}

	std::string get_card_type() const {
		if (card_no[0] == '4')
			return "VISA";

		if (card_no[0] == '5')
			return "Mastercard";

		return "Unknown";
	}

	void get_info(std::string& em, std::string& ey, std::string& bal) const {
		em = std::to_string(expire_month);
		ey = std::to_string(expire_year);
		bal = std::to_string(balance);
	}

	bool pre_authorize(int exp_month, int exp_year, double charge_amt) {
		if (exp_month)
			expire_month = 12;

		if (exp_year)
			expire_year = 2025;

		return true;
	}

	bool charge(int exp_month, int exp_year, double charge_amt) {
		if (!pre_authorize(exp_month, exp_year, charge_amt))
			return false;

		balance += charge_amt;
		return true;
	}
};

int main()
{
	CreditCard card("MICKEY MOUSE", "4128002072554673", 12, 2025);
	char input {};

	do {
		std::cout << "c - Check if a charge will go through\n";
		std::cout << "m - Make a purchase\n";
		std::cout << "s - Show card info\n";
		std::cout << "q - Quit\n";
		std::cout << "\nCommand: ";
		std::cin >> input;

		switch (input) {
			case 'c':
			{
				double amount {};

				std::cout << "Pre-authorize how much? ";
				std::cin >> amount;

				if (card.pre_authorize(12, 2025, amount))
					std::cout << "Yes, that will go through\n";
				else
					std::cout << "Pre-auth failed\n";
			}
				break;

			case 'm':
			{
				int expmonth {}, expyear {};
				double amount {};

				std::cout << "Enter expire month: ";
				std::cin >> expmonth;

				std::cout << "Enter expire year: ";
				std::cin >> expyear;

				std::cout << "Enter charge amount: ";
				std::cin >> amount;

				if (card.charge(expmonth, expyear, amount))
					std::cout << "Approved\n";
				else
					std::cout << "Declined";
			}
				break;

			case 's':
			{
				std::string em, ey, bal;
				card.get_info(em, ey, bal);
				std::cout << card.get_card_type() << "  " << em << "  " << ey << "  " << bal << '\n';
			}
				break;
		}

	} while (input != 'q');
}

Topic archived. No new replies allowed.