Hello blade007zg,
Starting from the top:
Your header files "iostream" and "fstream" are OK."cstdlib" is the correct useage for this header file, but its use is 50/50 for including it. There are other header file that will include "stdlib.h" without you knowing it. Although some people will tell you that it is best to include all the header files that you will need. This is OK too.
"windows.h" tends to limit who can compile and run your code. I would say that anyone not using the "Windoes" operating system is likely to have a problem with the program. If this program is for your own use or for school, who uses "Windows" then it is fine. Just keep in ind that "windows.h" is not a standard C++ header file.
"conio.h" again this is not a standard C++ header file and that it is old and out dated along with newer compilers do not even include this file with their standard setup. This is fine for your own use, not everyone can use it.
"stdio.h" is s C file for input and output that you have already covered with "iostream".
"time.h" is OK, but you should use "ctime" instead."ctime" is the C++ header file.
The line
using namespace std;
should not be used because it
WILL give you problems some day. Just starting it is better to learn what is in the standard namespace and how to qualify them. For now what you are most likely to use is "std::cin", "std::cout" and "std::endl" and with the use of "fstream" "std" "ifstream" and "std::ofstream" for use with files.
These days "double" is preferred over float. The short answer is that a "double" will store a number better than a "float" and give more accurate results in calculations. If you need more information let me know.
As Thomas1965 said your global variables are better put in main. As global variable anything in the file has the ability to change the and it becomes a potential problem. With your program the way it is it is still a potential problem, but better. Anything that you would put as a global variable should start with "const" or the newer version "constexpr". After the type the variable should be in all caps to remind you that it is a constant that can not be changed. An example:
constexpr size_t MAXSIZE{ 10 };
. You can think of "size_t" as another name for "unsigned int" which you will learn is a return value for many functions that you will use. You can also write "std::size_t", but the "std::" is not always needed, but may be a good habit to get into.
As a global variable "MAXSIZE" can be used through out the file, but is most often used to define an array, i.e.,
int numArray[MAXSIZE]{}
. This way to change the array size you only have one place to go to change the size and since it is near the top of your program it is easy to find. Do not feel that you have to use "MAXSIZE". This name can be anything that describes what it is used for. Read enough here and you will see that "MAXSIZE" is most often used.
With that said the use of the "_" in the variable name is OK, but the use of camel case is more often used these days, so
another_choice
would become
anotherChoice
. What I do and some of this may be my choice is that a regular variable starts with a lower case letter. I use an upper case letter when I define a "class", "function" or "struct" to set them apart from regular variables. I use all upper case letters when I define a variable as a constant, like "MAXSIZE"
I will take this time to say that it is a good idea to initialize your variables. With the use of the "uniform initializer" the {}, it is easy
int num;
becomes
int num{};
. The empty {}s will initialize integer types to zero and floating point types, i.e., "double"s and "float"s to 0. and "char"s to '\0'. the "std::string" along with "vector"s, "list"s and others are defined as empty and do not need initialized. Some people have said that you do not need to initialize variables when they are defined which does work most of the time, but there are occasions when it is necessary to give a variable a value before it is used.
I will say this when a variable is defined memory is set aside for that variable, but the "1"s and "0"s that are left there from some other use are known as "garbage" or a "garbage value" and have no real value to your program and could potentially cause a problem in your program later.
This may be a personal preference of mine, but I like to define may variables at the top of the function be it "main" or some other function. I also like to group the variables, i.e., keep "int"s and the like together then "double"s, "char"s and "string"s. I generally end up putting"bool"s last then things like "class"es and "struct"s. Sometimes I put "class"es and "struct"s first. Some people will tell you to define variables just before they are used, but this can make them hard to find in the program. That is why I put them at the beginning of a function where they are easy to find.
Inside "main" the endless for loop works, but a do/while loop is a better choice. This is what I like to do with a do/while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <cctype> // <--- For tolower() and toupper().
int main()
{
char ans{};
bool cont{ true };
do
{
// Code here.
std::cout << "Do another Y/N: "; // <--- Change message to what you want.
std::cin >> ans;
ans = std::toupper(ans);
if (ans == 'N')
cont = false;
} while (cont);
}
|
As Thomas1965 said you should learn how to use functions. You can read about them here:
http://www.cplusplus.com/doc/tutorial/functions/ To that end I would not only put the menu in a function I would also validate the choice and only return a valid choice.
The use of "getch()" is something you and I could use, but not everyone can and on my computer I have to use getch()" for it to work because "getch()" is out dated. A better choice hare is
std::cin >> choice
and that would probably need to be followed with
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
to clear the input buffer of the new line character left there by the "stdcin >>". I will know more when I have a chance to test the program.
The use of the "switch"s are a good choice. I have with my VS IDE the case statements do not indent the way I would like, so I have to indent the case statements once i first get started to make it easier to read.
Looking through the case statements I see "srand()" this should be at the top of "main" and only done once not ease time you call that case statement. I usually write that line as:
srand(static_cast<std::size_t>(time(NULL)));
This has always worked for me, but may not be 100% correct.
Your variable "tryy" as you most likely found out that "try" is a reserved key word of "try/catch", but I would come up with a better name. Something that describes what it does would be nice.
I see "system("pause");" and "system("cls")" both and anything "system" should be avoided. For you own use it is OK, but try to avoid using "system". For "pause" I use:
1 2 3 4
|
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
|
And for 'cls" I have a function that I use. If you would like it let me know.
Again in "case 3:" for the menu I would put this in a function returning a valid choice to use in the switch.
BTW in the switch/case statements the {}s for the case statements are not needed unless you are defining a variable inside the case staatement which means that you are most likely doing something wrong.
End part 1.