# Beginning Tutorials

• 12-28-2011
Sugar
Beginning Tutorials
My first post.

I am trying to learn C++ and have been reading the tutorials (learn something! Presidential order…seams reasonable). Books are on the way. Anyway, as an exercise, I’ve taken some of the tools presented here and put them into one thing.

Code:

```#include <iostream> using namespace std; int a; void playgame() {   cout << "Play"; } int main() { for (;a=1;) {  int input;  cout<<"1. Multiply\n";  cout<<"2. Devide\n";  cout<<"3. Add\n";  cout<<"4. Exit\n";  cout<<"Selection: ";  cin>> input;  switch (input)  {  case 1:   playgame();   cout<<" Multiplication\n";   {float mult ( int x, int y );   float x;   float y;   cout<<"Please input two numbers to be multiplied: ";   cin>> x >> y;   cin.ignore();   cout<<"The product of your two numbers is "<< x * y <<"\n";   a=1;   }   break;  case 2:   playgame();   cout<<" Devision\n";   {float div (int x, int y);   float x;   float y;   cout<<"Please enter a number for a dividend and a number for a devisor: ";   cin>> x >> y;   cin.ignore();   cout<<"The quotent is "<< x / y <<"\n";   a=1;   }   break;  case 3:   playgame();   cout<<" Addition\n";   {float add (int x, int y);   int x;   int y;   cout<<"Please enter two numbers to add: ";   cin>> x >>y;   cin.ignore();   cout<<"The result is "<< x + y <<"\n";   a=1;   }   break;  case 4:   cout<<"Thank you for playing!\n";   break;   a=0;  default:   a=0;   cout<<"Error, bad input, quitting\n";   break;   }   cin.get();} cout<< a <<endl; }```
Ok, it kinna sorta does what I expected. BUT! I expected it would quite automatically if case 4 were in play. It doesn’t. It just continues to loop. Same with default.
Any hint about what I’m missing? I’ve tried sooooo many things, too many to list here.

Thanks,
Sug
• 12-28-2011
MK27
Code:

```case 4:   cout<<"Thank you for playing!\n";   break;  // I think you know what "break" is supposed to do.   a=0;    // so perhaps this is in the wrong place? ;)```
Quote:

I expected it would quite automatically if case 4 were in play. It doesn’t. It just continues to loop.
Well, here's the loop condition:
Code:

`for (;a=1;)`
When I started programming seriously I had to write a giant:
==
at the top of my keyboard because I made this mistake so often and couldn't see it. That's the equality compare operator. "=" is the assignment operator.

Keep in mind that even if you correct that, you still have the problem I first mentioned.
• 12-28-2011
Elysia
Your indentation is horrible. This is a high priority item to fix.
Also, these
float mult ( int x, int y );
make no sense. They are function declarations, but you have no such functions in your code.
Even if you were to define a function, you can't define them within another function (they must be outside other functions), plus, you never really call them. So review functions.
• 12-28-2011
spaghetticode
Quote:

Originally Posted by Elysia
Your indentation is horrible. This is a high priority item to fix.

Seen much worse around here.
• 12-28-2011
Sugar
Hey! Fixed it! Although it was not something that ever had to be fixed really. I’m not getting paid or graded for this. Nevertheless it is very cool to see it do what I expect it to do. While I am happy about that I realize there is a looooooong way to go. Sure it does what I expect but I do not know why and until I do know why I don’t know jack.

MK27 you were spot on. I did 2 things.

Code:

```int main() { a=1; for (;a==1;)```
And
Code:

```case 4: a=0; cout<<"Thank you for playing!\n"; break;```
Elysia, I looked up the indent thing. I’ll work on it.

There is one thing though…If
float mult ( int x, int y ); makes no sense then why does it work? If I write int mult (intx, inty); and the compile and try to multiply say 3.14159 times 5.5 the result is a computerized hissy fit. But float mult (intx, inty); yields an acceptable result. Therefor it makes some sense, maybe not all the sense available but some portion of sense. That said I will keep all that everyone has put forth here in mind as I push on.

Thanks to all!
~S~!
Sug

Obviously I still don't know how to post code, sorry all.

• 12-28-2011
whiteflags
Quote:

Originally Posted by Sugar
There is one thing though…If [/SIZE][/FONT][COLOR=black][FONT=Verdana]float mult ( int x, int y ); makes no sense then why does it work? If I write int mult (intx, inty); and the compile and try to multiply say 3.14159 times 5.5 the result is a computerized hissy fit. But float mult (intx, inty); yields an acceptable result. Therefor it makes some sense, maybe not all the sense available but some portion of sense. That said I will keep all that everyone has put forth here in mind as I push on.

Ah, post hoc, ergo propter hoc.

The statement
float mult ( int x, int y );
is a function declaration, also known as a function prototype. There is no restriction in the placement of such statements. You can in fact declare functions anywhere, but you shouldn't want to do it exactly like that. Be organized; declare functions in one place at the top of a file. There usually aren't any problems as far as compiling is concerned until you actually call the function. There has to be a definition, somewhere, that explains what mult does.

Code:

```float mult ( int x, int y ) {   return x * y; } // call in client code: float prod = mult( arg, arg2 );```
Like that. Since you didn't actually call mult there was no obvious problem.

To additionally clarify things, I will say that you opted to compute the results inline like this.
Code:

`  cout<<"The product of your two numbers is "<< x * y <<"\n";`
So that protected you from problems.

Basically, my advice is if you want to write functions, please also try to use them. The compiler is very good at tweaking you about errors with functions and their calls. Also please don't focus on making your posts look fancy anymore.
• 12-28-2011
Sugar
Now that is just freaking farout useful.

~S~

Sug
• 12-28-2011
Sugar
"Ah, post hoc, ergo propter hoc."

Looked it up. More to learn here than meets the eye.

~S~!