Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialshane smith
5,247 Pointsage test c++
i am tying to make a simple age test programme to show whether you are adult, teen or child. but can't seem to get it any suggestions?
my code using code:: blocks :
#include <iostream>
using namespace std;
int main()
{
int age = 18;
cout << "please enter age\n";
cin >> age;
if(age >= 18){
cout << "Adult";
}
else {
cout << "teenager";
}
if(age < 13){
cout << "child";
}
else {
cout << "something's wrong";
}
return 0;
}
3 Answers
Jennifer Nordell
Treehouse TeacherHi there! First of all, I'd like to say that it's been years since I've done any C++. But, as Treehouse doesn't offer any courses on C++, I'm not sure how many people are going to be willing to answer this question.
Long story short: the logic in your conditionals is incorrect. These should be set up as a series of if/else statements. Let me show you how I did it:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int age = 18;
int age2;
cout << "please enter age\n";
cin >> age;
if (age >= 18) {
cout << "Adult";
}
else if(age >= 13) {
cout << "teenager";
}
else if(age > 0)
{
cout << "child";
}
else {
cout << "something's wrong";
}
cin >> age2;
return 0;
}
Here we start with your initial age variable. I left it as is. Then I set up a new age. This is not actually necessary other than I want the console app to pause after it prints out. To do this we need a new cin
to keep the app from immediately closing.
Now, we check if the age is over 18. Then we check to see if it's 13 or more. Then we check to make sure the age is over 0. And if none of those work, then we print out something is wrong. This is where I implemented the new cin
line to pause the console program.
Note: Visual studio insisted that I have that first #include
statement to build the program.
Hope this helps!
shane smith
5,247 PointsJennifer Nordell do you think i am wasting time learning c++ and move my mined to c# as have read up that c++ is not widely used as much in the industry. ty.
Jennifer Nordell
Treehouse TeacherI don't know that I'd call it a waste necessarily. Learning any programming language will expose you to ideas and concepts that are reusable in others. A loop, a conditional statement etc, while written differently in other languages, are essentially the same idea. However, I'm given to understand that development is going in the direction of C# as opposed to C++. Keep in mind that C# is Microsoft's native programming language these days.
shane smith
5,247 Pointsgreat help and advise thank you very much for you time answering my questions. :)
shane smith
5,247 Pointsshane smith
5,247 Pointsthanks this woks for what what i wanted didn't need extra #include statement or cin function to run programme in code blocks also use visual studio so will bare them in mind if i cross over into msvs and also thanks for answering as like you said didn't think anyone would works fine thanks. ;)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse Teachershane smith You're quite welcome! Glad I could help