Hello ahmedddddddd,
Sorry this is long it kink of got away from me, so it is in two parts.
Looking at your program it is easy to see that it is written for Windows.What is not easy to figure out is what IDE you are using, if any, as it would help in answering your questions.
I am not saying that your program is totally wrong as it can be made to work, but there are some better way to do what you did.
The following code works and I will use it to explain where you are having problems.
1 2 3 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <cctype> // <--- Added. For "std::tolower()" and "std::toupper()".
#include <iostream>
#include <iomanip> // <--- Not needed in this program, but will be used in the future. For "std::setpercision()" and "std::setw()".
#include <string>
#include <conio.h>
#include <windows.h>
#undef min
#undef max
#include <limits>
//#include <ctime>
#include <cstdlib>
#include <chrono>
#include <thread>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
//void repeation(int maxx); // <--- Not needed if you put the functions in this order.
void delay(DWORD de)
{
Sleep(de);
}
void repeation(int maxx)
{
int numb = 0;
while (maxx > numb && !_kbhit())
{
std::cout << "\n any thing";
delay(1000);
numb++;
}
if (_kbhit())
_getch();
//numb = 0; // <--- Not needed. taken care of by line 28.
delay(2000);
}
void command(char choose)
{
constexpr size_t MINNUM{ 0 }, MAXNUM{ 10 };
if (choose == 'y' || choose == 'Y') // <--- Should be done in "main" not here.
{
int number;
std::cout << "\n Enter the number of repeation: ";
std::cin >> number;
//if (!(std::cin >> number))
//{
// std::cout << "\n Invalid number!\n";
// number = 0; // <--- Set to zero here for the function call on line 108.
// std::cin.clear();
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//}
//else
// std::cout << "\n Press any key to stop the repeat.\n";
//while (!std::cin)
//{
// std::cout << "\n Invalid number!\n";
// number = 0; // <--- Set to zero here for the function call on line 108.
// std::cin.clear();
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// std::cout << "\n Enter the number of repeation: ";
// std::cin >> number;
//}
while (!std::cin || (number < MINNUM || number > MAXNUM))
{
if (!std::cin)
{
std::cout << "\n Invalid number!\n";
number = 0;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.;
std::cout << "\n Enter the number of repeation: ";
std::cin >> number;
}
else
{
std::cout << "\n Number out of range! Range is 1 - 10.\n";
std::cout << "\n Enter the number of repeation: ";
std::cin >> number;
}
}
std::cout << "\n Press any key to stop the repeat.\n";
delay(2000);
repeation(number);
}
else
{
std::cout << "\n Denied.";
delay(1000);
}
}
int main()
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Requires header files "chrono" and "thread".
while (1)
{
//............................------------------------------...........................;
std::cout << "\n\n repeatation program " << std::endl;
std::cout << " ____________________________ \n\n";
std::cout << "\n Enter y to choose to repeat: ";
char choice;
std::cin >> choice;
std::cout << "________________________________ \n\n";
command(choice);
if (std::toupper(choice) != 'Y') // <--- An alternative for the if condition. And, if moved up, an alternative to the if/else in the "command" function.
{
// <--- A "cout" statement of your choice. e.g., "Denied.".
break;
}
system("cls");
}
return 0;
}
|
Starting with the header files.As
jonnin mentioned you should fix your header files.Mixing C and C++ header files is not always a good idea.Sometimes you have no choice like with "Windows.h" many of the C header files do have a C++ version that starts with "c" followed by the C file name and no extension like "cstdlib".At http ://www.cplusplus.com/reference/ click the "+" next to "C Library" to see the C++ equivalent of the C header file. By clicking on the file name you can see what is available for the given header files.
The "cctype" is optional for this program.I included and used it as an example of what could be done.
"iomanip" is not needed for this program, but you might as well get use to seeing and using it because it will be used in the future.
"conio.h":
salem c once wrote :
#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.
|
Another problem here is that your "conio.h" may be old enough that it might contain functions that are no longer available in the newer versions of the header file.And even then the header file is for backward compatibility and not meant to be used in newer programs.
A couple of years ago when I first tried to mix "Windows.h" and "limits" trying to use the line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
I had a problem with the "max()" that "limits" wanted to use.As I found out later "Windows.h" defines the macros of "min" and "max" as:
#define min(a, b) (((a) < (b)) ? (a) : (b))
and
#define max(a, b) (((a) > (b)) ? (a) : (b))
Unfortunately the "min" and "max" in "limits" is different than the above code hence the
#undef
s before you include "limits". This will let the "std::cin.ignore(...)" work properly.
"ctime" or "time.h", you should use the "ctime", is not used in your program.As such it is not needed and is just putting extra code into your program that you are not using.
The header files
chrono
and
thread
are there to show you an example replacement for the "Sleep()" you are using.
For using
using namespace std;
read the link.It is worth the time as is http ://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
Line 19 the prototype can be deleted if you order your functions as I have done.The reason you needed the prototype is because you were trying to call a function that had not yet been defined.This way "delay" is define and compiled before "repeation" used it and both functions are defined and compiled before "command" uses them.If you are going to prototype a function you might as well prototype all the functions just to be safe.Also prototyping all functions means that you do not have to worry about the order the functions are done in.
In the "delay" function Microsoft has this to say about "Sleep" :
void Sleep(
DWORD dwMilliseconds
);
|
Where "DWORD" is
typedef unsigned long DWORD;
on my computer/IDE/header files. It is possible that this could be different with a different compiler and header files.Your use of sending a "float", a "double" is the preferred floating point type these days, to the function which eventually is stored in a "DWORD" could cause possible data loss when the decimal portion is dropped to store the whole number.The one place you used the "float" by sending "0.01" to the function ended up with a value of(10) and (10) milliseconds is not enough time to see any delay.
By reworking the "delay" function you could just as easily replace "delay" in the program with "Sleep" and eliminate the need for the function.
End part 1.