Originally Posted by
sharris
I just submitted the code below. It's a little better and I think it works. This was not a easy thing to do. I had to resort to stop using the brain and went gun-ho on trial and error. So many re-do's, I all most lost the one that worked, if it works.
cpjust, I got rid of the call back to MAIN and made a procedure for it but it's hard not to have three of the same type functions to complete such instructions. Recursion could have been a better choice for such an operation. I hope soon that you or someone will post the best way of doing such a thing. I would really like to see how code for completing these type steps should have been written. Thanks cpjust
You made your point loud and clear to me so I re-read about WHILE and DO-WHILE. I like the use of [DO] now that I have a better understanding of how it works. Thanks a lot rags_to_riches
Thanks Tropod, I will be working on my indentation habits, but I like pushing some parts of the code far out so when I need to go back I catch the point I made 100x quicker.
I'm going to give you your program, re-written to be a lot cleaner and less clunky. You were heading in the right direction, but I think your thought process wasn't quite grasping what you needed to do. As an excersize, I've left you a couple places where the program needs improvements - and it also won't compile straight away (there's a hidden bug or two). Just pay attention to the code and you should spot what needs to be 'fixed'.
Heavily commented to try to help you figure out what's going on.
Code:
#include <iostream>
//using namespace std; -- NO NO NO. Never do this. It pollutes the global namespace.
int do_Multiplication(int first, int second);
// Using global variables is usually a bad idea.
// Only use them when there's no other option.
int main()
{
int first[3] = {3, 3, 1};
int second[2] = {2, 0, 0};
// FIXME: This is a fixed length loop.
// Can you write it in such a way that it can be any length?
for (int x = 0; x < 3; ++x)
{
// You can avoid globals by passing in parameters
// and using return values wisely.
int ret = do_Multiplication(first[x], second[x]);
if (ret != -1) // Check if the user wants to exit.
{
// compare(), inlined.
if (ret = (first[x] * second[x]))
std::cout << "\nVery good!\n" << std::endl;
else
{
std::cout << "\nTry again.\n" << std::endl;
--x;
}
}
else
break; // Here's where we stop execution if -1 is entered.
}
std::cout << "\nThat's all for now. Have a nice day!\n" << std::endl;
std::cin.ignore(); // Flush the extra \n from the buffer.
std::cin.get(); // Pause execution until keypress.
return(0);
}
int do_Multiplication(int first, int second)
{
int retval;
std::cout << "How much is " << first << " x " << second << "? (-1 to exit): ";
// FIXME: There's no error checking here to make sure a valid number was entered.
// In a well-formed application, there would be. As an excersize for you,
// write error checking code. This is also known as, "Never trust user input."
std::cin >> retval;
return(retval);
}