Find the answer to your Linux question:
Results 1 to 7 of 7
So I recently decided to start learning C++ again, and when I did all the basic knowledge came rushing back. So with a few syntax errors here and there I ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9

    Suggestions/improvements for basic C++

    So I recently decided to start learning C++ again, and when I did all the basic knowledge came rushing back. So with a few syntax errors here and there I managed to make a program COMPLETE with predefined function:

    Code:
    //What is your age, goofy program
    //because I am a tool
    #include <iostream.h>
    
    void funAge();
    
    int Age;
    
    int main ()
    {
    	cout << "Want to find out how much of a tool you are? \n Just input your age!";
    	cin >> Age;
    	while(Age<=0 or Age>100) {
    		cout <<"Error, please enter a valid number or stop lying";
    		cin >> Age;
    	}
    	funAge();
    	return 0;
    }
    
    void funAge()
    {
    	if(Age<12)
    		cout <<"You are such a huge tool a toolbox has nothing on you";
    	else if(Age=13)
    		cout <<"Good job, you hit puberty, learn what to do with that tool";
    	else if(Age=14)
    		cout <<"Great, you're in high school, worry about getting laid with\n That flathead screwdriver.";
    	else if(Age=15)
    		cout <<"Your tool is still underserved, maybe one of these days your\n voice will stop cracking long enough to talk to a girl.";
    	else if(Age=16)
    		cout <<"Hey now you have a car to not drive girls in, tool";
    	else if(Age=17)
    		cout <<"Haha, still one more year to freedom you friggin tool";
    	else if(Age=18)
    		cout <<"You're 18, but still living with your parents, sucks, tool";
    	else if(Age=19)
    		cout <<"So you've been drinking for a year without parent permission\n good job maybe one of these days you'll get laid";
    	else if(Age=20)
    		cout <<"Haha, still cant drink, loser";
    	else if (Age>=21)
    		cout <<"Yay, you're not a tool";
    }
    Is there anything I could have done differently? I'm mostly looking at the funAge(); function to see if it could have been done more efficiently.

  2. #2
    Linux Engineer Zelmo's Avatar
    Join Date
    Jan 2006
    Location
    Riverton, UT, USA
    Posts
    1,001
    Two things come to mind immediately:
    1. Rather than have Age as a global variable, declare it within main() and pass it to your function.
    2. Inside the function, a switch construct is most appropriate for selecting from multiple possible cases.

    A couple of things not related to the function are also worth noting:
    1. The ".h" extension is deprecated, and g++ will tell you so when you compile (though only as a warning). ANSI prefers that you now include <iostream>.
    2. There is no "using" directive, and the cout and cin statements are not fully qualified. That will give compile errors. You have three options that I know of: a "using" directive for the entire std namespace, a "using" directive for std::cin and std::cout, or fully qualifying std::cin and std::cout each time they're used.
    Stand up and be counted as a Linux user!

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9
    Well after dropping the .h extention and passing the integer to the function instead of defining it globally, I've found that now I'm getting an error with the cin's and cout's not being defined in "this" scope.

    For the part on "using" directives, my geekhood in c++ is not quite up to comprehending what you said, hopefully there's a way you can dumb it down for me >.< I'm not used to not using cin and cout in linux, which I think is the problem.

  4. #4
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9
    Ok, so i've discovered to use the printf command instead, this leaves me with a blank for what to use for cin, I'll try google, hopefully I'll figure something out.

  5. #5
    Linux Engineer Zelmo's Avatar
    Join Date
    Jan 2006
    Location
    Riverton, UT, USA
    Posts
    1,001
    Oh, there's no need to use printf. What I meant by the "using" directive is that people will often have a line right after your include statements that looks like this:
    Code:
    using namespace std;
    It's normally not recommended to specify an entire namespace, but for std, it's kind of the norm. You can also leave that line out and fully qualify each instance of cin and cout by replacing them with std::cin and std::cout.

    What I had in mind was something like this:
    Code:
    //What is your age, goofy program
    //because I am a tool
    #include <iostream>
    using namespace std;
    
    void funAge(int);	// Changed the prototype to accept an integer
    
    int main()
    {
    	int Age;		// Declared Age locally to keep it from being too exposed
    	cout << "Want to find out how much of a tool you are?\n"
    		<< "Just input your age! ";
    	cin >> Age;
    	while(Age <= 0 or Age > 100)
    	{
    		cout <<"Error, please enter a valid number or stop lying: ";
    		cin >> Age;
    	}
    	funAge(Age);	// Passed Age to the function
    	return 0;
    }
    
    void funAge(int Age)	// Added a parameter to the function
    {
    	// Switch can't handle ranges, so take care of them separately:
    	if(Age<12)
    		cout <<"You are such a huge tool a toolbox has nothing on you";
    	else if (Age>=21)
    		cout <<"Yay, you're not a tool";
    	// Now use switch for more straightforward handling of specific cases:
    	else switch(Age)
    	{
    		/*
    			Some of the longer lines have been split. Ideally, keep your lines
    			less than 80 characters long to make them fit better in console-
    			based editors. You never know when that will come in handy.
    		*/
    		case 13: cout <<"Good job, you hit puberty,"
    			<< " learn what to do with that tool";
    			break;
    		case 14: cout <<"Great, you're in high school, worry about getting laid\n"
    			<< "with that flathead screwdriver.";
    			break;
    		case 15: cout <<"Your tool is still underserved, maybe one of these days\n"
    			<< "your voice will stop cracking long enough to talk to a girl.";
    			break;
    		case 16: cout <<"Hey now you have a car to not drive girls in, tool";
    			break;
    		case 17: cout <<"Haha, still one more year to freedom you friggin tool";
    			break;
    		case 18: cout <<"You're 18, but still living with your parents, sucks, tool";
    			break;
    		case 19: cout <<"So you've been drinking for a year without parent permission.\n"
    			<< "Good job, maybe one of these days you'll get laid";
    			break;
    		case 20: cout <<"Haha, still cant drink, loser";
    			break;
    	}
    }
    Stand up and be counted as a Linux user!

  6. #6
    Just Joined!
    Join Date
    Jun 2007
    Posts
    9
    Much appreciated for that post. Totally forgot about switches, and the using name space std; is completely new to me. Thanks again

  7. #7
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Quote Originally Posted by Duros View Post
    Much appreciated for that post. Totally forgot about switches, and the using name space std; is completely new to me. Thanks again
    I could be wrong, but I think the "using namespace std" requirement has been an ANSI standard for a while, however it only started being enforced as of version 3.0 of the GNU C Compiler. It caught me by surprise too the first time I ran a "Hello, World" and got "cout undefined".

    In fact, I ranted about it when I noticed it in Redhat 8. It wasn't exactly something that was broadcasted.
    Registered Linux user #270181
    TechieMoe's Tech Rants

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...