Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

how to use an if statement with a class

Ok so I have put in a value in   cout << "Enter Transaction Type, ex. Enter D for Deposit or W for Withdrawals";
                    getline(cin,stuff);
            HERE        t.settransactionType(stuff);

Now I want to use an if statement to check what in it, but I'm not sure how to write it. I tried this and got errors. The problem is at the bottom of the page.


tHE CLASS
#include<iostream>
#include<string>

using namespace std;

#ifndef TRANSACTION
#define TRANSACTION

class transaction
{
private:
      string transactionType;
      string transactionAmount;

public:

      void settransactionType(string type);
      string gettransactionType();

      void settransactionAmount(string amount);
      string gettransactionAmount();
};
#endif

METHOD DEFINITIONS
#include "transaction.h"

void transaction::settransactionType(string type)
{
      transactionType = type;
}

string transaction::gettransactionType()
{
      return transactionType;
}

void transaction::settransactionAmount(string amount)
{
      transactionAmount = amount;
}

string transaction::gettransactionAmount()
{
      return transactionAmount;
}




THE FUNCTION
void entertransactions()
{      
      string stuff;
      double totalD;
      double totalW;
      char con = 'y';
      transaction t;
      int index = 0;

      while((con !='n')&&(con !='N'))
      {
            while(index <= 500)
            {
                  cout << "Enter Transaction Type, ex. Enter D for Deposit or W for Withdrawals";
                  getline(cin,stuff);
                  t.settransactionType(stuff);

                  if (t.gettransactionType(stuff) == 'D' && 'd')    //HERE IS WHERE THE PROBLEM IS, IT SAYS FUNCTION DOES NOT TAKE 1 ARGUMENT      
                                               {
                        totalD = totalD + stuff;
                  }
0
stallion5000
Asked:
stallion5000
  • 8
  • 4
1 Solution
 
AxterCommented:
Hi stallion5000,
Try the following:

 if (t.gettransactionType(stuff) == "D" ||  t.gettransactionType(stuff) ==  "d")

Cheers!
0
 
AxterCommented:
Since you're comparing a string, you should  use double quotes.

Also, the logic should be an OR condition, instead of an AND condition.

For each OR condition you need to have an evaluation.
0
 
Jaime OlivaresSoftware ArchitectCommented:
If transaction type will be always just 1 character long, then will be better to declare it as char:

class transaction
{
private:
     char transactionType;
     string transactionAmount;
// etcetera

So you can compare as (single quotes):
if (t.gettransactionType(stuff) == 'D' ||  t.gettransactionType(stuff) ==  'd')
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AxterCommented:
FYI:

For portability between VC++ 6.0 and 7.x, I recomend using double quotes instead.

Single character comparison with std::string works with VC++ 7.x, and it's part of the C++ standard, however it was added late to the standard, and some compilers like VC++ 6.0 do not support it.

You don't gain that much by making it a character comparison, but you loose portability.
0
 
AxterCommented:
Actually, your code should be the following:
 if (t.gettransactionType() == "D" ||  t.gettransactionType() ==  "d")

or with single quotes

if (t.gettransactionType() == 'D' ||  t.gettransactionType() ==  'd')


According to your class, the gettransactionType function does not take an argument.
0
 
Jaime OlivaresSoftware ArchitectCommented:
>You don't gain that much by making it a character comparison, but you loose portability.
There is no portability loss, char is absolutely portable (even more than string).
But you have to accomodate all your code to the new member type:

void transaction::settransactionType(char type)
{
     transactionType = type;
}

char transaction::gettransactionType()
{
     return transactionType;
}

char type is faster and less space consuming than std::string, so when you can use it, you should use it.
Also is not an elegant programming practice to declare all as strings, when you know it is not really a string but a single character.
char type will allow you to make ***fast*** case conversions like this:

void transaction::settransactionType(char type)
{
     transactionType = toupper(type);
}

or

char transaction::gettransactionType()
{
     return toupper(transactionType);
}

I suggest to use the first, so you don't have to make a double comparison every time. Just:

if (t.gettransactionType(stuff) == 'D' )     // you know it is uppercase always
0
 
AxterCommented:
>>There is no portability loss, char is absolutely portable (even more than string).

It's absolutely portable with the current C++ standard (which I did state already)

However, it's not portable with some compilers.
It will not compile on VC++ 6.0.

So it's not even portable with different versions of the same compiler.
0
 
AxterCommented:
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
      string d = "d";
      if (d == 'd')
            cout << "good" << endl;

If you try to compile the above code in a VC++ 6.0 compiler, it will give you the following error:

error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acceptable to the predefined

Single character comparison with std::string is not portable with most compilers created before the C++ first official standard.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Axter,
You are not understanding me, I am not proposing to compare a string with a char. I am proposing to change entirely the
transactionType variable to char, and the managing functions to (settransactionType and gettransactionType).
So comparison is char with char, not string with char, this is portable since K&R C compiler.
0
 
AxterCommented:
void transaction::settransactionType(char type)
{
     transactionType = type;
}

The above also will not compile on VC++ 6.0.

If portability is not an issue, and you never plan on using the code no an older version of the compiler, then this shouldn't be important.

However, if it's a possibility, then you should avoid this type of code.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Again, my answer is the same: not proposing to compare string with char. I have clearly stated that transactionType should be ***char*** so you compare (or assign) char with char.
0
 
AxterCommented:
>>I have clearly stated that transactionType should be ***char***

Sorry, I didn't see that.

Yes, making it a char would make sense if it's value will only ever be a single character.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now