Alas, two response posts before I could completely formulate an answer...
C++ doesn't work like FORTRAN or BASIC -- it requires a definite syntactical structure. All code must be in a function, and you cannot nest functions.
If you take the time to read through the tutorials on this site (as
Moschops linked for you) you will be up to speed, sufficient to teach basic C++, at least.
A couple of professional notes to help you get started.
--- 1 ---
Avoid teaching dangerous "simplified" constructs and stick with correct ones. It may require a bit more typing, but it has the same memetic value without the horrors. Then when you completely remove the scaffolding (after your course is completed) your students will not need to unlearn bad habits you shouldn't have modelled for them.
For example, you used a common bad beginner's idiom by recurring to
system() to keep the console window open long enough to see the program's output on Windows. Don't do that. The
system() command has very well-documented issues, not the least of which is that it subverts system security. Instead, insist that the student understand a basic piece of information about handling user input:
All user input is terminated by pressing the ENTER key.
Then, as the student gathers input, he should get rid of the ENTER key presses (the 'newlines') at the end of each input. At the end of the program, he can then ask for, essentially, a blank input: press the ENTER key.
--- 2 ---
Keep program flow and I/O in the
main() function. Delegate computation to other functions. Functions should not know anything about the world outside itself.
A simple function to ask for and get user input is not necessary -- as it is properly part of the main program. The 'celsius' function knows a little too much about the world -- it is trying to do something that belongs in
main(): interact with the user and modify data outside itself.
Likewise, a function to calculate
part of a formula is not necessary. Calculations should not be split up any more than absolutely necessary. The 'fahrenheit' function does not represent a complete formula, and it knows too much about things outside itself. (Remember, a function takes an argument and uses
only that argument (or arguments) to compute a value to
return to the caller.)
--- 3 ---
Stick to well-known formulae. The F-->C and C-->F functions are pretty standard fare:
http://www.albireo.ch/temperatureconverter/formula.htm
Also be careful how you space out your operations.
factor * celsius/100 + 32
This is just too easy to read as (factor * (celsius/100)) + 32. Yet, because 'celsius' is an integer, the
integer division would likely produce zero for normal temperatures, and the result would always be around 32. Remind students of their basic mathematical order of operations (PEMBAS or whatever weird acronym they are familiar with). It does not matter that the calculation would actually work, because what the student is
learning is in part assumed by the way you present it. It would have been better to write:
factor*celsius / 100 + 32
Best practice, IMHO, is to put equal spacing around all operators and, if necessary, apply liberal use of parentheses. Hence, the following are best:
((factor * celsius) / 100) + 32
factor * celsius / 100 + 32
--- 4 ---
Make sure to point out compiler-specific stuff, such as the
#include <stdafx.h>
thing that Microsoft compilers like to have. If you think that your students may be using non-MS compilers (assume that some will!), then you may even have them wrap it up so that both you and they can compile cleanly:
1 2 3
|
#ifdef _MSC_VER
#include <stdafx.h>
#endif
| |
---------
Well, here is some code that explains itself pretty well. Feel free to use it as part of your coursework.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <limits>
using namespace std;
//-------------------------------------------------------------------
// This is our library function to get rid of the ENTER key after
// the user has given us input.
//
// (Remember, the user will ALWAYS press ENTER after EVERY input!)
//
void clear_enter_key()
{
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
}
//-------------------------------------------------------------------
// This is our library function to PAUSE the program.
//
void press_enter_to_continue()
{
cout << "Press ENTER to continue." << flush;
clear_enter_key();
}
//-------------------------------------------------------------------
// This function converts C to F using the well-known
// formula:
// 9
// F = C * - + 32
// 5
//
// It is important to remember that since we are using
// INTEGERS that 9/5 is ONE, so we rely upon operator
// precedence order to do things from left to right:
// first we multiply by 9, then integer divide by 5,
// then add 32.
//
// The results would be more precise if we were using
// 'float' instead of 'int', because that division would
// then be a floating-point division. Do you see why?
//
// If C is an 'int', then C * 9 is an 'int'.
// If C is a 'float', then C * 9 is a 'float'.
//
int celsius_to_fahrenheit( int C )
{
return C * 9 / 5 + 32;
}
//-------------------------------------------------------------------
// This is the main program.
//
int main()
{
int celsius;
cout << "Enter the temperature in Celsius: ";
cin >> celsius;
clear_enter_key();
int fahrenheit = celsius_to_fahrenheit( celsius );
cout << "The temperature in Fahrenheit is " << fahrenheit << ".\n";
press_enter_to_continue();
return 0;
}
| |
Once that is digested, it might be a workable exercise to have the students write a function to work the other way:
1 2 3 4
|
int fahrenheit_to_celsius( int F )
{
return (F - 32) * 5 / 9;
}
| |
Hope this helps.