|
-
Feb 6th, 2003, 05:44 AM
#1
Thread Starter
Hyperactive Member
Newb: How does this look
I just put this together for my C++ class. I've tested it thoroughly but I want to make sure I'm doing everything correctly. Tell me if you see anything that doesn't stick to standards or whatever. By the way, this is supposed to check the difference between the ages of two people. Thanks for your time.
PS - You'll probably notice some strange things about the code. For instance, I chose to put std:: in front of cout and cin. This was intentional (part of the requirements for this particular exercise).
Code:
#include <iostream>
#include <stdlib.h>
typedef unsigned short int USHORT;
int main()
{
char FirstPersonName[40];
char SecondPersonName[40];
USHORT FirstPersonAge;
USHORT SecondPersonAge;
USHORT AgeResult;
std::cout << "This program calculates the age difference between two people. \n\n";
std::cout << "What is the name of the first person? \n";
std::cin >> FirstPersonName;
std::cout << "How old is " << FirstPersonName << "? \n";
std::cin >> FirstPersonAge;
std::cout << "What is the name of the second person? \n";
std::cin >> SecondPersonName;
std::cout << "How old is " << SecondPersonName << "? \n";
std::cin >> SecondPersonAge;
if (FirstPersonAge == SecondPersonAge)
{
std::cout << "Result: " << FirstPersonName << " and " << SecondPersonName << " are the same age. \n";
system("pause");
exit(0);
}
if (FirstPersonAge > SecondPersonAge)
{
AgeResult = FirstPersonAge - SecondPersonAge;
std::cout << "Debug AgeResult: " << AgeResult << "\n";
if (AgeResult == 1)
{
std::cout << "Result: " << FirstPersonName << " is " << AgeResult << " year older than " << SecondPersonName << ". \n";
}
else
{
std::cout << "Result: " << FirstPersonName << " is " << AgeResult << " years older than " << SecondPersonName << ". \n";
}
}
else
{
AgeResult = SecondPersonAge - FirstPersonAge;
if (AgeResult == 1)
{
std::cout << "Result: " << SecondPersonName << " is " << AgeResult << " year older than " << FirstPersonName << ". \n";
}
else
{
std::cout << "Result: " << SecondPersonName << " is " << AgeResult << " years older than " << FirstPersonName << ". \n";
}
}
system("pause");
return 0;
}
Last edited by Comreak; Feb 6th, 2003 at 05:48 AM.
-
Feb 7th, 2003, 05:59 AM
#2
Fanatic Member
it looks ok :)
it looks ok, but don't count on what i say, im quite a n00b when it comes to c++, and i didn't read the whole thing throughly...
but anyway, good luck presenting it!
Best Regards,
seec77
If you helped me, cosinder yourself thanked.
Get each and every Garfield strip here!
Here you can get all Calvin & Hobes strips!
Damn UComics! It was probably unprofitable for them to allow us to just download Garfield and Calving & Hobes strips... so they made folder indexing unallowed on their server!!!
I am 33% addicted to Counterstrike. What about you?
I am 23% addicted to Star Wars. What about you?
I am 0% addicted to Tupac. What about you?
-
Feb 7th, 2003, 07:33 AM
#3
Hyperactive Member
Why don't you use namespace std?
Code:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
.....
}
You can do this this way,
typedef unsigned short USHORT;
-
Feb 7th, 2003, 08:23 PM
#4
Thread Starter
Hyperactive Member
Originally posted by transcendental
Why don't you use namespace std?
Code:
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
.....
}
You can do this this way,
typedef unsigned short USHORT;
Under normal circumstances, I would have used namespace std. Putting the "std::" in front of everything, however, was part of this particular assignment. In other words, my instructor wanted me to do it this way for this assignment.
I've a question for you transcendental. I noticed you used #include <cstdlib> when you quoted by include list. Is cstdlib equivalent to stdlib.h? What I mean is, should I use cstdlib instead of stdlib.h?
-
Feb 7th, 2003, 08:56 PM
#5
Hyperactive Member
Originally posted by Comreak
I've a question for you transcendental. I noticed you used #include <cstdlib> when you quoted by include list. Is cstdlib equivalent to stdlib.h? What I mean is, should I use cstdlib instead of stdlib.h?
cstdlib is the new stdlib.h, in std namespace; Meaning you have to put std::system("pause"); as well if you use cstdlib.
-
Feb 8th, 2003, 06:51 PM
#6
Thread Starter
Hyperactive Member
Originally posted by transcendental
cstdlib is the new stdlib.h, in std namespace; Meaning you have to put std::system("pause"); as well if you use cstdlib.
That's what I figured. Luckily, I don't have to add system("pause") to the code because we use MVC++ at school which adds that in automatically. I only added it here because I'm using DevCpp. Thanks for the info transcendental.
edit:// You know, I just remembered I'm using stdlib for the "exit()" function. Well, that's not too much to type.
-
Feb 8th, 2003, 07:16 PM
#7
You shouldn't use exit(), but rather return from the main function:
return 0;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 9th, 2003, 03:00 AM
#8
Thread Starter
Hyperactive Member
Originally posted by CornedBee
You shouldn't use exit(), but rather return from the main function:
return 0;
Correct me if I'm wrong. Return(0) can be used like it normally would to end a program if it is not used withen a function? If this is the case, how do I end the program withen a function? Sorry if these question seem simple. I'm still learning.
-
Feb 10th, 2003, 05:57 AM
#9
Very simple. Your program ends when you return for the main function. return is always used within a function, there is no code outside functions in C, it's a procedural language. main is a function too.
This means that if you use return from within the main function your program ends. But the statement still does the same as in every other function.
In main you should prefer return to exit for one reason: exit doesn't return. If you call exit, no code is executed after that. Important cleanup of your app might not take place. exit calls CRT shutdown code, object destructors (in C++, of global objects, not of dynamic or local objects) and ends.
You shouldn't ever use exit in any other function either: it's the same problem (resources might not be freed, important shutdown code not executed) but worse, because the function doesn't know what the call stack did. Each function should be an entity, independent of what the other functions do. A function should be able to rely on that another function either will surely return or never return (like exit).
If you ever encounter a critical error in your app and it must be terminated you could in C++ throw a bad_exception or in C and C++ call abort(), which really ends your app immediatly without doing anything else.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 10th, 2003, 06:04 PM
#10
Thread Starter
Hyperactive Member
Ok, lets say I have an if statement. If the statement is true, I want the program to perform an action and continue. On the other hand, If the statement is false, I want the program to exit entirely. How would I do this?
Thanks for the explanation. I had a feeling that exiting the program with exit() couldn't be too good.
-
Feb 11th, 2003, 03:37 PM
#11
That depends on where the if statement is.
In main:
Code:
if(!condition)
return 0;
// continue here
// ...
return 0;
Some people claim that every function should have exactly one return statement:
Code:
if(condition) {
// stuff here
}
return 0;
In other functions it could be like this:
Code:
void func(void)
{
if(!condition)
return;
}
int main()
{
func();
return 0;
}
Or
Code:
int func(void)
{
if(!condition) {
return 0;
}
// ...
return 1;
}
int main()
{
if(!func()) {
return 0;
}
return 0;
}
which again makes it a problem of the main function.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 12th, 2003, 02:00 AM
#12
Thread Starter
Hyperactive Member
Ok, what if I just want to exit a program from withen a function?
-
Feb 12th, 2003, 07:30 AM
#13
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 13th, 2003, 07:00 AM
#14
Thread Starter
Hyperactive Member
Good point, I don't think I would ever exit a program outside of an if statement anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|