Login Register






Help with Beginner Code filter_list
Author
Message
Help with Beginner Code #1
I am very new to C++. I only barely decided to use my extra time to study C++ in 21 Days, that I have saved in my bookmarks.

I am not even halfway through Day 3 but decided to TRY and write a small calculator from an already written example program.

In advance, there is MUCH missing in this program, I already realize. It's only something I wish to do before continuing in the book.

I wrote it where, in the main menu, you may choose a form of math.

Code:
cout << "Choose one: (M)ultiply, (A)dd, (S)ubtract, (D)ivide: \n"; cout << "Letter of choice: ";

For now I am using Multiply() to experiment with.

The entire main() in progress is:

Code:
/***************MAIN BLOCK***************/ int main() { /*Working redirection to Multiply()*/ cout << "Choose one: (M)ultiply, (A)dd, (S)ubtract, (D)ivide: \n"; cout << "Letter of choice: "; char a; cin >> a; cin.ignore ( 500, '\n' ); //not completely positive what this line is for besides ignoring *blank* if (a == 'M' or 'm') { Multiply(); } system("PAUSE"); return 0; }

Once I enter M, it allows me to enter 2 numbers to multiply as written in Multiply ():

Code:
/***************MULTIPLY BLOCK***************/ int Multiply (int x, int y) { return (x*y); } void Multiply() { int a, b, c; cout << "2 numbers to multiply: "; //for user to enter numbers separated w/spaces cin >> a; //input number cin >> b; //input number c=Multiply(a,b); //calculating step cout << "The product of " << a << " and " << b << " = " << c << "\n"; //returns product to user in text w/number values system("PAUSE"); //to prevent spam of incorrect input /*Working redirection on Multiply()*/ cout << "Another ('Y' or 'N')? "; //attempting to redirect 'Y' to Multiply() and 'N' to cout char d; cin >> d; if (d == 'N' or 'n') { cout << "Have a nice day!\n"; } }
[Image: 14ie4hl.jpg]

The first problem I have that I want to solve before anything is redirecting a 'Y' input back to the Multiply() function. If I use another char such as 'e' and try:

Code:
else if (e == 'Y' or 'y') { Multiply() }

below the first if, no matter what choice I enter it exits the program.


The second problem I have is in main(). It's the exact same problem I'm working in Multiply() but want to use more choices and return them to the function chosen. Again, I can only seem to do it for Multiply() (or any other I replace Multiply() with) but any second, or third input that I have tried before using if to link them to a specific block, has redirected back to Multiply().

It may be baby steps that I am missing but I decided to give it a shot anyway.
[Image: 20t2nn7.jpg]
︻┳═一
HackerExecute
Pwned | Twitter


RE: Help with Beginner Code #2
Code:
/*Working redirection on Multiply()*/ cout << "Another ('Y' or 'N')? "; //attempting to redirect 'Y' to Multiply() and 'N' to cout char d; cin >> d; if (d == 'N' or 'n') { cout << "Have a nice day!\n"; }else if (d == 'Y' or 'y') { multiply(); } }
[username], need some help?, PM me.
[Image: kjKks6Y.png]


RE: Help with Beginner Code #3
(11-21-2012, 12:50 AM)RA1N Wrote:
Code:
/*Working redirection on Multiply()*/ cout << "Another ('Y' or 'N')? "; //attempting to redirect 'Y' to Multiply() and 'N' to cout char d; cin >> d; if (d == 'N' or 'n') { cout << "Have a nice day!\n"; }else if (d == 'Y' or 'y') { multiply(); } }


Same problem.
[Image: 20t2nn7.jpg]
︻┳═一
HackerExecute
Pwned | Twitter


RE: Help with Beginner Code #4
What is the problem exactly?
[username], need some help?, PM me.
[Image: kjKks6Y.png]


RE: Help with Beginner Code #5
You're using System() which is already bad.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


RE: Help with Beginner Code #6
Put the choice of whether the user wishes to continue or not inside a loop in the main() function and not inside the Multiply() function.
Folow me on My YouTube Channel if you're into art.


RE: Help with Beginner Code #7
First: I am not a C++ programmer. But I have a strong guess.

Code:
if (d == 'N' or 'n')

That is like saying if (d=='N') is true or if ('n') is true. The second part (if('n')) is always true, because 'n' != 0.
What you really want to do is this:

Code:
if(d == 'N' or d == 'n')

Same applies to the other if statements.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]


RE: Help with Beginner Code #8
First, it's great that you experiment with what you've already learned, instead strictly following the book that's the best way to learn something very well!

Secondly, in this case, you'll need to go a bit further in the book though, to learn about something called loops.

Loops essentially allow you to repeat a portion of the same code how many times you want, which is exactly what you want to do in this scenario.

------------------------

First this I would do is to move the code that checks if the user wants to end outside of the Multiply function. Remember that in good code, function should always do only what its name says, which in case of Multiply is multiplying. It doesn't care about anything else other than multiplying. Otherwise, as the code grows in complexity, it gets into a mess very quickly.

To keep it simple, make another function that will check if the user wants to end or not and return two value - true or false (these values are called boolean or bool for short), based on user's choice:


Code:
bool Another() { cout << "Another ('Y' or 'N')? "; //attempting to redirect 'Y' to Multiply() and 'N' to cout char d; cin >> d; if (d == 'N' or 'n') return false; else return true; }

Now wrap the main program in a loop called do-while, which fill first execute a piece of your program and then checks a condition in the while statement - basically the function Another, which will ask the user whether to exit and return true or false based on his choice. If his choice is yes, then function returns true, which tells the do-while cycle to repeat the block again and it will keep doing so, until user chooses No.

Code:
/***************MAIN BLOCK***************/ int main() { do { cout << "Choose one: (M)ultiply, (A)dd, (S)ubtract, (D)ivide: \n"; cout << "Letter of choice: "; char a; cin >> a; cin.get(); if (a == 'M' or 'm') { Multiply(); } } while( Another() ); cout << "Have a nice day!"; cin.get(); return 0; }

It should work now. There are other things that could be done better in this code, but I didn't want to change it too much, so it's not too much confusing to you at this point.

(11-21-2012, 06:35 AM)ArkPhaze Wrote: You're using System() which is already bad.

Using system() is not the best way to do it, but neither is the way you said it. He's just beginning with learning this and you slap him in the face with this and walk away. Now that's just rude. Try to explain why is it bad and point to better alternative if you want to be really helpful.

(Now back to HackerExecute): As you can see, I swapped it in the code for cin.get(), which is a better way to wait till user presses enter and also you can use it to filter the new-line character (the '\n') too instead of the cin.ignore(...) function.

You see, the input characters are handled by the operating system that runs your program. All the keys that are pressed are put into some sort of pipe, called stream. On the input of that pipe is the OS, sending all the keys that user pressed into it and on the other end is your program, which can read from it when it needs to.

When user presses a letter and then enter, the system sends into the pipe both the letter and the "Enter symbol". In your program, you read and remove from the pipe only the letter, so the pipe still contains the "Enter key".

So next time you try to read another letter from the input stream, the Enter key is still waiting there, so it will be read instead, which is what you don't want.

If you add cin.get() after the bit of code ("cin >> d" for example) that reads the letter, it will remove the Enter key from the stream, so it's clean again and another letter can be put into it. Simply put, the "New-line character, corresponding to the Enter key" won't be clogging the input pipe anymore.

It also works as waiting. If there's no enter key in the pipe, then cin.get() will wait until there's one, meaning until the user presses the Enter key, then it will continue. If it doesn't wait (meaning there already was an "Enter key" in the pipe, put two cin.get(); in a row. One to clean the pipe up, the other one to wait for the Enter.

------------------------------------------

I hope this helps you and your learning. If you have any questions, feel free to ask :3
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.


RE: Help with Beginner Code #9
I fixed his problem a few days ago via PM. Showed him how to use a switch statement with Char values
[username], need some help?, PM me.
[Image: kjKks6Y.png]


RE: Help with Beginner Code #10
He should have told so. Now Frooxius made a really great effort to help him and it's all for nothing. (But I hope people with similar problems might learn from it)
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]