Hey guys i have this program that i need to write for class. here are the instructions:
Write a function called foo that asks the user for their age. Pass the age value to a function called showAge that uses a select case structure to print out the following: If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager. If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature
This is what i have, its works but i dont see why my teach wont let me use an if else..... please help me. Is this the right solution? 24 3085 sicarie 4,677
Recognized Expert Moderator Specialist
Wow, that is one hell of a case statement. I would recommend the ternary operator, though. It's a nifty workaround to if-elses.
::Edit:: You could also use an if statement to call a function. If you if statements were set up properly, one would not overlap with the other, and you could leave out the else statement. Then you're not using if-elses...
I GUESS I WILL DO THIS: -
#include <iostream>
-
using namespace std;
-
int foo ();
-
-
int x;
-
int showAge ();
-
int main()
-
{
-
-
foo();
-
-
-
}
-
int foo()
-
{
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge();
-
return x;
-
}
-
int showAge ()
-
{
-
switch ( x )
-
{
-
case 1:
-
case 2:
-
case 3:
-
case 4:
-
case 5:
-
case 6:
-
case 7:
-
case 8:
-
case 9:
-
case 10:
-
case 11:
-
case 12:
-
cout << "You are still a child" << endl;
-
break;
-
case 13:
-
case 14:
-
case 15:
-
case 16:
-
case 17:
-
case 18:
-
case 19:
-
cout << "You are a teenager." << endl;
-
break;
-
case 20:
-
case 21:
-
case 22:
-
case 23:
-
case 24:
-
case 25:
-
case 26:
-
case 27:
-
case 28:
-
case 29:
-
case 30:
-
case 31:
-
case 32:
-
case 33:
-
case 34:
-
case 35:
-
case 36:
-
case 37:
-
case 38:
-
case 39:
-
case 40:
-
cout << "You are an adult" << endl;
-
break;
-
-
default:
-
cout << "ou are starting to become mature or too old" << endl;
-
}
-
-
-
-
return x;
-
}
-
sicarie 4,677
Recognized Expert Moderator Specialist
I GUESS I WILL DO THIS:
That works too. Did you look at the ternary operator?
JosAH 11,448
Recognized Expert MVP
I GUESS I WILL DO THIS:
Much better and tell your teacher that he sucks big times.
kind regards,
Jos ( <--- 50 years old ;-)
ok i guess im not aloud to do int x; in global, how would i pass X throughout?
This is what i have, its works but i dont see why my teach wont let me use an if else..... please help me. Is this the right solution?
These are just guesses:
1.) From what I heard, switch statements run faster than if else statement (just barely).
2.) To force you to try something new for a change.
3.) If she told you to use a switch statement, then she's either silly, stupid, or enjoys seeing her students suffer!
sicarie 4,677
Recognized Expert Moderator Specialist JosAH 11,448
Recognized Expert MVP
These are just guesses:
1.) From what I heard, switch statements run faster than if else statement (just barely).
That is not true; for tests against an integer, switches are quite a bit faster than
a cascade of if-statements. The latter always run in O(n) while switch statemtents
can run in O(1) for 'dense' switches and O(log(n)) for sparse switches. Both of
which are way better than O(n).
kind regards,
Jos
i read that tutorial, i still dont understand.
sicarie 4,677
Recognized Expert Moderator Specialist
i read that tutorial, i still dont understand.
Wow, that was really fast for you to read it and understand it. Did you run the example? Did you change the values when you ran the example? Did you change the datatypes? What else did you try?
i read that tutorial earlier today, i just get this one error that says:
"error C2660: 'showAge' : function does not take 0 arguments"
This is my code now: -
-
/*
-
Write a function called foo that asks the user for their age.
-
Pass the age value to a function called showAge that uses a select case structure to print out the following:
-
If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
-
If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
-
*/
-
-
#include <iostream>
-
using namespace std;
-
int foo ();
-
int showAge (int &x);
-
int main()
-
{
-
-
foo();
-
-
return 0;
-
}
-
-
int foo()
-
{
-
int x;
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge();
-
-
}
-
-
int showAge (int x)
-
{
-
switch ( x )
-
{
-
-
case 1:
-
case 2:
-
case 3:
-
case 4:
-
case 5:
-
case 6:
-
case 7:
-
case 8:
-
case 9:
-
case 10:
-
case 11:
-
case 12:
-
cout << "You are still a child" << endl;
-
break;
-
case 13:
-
case 14:
-
case 15:
-
case 16:
-
case 17:
-
case 18:
-
case 19:
-
cout << "You are a teenager." << endl;
-
break;
-
case 20:
-
case 21:
-
case 22:
-
case 23:
-
case 24:
-
case 25:
-
case 26:
-
case 27:
-
case 28:
-
case 29:
-
case 30:
-
case 31:
-
case 32:
-
case 33:
-
case 34:
-
case 35:
-
case 36:
-
case 37:
-
case 38:
-
case 39:
-
case 40:
-
cout << "You are an adult" << endl;
-
break;
-
-
default:
-
cout << "ou are starting to become mature or too old" << endl;
-
}
-
-
}
its inconsistant? or im not passing is correctly?
sicarie 4,677
Recognized Expert Moderator Specialist
i read that tutorial earlier today, i just get this one error that says:
"error C2660: 'showAge' : function does not take 0 arguments"
Okay, let's take a look at all the places you use showAge() -
int showAge (int &x);
-
int main()
-
{
-
//...
-
}
-
-
int foo()
-
{
-
//...
-
cin >> x;
-
showAge();
-
}
-
-
int showAge (int x)
-
{
-
//...
-
}
-
Do you see anything odd there?
Savage 1,764
Recognized Expert Top Contributor
He can use switch statements,but after: - int k;
-
if(age>0&&age<=12) k=1;
-
if(age>=13&&age<=19) k=2;
-
.
-
.
-
.
-
-
switch(k)
-
{
-
-
//blah,blah,blah..
-
-
.
-
.
-
.
-
Savage
ok here is my code now: Same error as above: -
/*
-
Write a function called foo that asks the user for their age.
-
Pass the age value to a function called showAge that uses a select case structure to print out the following:
-
If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
-
If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
-
*/
-
-
#include <iostream>
-
using namespace std;
-
int foo (int x);
-
-
-
int showAge (int &x);
-
int main()
-
{
-
-
foo();
-
-
return 0;
-
}
-
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge();
-
return x;
-
}
-
int showAge (int &x)
-
{
-
if(x>0&&x<12) x=1;
-
if(x>=13&&x<19) x=2;
-
if(x>20) x=3;
-
switch ( x )
-
{
-
case 1:
-
cout << "You are still a child" << endl;
-
break;
-
case 2:
-
cout << "You are a teenager." << endl;
-
break;
-
case 3:
-
cout << "You are an adult" << endl;
-
break;
-
-
-
default:
-
cout << "ou are starting to become mature or too old" << endl;
-
-
return x;
-
}
-
JosAH 11,448
Recognized Expert MVP
He can use switch statements,but after: - int k;
-
if(age>0&&age<=12) k=1;
-
if(age>=13&&age<=19) k=2;
-
.
-
.
-
.
-
-
switch(k)
-
{
-
-
//blah,blah,blah..
-
-
.
-
.
-
.
-
Savage
That'd be silly because why postpone what you can deal with right after the
if-clause itself?
kind regards,
Jos
Savage 1,764
Recognized Expert Top Contributor
ok here is my code now: Same error as above: -
/*
-
Write a function called foo that asks the user for their age.
-
Pass the age value to a function called showAge that uses a select case structure to print out the following:
-
If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
-
If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
-
*/
-
-
#include <iostream>
-
using namespace std;
-
int foo (int x);
-
-
-
int showAge (int &x);
-
int main()
-
{
-
-
foo();
-
-
return 0;
-
}
-
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge();
-
return x;
-
}
-
int showAge (int &x)
-
{
-
if(x>0&&x<12) x=1;
-
if(x>=13&&x<19) x=2;
-
if(x>20) x=3;
-
switch ( x )
-
{
-
case 1:
-
cout << "You are still a child" << endl;
-
break;
-
case 2:
-
cout << "You are a teenager." << endl;
-
break;
-
case 3:
-
cout << "You are an adult" << endl;
-
break;
-
-
-
default:
-
cout << "ou are starting to become mature or too old" << endl;
-
-
return x;
-
}
-
Anything odd here: -
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
vvvvvvvvvvvvvvv
-
--------------->\\showAge();//<-------------------
-
^^^^^^^^^^^^^^^
-
return x;
-
}
???
Savage
Savage 1,764
Recognized Expert Top Contributor
That'd be silly because why postpone what you can deal with right after the
if-clause itself?
kind regards,
Jos
Writing around 40 case statements is boring,silly and everything bad you can think of.
Savage
now when i do that to foo it says:
variable "x" is being used without defined:
/*
Write a function called foo that asks the user for their age.
Pass the age value to a function called showAge that uses a select case structure to print out the following:
If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
*/
#include <iostream>
using namespace std;
int foo (int x);
int showAge (int &x);
int main()
{
int x;
foo(x);
return 0;
}
int foo(int x)
{
cout << "Please Enter Your Age:" << endl;
cin >> x;
showAge(x);
return x;
}
int showAge (int &x)
{
if(x>0&&x<12) x=1;
if(x>=13&&x<19) x=2;
if(x>20) x=3;
switch ( x )
{
case 1:
cout << "You are still a child" << endl;
break;
case 2:
cout << "You are a teenager." << endl;
break;
case 3:
cout << "You are an adult" << endl;
break;
default:
cout << "ou are starting to become mature or too old" << endl;
return x;
}
}
the error is:
uninitialized local variable 'x' used
sicarie 4,677
Recognized Expert Moderator Specialist
the error is:
uninitialized local variable 'x' used
Move your cin to main.
Ok i fixed it :)
Thanks everyone for the help; -
/*
-
Write a function called foo that asks the user for their age.
-
Pass the age value to a function called showAge that uses a select case structure to print out the following:
-
If the age is 0-12 print out "You are still a child. If the age is 13 - 19 print out "You are a teenager.
-
If the age is 20 - 40 print "You are an adult" anything else print "You are starting to become mature"
-
*/
-
-
#include <iostream>
-
using namespace std;
-
int foo (int x);
-
int showAge (int &x);
-
int main()
-
{
-
int x = 0;
-
foo(x);
-
-
return 0;
-
}
-
int foo(int x)
-
{
-
-
cout << "Please Enter Your Age:" << endl;
-
cin >> x;
-
showAge(x);
-
return x;
-
}
-
int showAge (int &x)
-
{
-
if(x>0&&x<12) x=1;
-
if(x>=13&&x<19) x=2;
-
if(x>20&&x<40) x=3;
-
switch ( x )
-
{
-
case 1:
-
cout << "You are still a child" << endl;
-
break;
-
case 2:
-
cout << "You are a teenager." << endl;
-
break;
-
case 3:
-
cout << "You are an adult" << endl;
-
break;
-
-
-
default:
-
cout << "You are starting to become mature or too old" << endl;
-
-
}
-
-
-
return x;
-
}
-
JosAH 11,448
Recognized Expert MVP
Writing around 40 case statements is boring,silly and everything bad you can think of.
Savage
You didn't read my reply (in the context of yours) carefully enough; btw, the
amount of typing is nowhere proportional to the (in)efficiency of an algorithm
big-Oh wise speaking.
kind regards,
Jos
Savage 1,764
Recognized Expert Top Contributor
You didn't read my reply (in the context of yours) carefully enough; btw, the
amount of typing is nowhere proportional to the (in)efficiency of an algorithm
big-Oh wise speaking.
kind regards,
Jos
That's just what they want us to believe. :P
Just for fun using high accuracy counter(QueryPerformanceFreuqency and QueryPerformanceCounter),I discovered that my solution is faster for 142 ticks.(Avg for my sol:2483,Avg. for pure case sol.:2625 dif=-142 ticks)
:P
regards,
Savage
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Thomas Matthews |
last post by:
Hi,
My son is writing a program to move a character. He is
using the numbers on the keypad to indicate the direction
of movement:
7 8 9
4 5 6
1 2 3
Each number has a direction except...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |