Help with Beginner Code 11-21-2012, 12:43 AM
#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.
For now I am using Multiply() to experiment with.
The entire main() in progress is:
Once I enter M, it allows me to enter 2 numbers to multiply as written in Multiply ():
![[Image: 14ie4hl.jpg]](http://oi48.tinypic.com/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:
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.
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]](http://oi48.tinypic.com/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]](http://oi49.tinypic.com/20t2nn7.jpg)
![[+]](https://sinister.li/images/modern/collapse_collapsed.png)


![[Image: kjKks6Y.png]](http://i.imgur.com/kjKks6Y.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)