Anyone good at c++?

Recommended Videos

ethaninja

New member
Oct 14, 2009
3,144
0
0
Ok so I've been starting out with some c++. Anywho, I just finished writing a program (what the program does is irrelevant, it's a useless program to everyone else) and there are no errors in it at all, but there are two sections to it (calculator and age input), and it won't go to the second one one (Age input)

Anybody wish to take a crack at it?
45 lines, so not much at all. Most of those lines are brackets and spaces.

#include
#include

using namespace std;

//Two test functions I'm trying out for a functions tutorial. The first one multiplise two numbers
//the second one gets age input from the user. Both completely irrelevent to each other.
int calc (int num1, int num2);
int age (int x);

int main()
{
int num1;
int num2;

cout<<"Please insert two " <<"numbers you wish to be multiplied\n";
cin>>num1 >>num2;
cout<<"The answer is " <<calc (num1, num2) <<"\n";
cin.ignore();
}

int calc (int num1, int num2)
{
return num1 * num2;
}

int age (int x)
{


cout<<"Enter your age";
cin>>x;

if (x < 50 ){
cout<<"Yay";
}

else if (x > 50 ){
cout<<"Nooo";
}

else if ( x == 50 ){
cout<<"Huh?";
}

return x;
}
 

Cpt. Red

New member
Jul 24, 2008
531
0
0
You never call the age function...
EDIT:
Also why does age have a parameter when you never use it and ask for input in the function?
 

randomsix

New member
Apr 20, 2009
773
0
0
Cpt. Red said:
You never call the age function...
EDIT:
Also why does age have a parameter when you ask for input in it?
This. You need to call it after you have cin.ignore in your main.
 

Lukeje

New member
Feb 6, 2008
4,048
0
0
Yup, you need to call
Code:
age()
.

...your program should also return a value (typically zero) from main. This tells whatever shell that you're running the program in that execution went as planned.
 

ethaninja

New member
Oct 14, 2009
3,144
0
0
randomsix said:
Cpt. Red said:
You never call the age function...
EDIT:
Also why does age have a parameter when you ask for input in it?
This. You need to call it after you have cin.ignore in your main.
Ah that would explain a lot. Now what is the line for that? :p
 

RathWolf

New member
Apr 14, 2009
326
0
0
ethaninja said:
randomsix said:
Cpt. Red said:
You never call the age function...
EDIT:
Also why does age have a parameter when you ask for input in it?
This. You need to call it after you have cin.ignore in your main.
Ah that would explain a lot. Now what is the line for that? :p
I'm assuming you'd be calling age() after the cin.ignore in main. As is, the code just runs through main and never calls age().

Also, like the others mentioned, I don't see a reason to have a parameter for age().

Also, you don't really need to use cin.ignore unless you're doing combination of cin and getline.
 

randomsix

New member
Apr 20, 2009
773
0
0
ethaninja said:
randomsix said:
Cpt. Red said:
You never call the age function...
EDIT:
Also why does age have a parameter when you ask for input in it?
This. You need to call it after you have cin.ignore in your main.
Ah that would explain a lot. Now what is the line for that? :p
Hell if I know. I write java and VB, not c++.
 

burningdragoon

Warrior without Weapons
Jul 27, 2009
1,935
0
0
Oh I can hel.... oh beaten to it of course. But yeah just add a call to age on the next line in your main method.

also why is
cout<<"Please insert two " <<"numbers you wish to be multiplied\\n";
separated into sections? Unnecessary as far as I can tell.

Edit: Oh and unless I'm mistaken, a better form would be:

cout << "Please insert two numbers you wish to be multiplied" << endl;
 

ethaninja

New member
Oct 14, 2009
3,144
0
0
burningdragoon said:
Oh I can hel.... oh beaten to it of course. But yeah just add a call to age on the next line in your main method.

also why is
cout<<"Please insert two " <<"numbers you wish to be multiplied\\n";
separated into sections? Unnecessary as far as I can tell.
Yeah I didn't do it for any major reason. I was just making sure I had it down right.
 

burningdragoon

Warrior without Weapons
Jul 27, 2009
1,935
0
0
ethaninja said:
burningdragoon said:
Oh I can hel.... oh beaten to it of course. But yeah just add a call to age on the next line in your main method.

also why is
cout<<"Please insert two " <<"numbers you wish to be multiplied\\n";
separated into sections? Unnecessary as far as I can tell.
Yeah I didn't do it for any major reason. I was just making sure I had it down right.
Gotcha. It's good to know you can do that anyway. Also, see edit I made on the previous post. Another no biggie.
 

ethaninja

New member
Oct 14, 2009
3,144
0
0
burningdragoon said:
ethaninja said:
burningdragoon said:
Oh I can hel.... oh beaten to it of course. But yeah just add a call to age on the next line in your main method.

also why is
cout<<"Please insert two " <<"numbers you wish to be multiplied\\n";
separated into sections? Unnecessary as far as I can tell.
Yeah I didn't do it for any major reason. I was just making sure I had it down right.
Gotcha. It's good to know you can do that anyway. Also, see edit I made on the previous post. Another no biggie.
Sweet. What does end1; do? I've seen it in a lot of programs but I can't tell the difference between having it and not.
 

