We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,278 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
Page 2 of Article: Age Calculator
**Hello everybody!** I made a program which is able to calculate your age. You can find the source code below this text. There is already posted an article like this on http://www.daniweb.com/software-development/cpp/code/331349/c-code-for-simple-age-calculator But, my program is: **- Without mistakes**, it doesn't give your computer wrong instructions. **- More accurate**, because…

Give me a clear explanation.

I fail to see what's unclear about the hypocrisy here. You claim to be a beginner, someone who by definition isn't perfect and will make a greater than average number of mistakes that go unnoticed. You also claim that your program has no problems and cannot be improved despite being shown otherwise.

This alone doesn't make you a troll, but refusing to listen to anyone who points out flaws raises red flags.

As I said; I am a beginner who wants to learn C++ better.

Bullshit. You've rejected any attempts to teach you and stubbornly insist that your code is without flaws. With that attitude, the clueful people won't want to help you learn C++ better. I certainly have no incentive to review your code for you because I'm reasonably sure it would accomplish nothing and be a huge waste of my time.

deceptikon
Challenge Accepted
Administrator
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

Lucaci Andrew

All of your souce code is in one function, and that function is the specific starting function of your program - what is wrong with you? Haven't you heard about other functions, classes maybe, structures? This is not Python to just use any function as the starting point of your program, then to recursivelly call it to "restart your program".

In C++, it isn't necessary to use different functions in this situation. It is a possibility, but the compiler will still be able to compile the code fine, even if it has only one function. The program, with only one long function, will also run without any problems.

Just like you can use "\n" to go to the next line, or "endl". It's exactly the same.

Note that there are specific items from C++ that work only on some OS. Linux is different from Windows, and if you implement some specific items from Windows, don't expect to work on Linux.

Of course there are different operating systems. Linux is different than Windows, but the program language, C++, should be the same.

Now, I want to fix the problem that if I enter a character instead of a value, the program will crash. WaltP and deceptikon are refusing to help me. They just say that my program is bad and that I am trolling.

markwiering
Light Poster
37 posts since Jul 2012
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

In programing there are a lot of things that aren’t necessary for you to do. There are also a lot of things that you can do but shouldn’t do. The fact that the language or the compiler you are using allows you to do it doesn’t mean you should. The best thing to do is to get into the habit of programing in the standard manner. This includes putting code into functions that is going to be reused like your calculation to determine if it is a leap year or not. Your main function should mainly be an entry point for your program, you definitely shouldn’t be calling it from anywhere in your program. I understand that for small projects this might seem annoying but it gets you into the habit of doing it.

If I was you I would listen to what the people here are telling you instead of arguing saying that since it works it is okay. WaltP and deceptikon have some invaluable advice to give and when they give advice I don’t blindly ignore it. That’s not to say you shouldn’t argue a point if you don’t agree but if you are going to, make sure you have some clear examples and/or references to back you up. Learning to code never stops and it takes time to just master the basics. Well there is my two cents.

NathanOliver
Posting Virtuoso
1,516 posts since Apr 2009
Reputation Points: 281
Solved Threads: 278
Skill Endorsements: 3

WaltP and deceptikon are refusing to help me.

If you actually accepted help instead of rejected every suggestion, we'd be more inclined to make a serious attempt at helping you. But since you stubbornly refuse to listen to any advice and act like a know-it-all, we aren't going to bother. Well, at least, I won't. I can't really speak for Walt, but I suspect he feels the same way.

It's a shame too, I could help you improve this program and your C++ abilities drastically. But I know a lost cause when I see one.

deceptikon
Challenge Accepted
Administrator
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

NathanOliver

  1. I am using main(); to restart the program. If you know a better code to do that, give me the code.
  2. My program will crash if you enter a character instead of a value. Do you know with which code I can solve that problem?
  3. WaltP and Suzie999 aren't helping or giving advises. They are just bashing my program, without telling me which code I have to use to solve the problems I have made.
markwiering
Light Poster
37 posts since Jul 2012
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

Well to fix number 1 breaking your code into functions and using loops will eliminate the need to call main. To fix number 2 you should take your input in as a string and validate the data that way. You can take a look at this thread to get some ideas http://www.daniweb.com/software-development/cpp/threads/432195/bit-of-weirdness. As far as 3 goes they didn’t start off bashing your program. They gave you some constructive criticism of your code and totally disregarded it and decided to argue about the things they pointed out.

