-
Need help learning C++.
Hi, I'm Sarah.
I need help learning C++. I have been trying to read tutorials and such but I am the type of person that has trouble just reading something and getting it, if it is only put one way. I have been on many forums before, and I am sure this is going to get flamed to hell, but if anyone would be interested in helping me to understand more, I would really appreciate it.
Thanks for your time.
-
> I am the type of person that has trouble just reading something and getting it
So read it, then try it for yourself.
It doesn't matter how many tutorials you read, or how many tutors you can talk to, it all boils down to actually getting down and actually writing some stuff for yourself.
Then when you get stuck, post your code and questions to the forum, and someone will likely give you an answer.
-
Code:
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<"Please input your age: ";
cin>> age;
cin.ignore();
if ( age < 47 ) {
cout<<"You are pretty young!\n";
}
else if ( age == 47 ) {
cout<<"I love you mom!!!!!\n";
}
else if (age > 47 ) {
cout<<"You are old!!!!\n";
}
cin.get();
}
Ok, so what I am trying to do here.. is alter the thing in the tutorial to show my mom.
I managed to get it this far, but what I want it do is make it so that 47-100 is old, and 100+ is very old.
I tryed typing it a few different ways, but I can't seem to get it to work right.
I know this is very newb stuff, but I just started today.
-
So you need a condition like
if ( age > 47 && age < 100 )
-
how would you say "less than or equal too"
example.... greater than 5 but less than or equal to 25
-
Hi Sarah,
Don't worry we won't flame you... ;)
less than or equal to would be.... <=
more than or equal to would be..... >=
-
Heh, I knew that, I just didnt know if I had to use && and such. I know how to do this all mathmatically, just not in C++ form yet.
( If that made any sense)
also what is wrong with this it keeps giving me an error
Code:
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<"Please input your age: ";
cin>> age;
cin.ignore();
if ( age < 5 || age == 5 ) {
cout<<"Are you potty trained yet?!\n";
}
else if ( age > 5 && age < 12 || age == 12 ) {
cout<<"Woo! You're maturing.\n";
}
else if ( age > 12 && age < 20 ) {
cout<<"You're a teenager, welcome to hell.\n";
}
else if ( age > 21 && < 30 ) {
cout<<"Party it up, you can drink, your prolly in college, or just out of it. Have fun.\n";
}
else if ( age == 20 ) {
cout<<" You are the ........tiest age ever. You are in college, alot of work.. and you can't drink yet. Hold int here one more year.\n";
}
else if ( age > 30 && age < 40 || age == 30 || age == 40) {
cout<<"You are getting towards middle aged, start to settle down.\n";
}
else if ( age > 40 && age < 47 ) {
cout<<"You're now middle aged. Get used to it.n\";
}
else if ( age == 47 ) {
cout<<"I love you mom!!!!!\n";
}
else if ( age > 100 || age == 100 ) {
cout<<"Plan your funeral, make sure your will is correct, etc.\n";
}
else if ( age > 47 && age < 60 ) {
cout<<"You're getting up there in years!!!\n";
}
else if ( age > 60 || age == 60 && age < 100 ) }
cout<<"You're old.\n";
}
cin.get();
}
I followed the format of the tutorials one, I just wanted to see if I could elaborate on it.
-
Hi,
Something's wrong here
Code:
else if ( age > 21 &&???< 30 )
And here
Code:
else if ( age > 40 && age < 47 ) {
cout<<"You're now middle aged. Get used to it.n\";
And here
Code:
else if ( age > 60 || age == 60 && age < 100 ) }
cout<<"You're old.\n";
;)
-
last thing I swear ( i fixed those, and it runs well now thank you.)
Is there a way to make it go again? like after it tells you your answer, it asks again, and you can enter a new age. Without having to x out of it and run it again every time
( Sorry about my punctuation I havn't slept. xD)
-
No probs,
Use a while loop, do you want me to show you or do you want to read the tutorials and learn for yourself? There's a good tutorial section in the home page.
:)
-
Sure, lookup do...while loops. They look like this:
Code:
do{
//code here - this will always be executed at least once, since we havn't reached a test condition yet
} while( condition );
In your case, the condition will need to test whether the user wants to start again or not.
If you get stuck, post back here! Good luck...
-
I would actually appreciate the help. The only thing I understand on the tutorial is the examples, and that's after I do it over and over and over wrong. I want to show my mom this program soon, so I am hoping to get it done. I have been doing this all night as sad as that is.
I dont understand what you just told me, I was typing this while you posted that hence me editing this.
-
Possibly this...
Code:
#include <iostream>
using namespace std;
int main()
{
int age;
do
{
cout<<"Please input your age (0 to quit): ";
cin>> age;
cin.ignore();
if ( age < 5 || age == 5 ) {
cout<<"Are you potty trained yet?!\n";
}
else if ( age > 5 && age < 12 || age == 12 ) {
cout<<"Woo! You're maturing.\n";
}
else if ( age > 12 && age < 20 ) {
cout<<"You're a teenager, welcome to hell.\n";
}
else if ( age > 21 && age< 30 ) {
cout<<"Party it up, you can drink, your prolly in college, or just out of it. Have fun.\n";
}
else if ( age == 20 ) {
cout<<" You are the ........tiest age ever. You are in college, alot of work.. and you can't drink yet. Hold int here one more year.\n";
}
else if ( age > 30 && age < 40 || age == 30 || age == 40) {
cout<<"You are getting towards middle aged, start to settle down.\n";
}
else if ( age > 40 && age < 47 ) {
cout<<"You're now middle aged. Get used to it.\n";
}
else if ( age == 47 ) {
cout<<"I love you mom!!!!!\n";
}
else if ( age > 100 || age == 100 ) {
cout<<"Plan your funeral, make sure your will is correct, etc.\n";
}
else if ( age > 47 && age < 60 ) {
cout<<"You're getting up there in years!!!\n";
}
else if ( age > 60 || age == 60 && age < 100 ) {
cout<<"You're old.\n";
}
}while(age!=0);
cin.get();
}
Although it has a flaw... Gotta go now good luck.
-
I see the flaw... if you enter 0 as your age it tells you then quits.
-
More importantly, can you see a way around the flaw? Hint: Wouldn't it be a better idea to actually ask the user if he/she would like another go?