Can i do this in an if statement?

Pages: 12
I need to do see if the user inputs something and i am using an if statement but it gives me one error:

1
2
3
4
if(choice == v.Names() and "+" and v.RNums())
{
     cout << "Good Job!" << endl;
}


So this if statement checks to see if the player entered the name of the person, a plus sign and the amound of money. so it would look like this:

John+$375

but i get this error:


C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp||In member function 'void Vars::MainProgram()':|
C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp|141|error: no match for 'operator==' in 'choice == v.Vars::Names()'|
||=== Build finished: 1 errors, 0 warnings ===|


My program is pretty big so if you need to see the full code let me know and i'll post it.
Your if statement rewritten:
if ((choice == v.Names()) && ("+") && (v.RNums()))

In plain English:
If choice is equal to the returned value of v.Names()
AND
"+" (which will always evaluate to true)
AND
v.RNums() (if it returns a non zero integer or true, it's true)

Lets take a look at the first part. (choice == v.Names())
I'm assuming choice is an integer. You're trying to compare it to a function. What is the return type of that function? If it's not int, you might get an unexpected result. If it's not a numerical data type, you might get an error, similar to what you got.

As for the second part. ("+")
I believe you want to check that the character + exists within a string. You can use a bunch of functions for this, but .search is the easiest, IMHO, but requires you to be using std::string, which I don't believe you are from looking at your previous posts.

As for the third part. (v.RNums())
Again, the function should return a value other than 0 or false to get a true condition. If the function returns a string other than "" it will also evaluate to true.

Looking over your other recent threads, I believe you're jumping into classes way before you learned enough of the basics and that is what is resulting in so many of your errors. I'd suggest taking a step back, learning about C++ strings, function return types, and conditional statements.
It still gives me the same error. choice is a string, i tried changing it to an int but that gave me more errors. Im actually understanding stuff alot better now. I find myself not having to ask questions more frequently than i used to. i'll look up some class stuff and see if i can find out more stuff, but still help me with this please.
You have three choices.
Logical AND &&,
switch statement,
if statement hiarchy (i.e. if() within an if() block)

by the way, your code makes no sense. What does the "+" serve in line 1 in your code?
Last edited on
Because the player is supposed to be withdrawling or depositing money, so if the program chooses withdrawl then the player has to type in the persons name the plus sign and the amount of money.

I did change it and i still get an error:

1
2
3
4
5
if ((choice == v.Names()) && ("+") && (v.RNums()))
                {
                    xp += 1;
                    money += 20;
                }



C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp||In member function 'void Vars::MainProgram()':|
C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp|141|error: no match for 'operator==' in 'choice == v.Vars::Names()'|
||=== Build finished: 1 errors, 0 warnings ===|
Last edited on
Since you're using a string, you're going to need to learn how to parse a string. There is plenty of features and functions when using the C++ strings as opposed to the C strings. This will allow you to parse a string, IMHO, much easier than as if you were going to try to parse the string using just the C string functions.

That aside, the reason that I said that you needed to look at classes again is because your concept of them is misguided. A class should define a group of variables and functions under one name. You can then create an object that has all of those properties and access the different properties via functions (typically referred to as methods).

I point this out because just looking at the if statement you posted, v is obviously an object of a class that you made by yourself (quite possibly a third party library that I don't know of as well) and that you're more than likely trying to access the object within the class itself. This is unnecessary since classes have the implied "this" pointer that points to the object that is being used. You can use "this" to access different parts of the object. A quick example follows:
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
class myClass {
   private:
      int mSize;
      int mApples;
   public:
      // Constructor
      myClass() :
         mSize(0),
         mApples(0) {}
      void IncreaseSize() {
         // this is implied
         mSize ++;
         // You wouldn't want to do this
         // myObject.mSize ++;
      }
      void AddApples() {
         // The following code
         this->mApples ++;
         // is the same as writing this
         // mApples ++;
      }
};

int main() {
   myClass myObject;
   myObject.IncreaseSize();
   myObject.AddApples();

   return 0;
}


The above is a quickly written example to show you how to access members within the class itself. Unless you're passing an object to the class (which I'm not sure why you'd need to), then using a dot operator in a class's definition isn't necessarily needed. There is a lot of things that might need to be clarified for you.

I feel that you should post your entire code so we can help you out. If it's quite lengthy (several hundred lines) I'd suggest using a free pasting site like pastebin.com or codepad.org.
You must understand the C++ syntax.
Take for instance the && operator. It is actually a function in disguise. Syntactic sugar.
&& takes two operands, and if both of them are a nonzero value, the function returns a boolean true, or false otherwise.

Take if( 2 && "+") for instance.
2 is a nonzero, and "+" is actually a pointer to a null-terminated string (also nonzero), so your statement is nonsensical.

You're going to have to do the grindwork and compare the strings if you want to get this if statement down.

We still don't know the datatype of the symbols: choice , Names , and RNums
And I can only guess that v is a class.
Ok, here is my code, its only 295 lines:

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <iostream>
#include <string>
#include <fstream>
#include <random>
#include <ctime>

using namespace std;

class Vars
{
    public:
        void MainProgram();
        void Names();
        void Options();
        void RNums();
        void Save();
        void Load();
        void setStrings(string PNAME, string BNAME)
        {
            pname = PNAME;
            bname = BNAME;
        }
        string getPname()
        {
            return pname;
        }
        string getBname()
        {
            return bname;
        }

        void setIntegers(int MONEY, int XP)
        {
            money = MONEY;
            xp = XP;
        }
        int getMoney()
        {
            return money;
        }
        int getXp()
        {
            return xp;
        }
    private:
        string pname;
        string bname;
        int money;
        int xp;
};


int main()
{
    Vars v;
    string pname, bname;
    string choice;

    cout << "1) Load" << endl;
    cout << "2) New" << endl;
    cin >> choice;

    if(choice == "1")
    {
        v.Load();
        v.MainProgram();
    }
    else if(choice == "2")
    {
        while(choice != "1")
        {
            cin.sync();
            cout << "Please enter the name of your bank (dont put bank in the name)" << endl;
            getline(cin, bname);

            cout << "\n";

            cout << "Please enter your name" << endl;
            getline(cin, pname);

            cout << "\n";

            cout << "So your Bank name is " << bname << " ?\n" << endl;

            cout << "and your Name is " << pname << " ?\n" << endl;

            cout << "Please enter 1 for YES or 2 for NO" << endl;

            cin >> choice;

            if(choice == "1")
            {
                v.setStrings(pname, bname);
                v.MainProgram();
                break;
            }
        }
    }
}


void Vars::MainProgram()
{
    Vars v;
    int TIME;
    string choice;

    time_t T;
    ctime(&T);
    srand(time (NULL));

    for(int r = 0; r < 10; ++r)
    {
        TIME = rand() % 1;
    }

    v.setStrings(pname, bname);

    cout << "Welcome " << pname << " to " << bname << " bank" << endl;
    cout << "to play Enter the name of the person followed" << endl;
    cout << "by a plus or minus sign depending on if its a withdrawl or" << endl;
    cout << "deposit. Enter it EXACTLY like this: John+$475" << endl;
    cout << "Press ENTER to begin\n" << endl;
    cin.sync();
    cin.get();

    while(choice != "end" || "End")
    {
        switch(TIME)
        {
            case 0:
                cout << "Hi my name is ";
                v.Names();
                cout << " and I would like to make a ";
                v.Options();
                cout << "\n";
                cout << "Of $";
                v.RNums();
                cout << " please\n" << endl;
                cin >> choice;
                if ((choice == v.Names()) && ("+") && (v.RNums()))
                {
                    xp += 1;
                    money += 20;
                }
                cin.sync();
                cin.get();
                break;
        }
    }
}


void Vars::Names()
{
    int TIME2;

    time_t T;
    ctime(&T);
    srand(time (NULL));


    for(int r = 0; r < 13; r++)
    {
        TIME2 = rand() % 13;
    }

    switch(TIME2)
    {
        case 0:
            cout << "John";
            break;
        case 1:
            cout << "Mark";
            break;
        case 2:
            cout << "Angela";
            break;
        case 3:
            cout << "Tiffany";
            break;
        case 4:
            cout << "Alex";
            break;
        case 5:
            cout << "David";
            break;
        case 6:
            cout << "Steven";
            break;
        case 7:
            cout << "Becky";
            break;
        case 8:
            cout << "Bethany";
            break;
        case 9:
            cout << "Alexis";
            break;
        case 10:
            cout << "Martin";
            break;
        case 11:
            cout << "Samantha";
            break;
        case 12:
            cout << "Stephanie";
            break;
    }
}


void Vars::Options()
{
    int TIME3;

    time_t T;
    ctime(&T);
    srand(time (NULL));


    for(int r = 0; r < 2; r++)
    {
        TIME3 = rand() % 2;
    }

    switch(TIME3)
    {
        case 0:
            cout << "Withdrawl";
            break;
        case 1:
            cout << "Deposit";
            break;
    }
}


void Vars::RNums()
{
    int Random;
    int TIME3;
    time_t T;
    ctime(&T);
    srand(time (NULL));

    TIME3 = rand() % 4;
    Random = rand() %500;

    switch(TIME3)
    {
        case 0:
            cout << Random;
            break;
        case 1:
            cout << Random;
            break;
        case 2:
            cout << Random;
            break;
        case 3:
            cout << Random;
            break;
    }
}


void Vars::Save()
{
    Vars v;
    v.setStrings(pname, bname);
    v.setIntegers(money, xp);

    ofstream file("MM.txt");

    file << pname << endl;
    file << bname << endl;
    file << money << endl;
    file << xp << endl;
}

void Vars::Load()
{
    Vars v;
    v.setStrings(pname, bname);
    v.setIntegers(money, xp);

    ifstream file("MM.txt");

    file >> pname;
    file >> bname;
    file >> money;
    file >> xp;

}
Just taking a glance at it, I noticed you made the same mistake with the logical OR operator || on line 127:
while(choice != "end" || "End")
This statement is equivalent to while(true)
"End" is a nonzero value, so it boils down to while(choice!= "end" || true)
RNums() and Names() is also very dangerous in that if() statement, as it doesn't return anything. See line 13, 15, 154, and 239.
It's like saying true && void

That said, try to compare the strings.
Last edited on
This program really needs a rewrite altogether. First, you should come up with a more meaningful class name, possibly bank. Second, just looking at your last method, Load, you technically load the data correctly, but you go about it incorrectly.

When you call Load, you create a new object, v based on the same class you're using. You set the strings pname and bname to the current versions of the same strings, repeat for the integers, and then you open a file, read the variables into the local one, and v is destroyed. v does nothing in those functions other than cause unnecessary overhead (a good compiler will see this and remove it altogether from the final program).

Same goes for a lot of your other methods. What I suggest is sitting down and designing your class. Start with what variables are absolutely necessary for your class, then what functions you need to use to access those variables. I've always been an advocate of making classes not use iostream in any sense. The only streaming my classes should do is save/load information. Even then, I have believe more about overloading the stream operators instead since it's much easier to look at it that way.

Another thing that I noticed was your Names method. I don't believe that is necessary at all. I suggest, instead, is to use a string array to hold your names, and use a function called GetName that returns the string randomly selected from the array. Again, this goes back to the design aspect of the class.

I'm more than willing to help you with this code overall, but it does need a good bit of attention to minor details, specifically, you shouldn't need to create an object of the class you're working in (but of course there is always exceptions, but this isn't one of those exceptions).

I hope I haven't discouraged you, and I hope you're willing to take the initiative to improve your code and in turn improve your knowledge on the subject as well.
I'm not discouraged at all, i want to learn how to do this stuff the right way and i'll spend as much time on it as i can, i definetley want to improve my code and knoweledge of C++ as i hope to one day make games with it, but i have to get better at it first. Any help is greatly appreciated.
bump
Sorry, I forgot to reply last night. But I'd suggest looking at what a class really is. I design my classes to do all of the heaving loading, while my main function does the "unique" part of the program. Taking your program, for example, I would set up my class to have a list of "employees" and it can set the current employee to a random one from that list. I'd also suggest overloading the stream operators for easy input/output.

I'd also go back and look at how the main function and your class should interact. Do you want a lot of coding in main, or would you prefer to be able to slightly modify main to reflect changes in your class? What is required for your class? What isn't?

A lot of these questions will get you onto the right track of writing an efficient program. I'm not the best, but through the years of learning, I have picked up on some rather, albeit trivial, small pieces of programming design (the way my code is laid out and how it interacts with each other). This will help you further down the road when you decide to edit this code, or when you decide to write code from scratch.
Ok, is there any good videos on that stuff? I have ADHD so trying to concentrate and read articles online is almost impossible for me to do. It probably doesnt help that i have a learning disability either. Videos are what seems to help the most. Thats how i learned what i know so far, along with help from forums like this one.
*I didnt read all of the posts above, so i might be repeating some people*

that function could be written as:

1
2
3
4
if(choice == v.Names() && choice ==  "+" && choice == v.RNums())
{
     cout << "Good Job!" << endl;
}


without the "choice ==" line the if statement tries to check if the + and v.RNums return true.

Also, your class functions do not return a value. So calling a function that returns no value during the if statement results in:

if(choice == ??)

there is no value to be returned, so unless you overload the '==' operator the compiler returns the error. To prevent this, you would define your functions to return a value that the if statement can compare.
Last edited on
that still doesnt work for some reason


||=== Money Manager, Debug ===|
C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp||In member function 'void Vars::MainProgram()':|
C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp|144|error: no match for 'operator==' in 'choice == v.Vars::Names()'|
C:\Users\Chay Hawk\Desktop\Money Manager\main.cpp|144|error: no match for 'operator==' in 'choice == v.Vars::RNums()'|
||=== Build finished: 2 errors, 0 warnings ===|
Last edited on
closed account (zb0S216C)
Both Vars::RNums() and Vars::Names() return void. Therefore, comparing a std::string to a non-existent piece of data makes no sense. Both of the said member functions need to return something that's compatible with std::string, such as char *.

Wazzak
Last edited on
ok so what would i do, how would that look? can you give me an example.
1
2
3
4
5
6
7
8
9
std::string Vars::Names()
{
      std::string Name;
      switch(data)
      {
                 //do work
      }
      return Name; //this returns a value for the if statement to compare
}


This will validate your program and let it move on to hopefully run(or get to the next error).
Last edited on
Ok so i changed Names and RNums to strings and it all works, what does changing the function type do? why are some fucntions ints, strings, chars and void? what is the difference and why is it useful?

EDIT: when i get to the function that has the changes in it, the program crashes.
Last edited on
Pages: 12