NathanOliver
Posting Virtuoso
1,516 posts since Apr 2009
Reputation Points: 281
Solved Threads: 278
Skill Endorsements: 3

Hi markwiering,

I hear where you're coming from. You're still a fairly raw beginner, so for you, having code that appears to work fine when the expected input is provided probably feels like a good result. And in one sense it is. But you can probably understand where the others are coming from, too. Your programme doesn't guard against unexpected input, and in a real-world programme that'd be a major issue.

If you don't understand how to guard against it, at least understand that at some point this is going to matter to you.

There are quite a few things that someone more experienced with C++ would do differently, which is why you're attracting the comments. If the comments are beyond your current understanding, maybe come back and take a look at this code in a couple of months time.

All the best with your coding.

Bob
using namespace Bob
Team Colleague
221 posts since Feb 2003
Reputation Points: 30
Solved Threads: 11
Skill Endorsements: 0

Just like you can use "\n" to go to the next line, or "endl". It's exactly the same.

In fact, they're not exactly the same. It's quite possible that the difference between the two is irrelevent for this programme, but they're not the same and at some point it may matter. Here's where you can begin helping yourself, by acquiring a good C++ reference and reading up a little -- if not now then later -- and avoiding making concrete statements unless you're absolutely positive about them. It takes time to become proficient in any language, and that's certainly true of C++.

Once again, good luck, enjoy the ride, and strive to be the best you can be.

Bob
using namespace Bob
Team Colleague
221 posts since Feb 2003
Reputation Points: 30
Solved Threads: 11
Skill Endorsements: 0

WaltP was negative about the fact that I have used main(); and about the fact that my program will crash if you enter a character instead of a value.

Not just me. Others pointed it out first and I agreed. And you argued that your program was perfect. When asked "what happens when you input a character?" you responded:

You: The program will crash. You have to fill in a number.
You: I have tested my program already. I solved every single mistake which I have made. The program also takes to account with leap years and with correctionyears.
You: It is a perfect program now. :-D
You: It is. The program will prevent you from entering invalid data.
You: When you enter characters instead of numbers, the program doesn't know what to do with it, so it crashes.

Crashing is an error, not a valid way to prevent invalid data

See the pattern?

You: In some if-statements I am using main(); to restart the program. That is not bad!

More arguing about a bad practice.

I asked WaltP which code I have to use to restart my program is main(); is not good enough, and which code I have to use so my program won't crash if you enter a character instead of a value.

And why should I wish to help after all the above attitude, arguing, telling us we are wrong? What makes you think we'd be interested in helping after all that?

Yes, I can help you with that. I've done it many times on these boards. But you need to lose the I know more than you attitude before that happens.

And you continued:

Lucaci Andrew: All of your souce code is in one function, and that function is the specific starting function of your program - what is wrong with you? Haven't you heard about other functions, classes maybe, structures? This is not Python to just use any function as the starting point of your program, then to recursivelly call it to "restart your program".

You: In C++, it isn't necessary to use different functions in this situation. It is a possibility, but the compiler will still be able to compile the code fine, even if it has only one function. The program, with only one long function, will also run without any problems.

More arguing.

You: Just like you can use "\n" to go to the next line, or "endl". It's exactly the same.

More false information claiming it's true.

You: Note that there are specific items from C++ that work only on some OS. Linux is different from Windows, and if you implement some specific items from Windows, don't expect to work on Linux.

Of course there are different operating systems. Linux is different than Windows, but the program language, C++, should be the same.

But refusing to understand that using something that is Windows-based in the code makes the code not compile on linux!! therefore it is not standard C++.

You: Now, I want to fix the problem that if I enter a character instead of a value, the program will crash. WaltP and deceptikon are refusing to help me. They just say that my program is bad and that I am trolling.

I never said you were trolling.
As I said, lose the attitude, the arrogance, and open your mind to learn the facts, and you'll get help from us. But so far, that hasn't happened.

NathanOliver: That’s not to say you shouldn’t argue a point if you don’t agree but if you are going to, make sure you have some clear examples and/or references to back you up.

Exactly. And I've freely admitted when I've been wrong and someone corrects me.

You: WaltP and Suzie999 aren't helping or giving advises. They are just bashing my program, without telling me which code I have to use to solve the problems I have made.

We offered, but you argued rather than listened.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

Against my better judgement I'm posting again after declaring myself out
I'm sorry for the backtrack to members here.

markwiering, help comes in many forms, but when you just say
"tell me the code" you are not even helping yourself, so how
can anyone else help you?