burningdragoon

Warrior without Weapons
Jul 27, 2009
1,935
0
0
ethaninja said:
burningdragoon said:
ethaninja said:
burningdragoon said:
Oh I can hel.... oh beaten to it of course. But yeah just add a call to age on the next line in your main method.

also why is
cout<<"Please insert two " <<"numbers you wish to be multiplied\\n";
separated into sections? Unnecessary as far as I can tell.
Yeah I didn't do it for any major reason. I was just making sure I had it down right.
Gotcha. It's good to know you can do that anyway. Also, see edit I made on the previous post. Another no biggie.
Sweet. What does end1; do? I've seen it in a lot of programs but I can't tell the difference between having it and not.
endl (and that's an L btw) basically automatically adds a \n to the end and does some other clean up stuff. I'm a little shaky on the details myself and for most purposes you'll see for a while it won't matter, but I like to use endl for my own visual benefit. Kind of like "OK so this is the end of this part of the output".
 

RathWolf

New member
Apr 14, 2009
326
0
0
ethaninja said:
burningdragoon said:
ethaninja said:
burningdragoon said:
Oh I can hel.... oh beaten to it of course. But yeah just add a call to age on the next line in your main method.

also why is
cout<<"Please insert two " <<"numbers you wish to be multiplied\\n";
separated into sections? Unnecessary as far as I can tell.
Yeah I didn't do it for any major reason. I was just making sure I had it down right.
Gotcha. It's good to know you can do that anyway. Also, see edit I made on the previous post. Another no biggie.
Sweet. What does end1; do? I've seen it in a lot of programs but I can't tell the difference between having it and not.
It's endl, as in lowercase 'L', and it inserts an newline character, and generally looks neater.

Ex.
cout<<"Hello";
cout<<" World";

Would give the output:
Hello World

Whereas
cout<<"Hello"<<endl;
cout<<" World";

Would give the output
Hello
World
 

Necator15

New member
Jan 1, 2010
511
0
0
Well, you're age function is never called (Which has been pointed out), also you don't need a parameter for your age function. It's gotten in the program. You could also use type void for it as it doesn't return a value.

Like this:
void age()
{
//Yaddayaddayadda
}
 

Heart of Darkness

The final days of His Trolliness
Jul 1, 2009
9,745
0
0
ethaninja said:
//snip
Sweet. What does end1; do? I've seen it in a lot of programs but I can't tell the difference between having it and not.
Code:
endl
is basically the same thing as
Code:
\n
--all it does is tell the application to write text to a new line. I could be mistaken, but I don't think there's any real reason to use one over the other, except for ease of access depending on your code.

And it's a lowercase ENDL, not END1. Just making sure that that's clear.

EDIT: Dammit, ninja'd on both counts.

Also, shameless plug. We have a group here on the Escapist for programmers [http://www.escapistmagazine.com/groups/view/The-Escapist-Programmers-Help-Group]. It's a little underpopulated at the moment, but new members are always welcome!

EDIT2: Also, I'm going to post some tips for making your code a little more readable. I'm also learning C++, but most of this info comes from formatting other coding languages. I'm assuming you know about indentation, since the Escapist doesn't readily allow that. But you don't need brackets around the statements controlled by your
Code:
if
structures if you only have one statement. That is,

if (age < 50)
statement1;

else if (age > 50)
statement2;

else
statement3;

...is a perfectly valid syntax (you also don't need the (age == 50) line, since that's the only thing covered by the final else). In addition, I think you might need to prototype your functions at the beginning of your program, before you call the main() function (I think. I'm still new to C++, myself, so I don't know how valid that is). So the top of your program code would look something like this:

#include
#include
using namespace std;

int calc(int, int); //function prototype for the calc() function
void age(); //function protype for the age function

int main()
{
...
return 0; //Again, as others have said, it's a good idea to end your main() function with a return 0; command. It'll help others know where your //main loop ends.
}

int calc(num1, num2)
{...}

void age()
{...}

Again, I don't know how valid function prototyping is with modern C++ programs, but it can't hurt to get into the habit. Also, as others have said, the age() function should be a type void, since it doesn't really need a parameter. Just declare an int in the age() function and work from there. [void age() { int Age;...}]
 

burningdragoon

Warrior without Weapons
Jul 27, 2009
1,935
0
0
Heart of Darkness said:
Also, shameless plug. We have a group here on the Escapist for programmers [http://www.escapistmagazine.com/groups/view/The-Escapist-Programmers-Help-Group]. It's a little underpopulated at the moment, but new members are always welcome!
Hmmm... good to know. Good to know indeed.
 

Heart of Darkness

The final days of His Trolliness
Jul 1, 2009
9,745
0
0
burningdragoon said:
Heart of Darkness said:
Also, shameless plug. We have a group here on the Escapist for programmers [http://www.escapistmagazine.com/groups/view/The-Escapist-Programmers-Help-Group]. It's a little underpopulated at the moment, but new members are always welcome!
Hmmm... good to know. Good to know indeed.
As I said, kinda dead. But more people should mean more life! Woo!

...hopefully.