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.
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 ===|
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:
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.
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
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.
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.
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.
||=== 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 ===|
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 *.
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.