You need to help yourself first, learn how to search for a
question you need an answer to.

Type your question into a search engine and look through the
results.

I'm little more than a beginner myself, so bare that in mind
while reading what I'm advising.

Collect your input from cin() as a string/Char*
Loop through it, and perhaps check each element
with isdigit() function.

If they are all digits then convert the string/char*
to the data type you need to work with, if not then
continue the loop that this routine should be in.

While you are excersising your mind on how to put
that into code, you will learn rapidly that another
piece of advice offered in this thread was xaluable.

That is functions/methods/routines whatever you want to
call them.

Once again, best of luck and keep at it if you truly want
to learn.

Suzie999
Posting Whiz
319 posts since Jul 2010
Reputation Points: 49
Solved Threads: 15
Skill Endorsements: 0

Since I had a little time, and I never used isdigit myself
I made a very basic example of using it.

int main ()
{
  char CHAR[]="h"; // then change this to a digit

  if (isdigit(CHAR[0]))
  {
    cout << CHAR[0] << " is a digit" << endl;
    cin.get();
  }
  else
  {
    cout << CHAR[0] << " is not a digit" << endl;
    cin.get();
  }
  return 0;
}
Suzie999
Posting Whiz
319 posts since Jul 2010
Reputation Points: 49
Solved Threads: 15
Skill Endorsements: 0

WaltP

I would appreciate it if you would help me to fix the mistakes I have made. But in fact, you are not helping.

markwiering
Light Poster
37 posts since Jul 2012
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

I really don't know wheather I will reach on that extend of writing a program of 410 lines. Good work

littlephild
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I would appreciate it if you would help me to fix the mistakes I have made. But in fact, you are not helping.

Really? Than take the fricking advices!
Look at Suzie999 example and learn how to use the isdigit function, it would help you validate the input and see if it's formed only from digits, or if there are strings in it. Also, use this to find more about validating things.

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

Lucaci Andrew

You aren't helping me either. Only Suzie999 and NathanOliver have helped me.

To Suzie999 and NathanOliver: thanks for the help. :-D

markwiering
Light Poster
37 posts since Jul 2012
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

Hi markwiering,

Some hints to point you in the right direction: look up (here or wherever, e.g. Google or your C++ reference) cin.fail(), cin.clear() and cin.ignore().

Good luck with your coding.

Bob
using namespace Bob
Team Colleague
221 posts since Feb 2003
Reputation Points: 30
Solved Threads: 11
Skill Endorsements: 0

Suzie999

Thanks for the help, but char can only hold one character. In my program I have to enter more than one character. I tried your method, but when I enter 1997, the program simply will cut of the 199 and only sees the 7.

Which code do I have to use so the program is able to see at least 4 characters and won't crash if I enter a letter instead of a value?

markwiering
Light Poster
37 posts since Jul 2012
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

strlen() can be used to find the length of the entered string.
and a simple loop can check it.

Suzie999
Posting Whiz
319 posts since Jul 2010
Reputation Points: 49
Solved Threads: 15
Skill Endorsements: 0

Which code do I have to use so the program is able to see at least 4 characters and won't crash if I enter a letter instead of a value?

Your problem is that cin goes into a failed state when it gets the unexpected input (e.g. letters when it's expecting an integer value). The advice I posted earlier will enable you to detect when cin goes into a failed state (cin.fail), clear the fail state (cin.clear), and get rid of whatever characters are left in the stream (cin.ignore).

Technically you can, as an alternative, store and parse the input, and there are times when parsing input can be worthwhile, but you don't need to for simple input like this.

If you don't understand the explanation about cin going into a failed state, that's fine. Just look up cin.fail(), cin.clear(), and cin.ignore(). It'll take you 5 minutes and you will have your solution.

Bob
using namespace Bob
Team Colleague
221 posts since Feb 2003
Reputation Points: 30
Solved Threads: 11
Skill Endorsements: 0

Here is the updated version of my Age Calculator with a few improvements:

  • The program won't allow you to enter characters or symbols when values are required.
  • Two bugs have been fixed.
  1. The program asks you the wrong question when you enter an invalid current day. FIXED!
  2. In a few situations, the program shows you the wrong days. FIXED!

http://www.daniweb.com/software-development/cpp/threads/434926/updates-version-age-calculator#post1867334

markwiering
Light Poster
37 posts since Jul 2012
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1897 seconds using 2.96MB