?
Solved

how to use an if statement with a class

Posted on 2005-02-25
12
Medium Priority
?
220 Views
Last Modified: 2010-04-24
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
Comment
Question by:stallion5000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13406436
Hi stallion5000,
Try the following:

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

Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13406447
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13406528
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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
LVL 30

Expert Comment

by:Axter
ID: 13406624
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
 
LVL 30

Expert Comment

by:Axter
ID: 13406665
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13406682
>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
 
LVL 30

Expert Comment

by:Axter
ID: 13406737
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 13406763
#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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13406813
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
 
LVL 30

Expert Comment

by:Axter
ID: 13406816
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13406838
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
 
LVL 30

Expert Comment

by:Axter
ID: 13406894
>>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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question