|
-
Dec 23rd, 2002, 01:10 AM
#1
Thread Starter
Hyperactive Member
Anything wrong with this
I'm a newb to c++ and I don't want to start off with bad practices (I came from vb). Heres a little temp. conversion program I made. Tell me what you think.
Code:
#include <iostream>
int selection;
int number;
int ftoc(int number)
{
return((number-32)*(1.8));
}
int ctof(int number)
{
return((1.8*number)+32);
}
int main()
{
std::cout << "Welcome to the Fahrenheit/Celsius Conversion Utility! \n";
std::cout << "Please choose a number: \n";
std::cout << " [1] Fahrenheit to Celsius\n";
std::cout << " [2] Celsius to Fahrenheit\n";
std::cin >> selection;
std::cout << "Please enter the number to be converted: \n";
std::cin >> number;
switch(selection)
{
case 1:
std::cout << "Result: " << ftoc(number) << "\n";
break;
case 2:
std::cout << "Result: " << ctof(number) << "\n";
break;
default:
std::cout << "Please choose either 1 or 2.\n";
break;
}
return 0;
}
Anything look bad here? I know about the truncated integer variable. I figure this is for temperature conversion so I don't need the precision. I also received this warning from the compiler:
"warning: no new line at end of file"
Any idea what this means?
Thanks.
-
Dec 23rd, 2002, 04:08 AM
#2
Addicted Member
Is it a precompiled header project? If so, that 'could' be causing the problem. I know I've had the same error before but I can't remember the reason.
Also, by putting
Code:
using namespace std;
at the top (below #include) you can just write:
Code:
cout << "Some info";
Other than that it looks OK. Of course you have placed the cout's in a number of lines - it could be brought together, but that would probably be harder to read.
HD
-
Dec 23rd, 2002, 04:09 AM
#3
Hyperactive Member
Its good but why are you using global variables?
PHP Code:
int selection;
int number;
should be in int main();
-
Dec 23rd, 2002, 05:36 AM
#4
Monday Morning Lunatic
The warning from the compiler means exactly what it says it means. You don't have a newline at the end of your file 
By definition, text files *must* end with a newline.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 23rd, 2002, 06:51 AM
#5
Thread Starter
Hyperactive Member
Originally posted by made_of_asp
Its good but why are you using global variables?
PHP Code:
int selection;
int number;
should be in int main();
If I put them in main, will I still be able to use them in the functions?
Originally posted by parksie
The warning from the compiler means exactly what it says it means. You don't have a newline at the end of your file
By definition, text files *must* end with a newline.
How do I fix this?
-
Dec 23rd, 2002, 07:28 AM
#6
Addicted Member
You are passing 'number' as a parameter to the functions - they can therefore be local to main.
Selection is not used by the functions and therefore not necessary as a global.
If you wanted to, you could take it a stage further and make this a class. Then you could make the functions member methods and the selection/number globals could become member variables - but that may be unnecessary for you to do.
To fix the newline problem - add a newline.
HD
-
Dec 23rd, 2002, 07:29 AM
#7
Hyperactive Member
Originally posted by Comreak
If I put them in main, will I still be able to use them in the functions?
How do I fix this?
you will be able to use them in functions as long as you pass them as parameters.
-
Dec 23rd, 2002, 07:34 AM
#8
Thread Starter
Hyperactive Member
Thanks for the suggestions HairyDave and made_of_asp. What are the disadvantages to makeing "selection" and "number" global variables?
Edit://I take back what I said about not wanting to create objects (if you read it before I edited it that is). Sounds like it could be interesting. Thanks.
Last edited by Comreak; Dec 23rd, 2002 at 07:38 AM.
-
Dec 23rd, 2002, 07:47 AM
#9
Hyperactive Member
Global variables greatly make your program more prone to bugs.
The reason for that is, that functions all over your program are able to change the variable. You may loose the variable value without even knowing it.
The only places where global variables must be used are:
1. global objects that are used by a great number of functions around your program. You would need to keep track of them very carefully.
2. volatile variables for multithreaded applications.
There are probably more, but these are the two main ones for me.
OOP helps in this case, for example:
VB Code:
class MyConventer
{
void SetNumber(int);
int* GetNumber();
protected:
int number;
};
void MyConventer:SetNumber(int e)
{
this->number = e;
}
int* GetNumber()
{
return &this->number;
}
this is just an example of organising things
Last edited by made_of_asp; Dec 23rd, 2002 at 07:51 AM.
-
Dec 23rd, 2002, 08:24 AM
#10
Don't use pointers in C++ unless you have to. And the explicit this-> is not necessary.
Code:
class MyConventer
{
void SetNumber(int);
int GetNumber();
protected:
int number;
};
void MyConventer:SetNumber(int e)
{
number = e;
}
int GetNumber()
{
return number;
}
Or if you really want to make the number modifyable when returned from GetNumber (is not necessary) then you would do
Code:
class MyConventer
{
void SetNumber(int);
int& GetNumber();
protected:
int number;
};
void MyConventer:SetNumber(int e)
{
number = e;
}
int& GetNumber()
{
return number;
}
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.
-
Dec 23rd, 2002, 08:24 AM
#11
Thread Starter
Hyperactive Member
Sounds good. If I declare a variable in main and initialize it there as well and then try to change that value in another function, what would happen? Would the compiler give me and error or something? Sorry about the questions, but I'm a little curious about it.
-
Dec 23rd, 2002, 08:26 AM
#12
The compiler would give you an "undeclare identifier" error. Since the variable is declared in the scope of main it simply doesn't exist outside of main.
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.
-
Dec 23rd, 2002, 08:28 AM
#13
Thread Starter
Hyperactive Member
I see, trying to modify something that doens't exist. I'm so used to vb. Although, I do like somethings better in C++.
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
|