I don't understand what is wrong with my code in here. I have created a function called pause() where everything seems okay but the compiler is catching errors.
Another problem is when ever i run the code and select option-1, it goes to option 1 but doesn't do anything else for as there are more codes are written for it.Please run the code given below and let me know. I am using Code blocks.
Pleas help me out. Thank You.
it is used for making a loop work which doesn't do anything but the loop runs again and again.
and if it's wrong please help me. i have removed it also. but its not working.
Remove the semicolon at the end of line 9: #define size 550000
In morsecode::convert() you probably have a '\n' left in the buffer from previously 'cin'.
Try adding std::cin.ignore(1) immediately before your 'getline':
1 2 3
cout << "Enter the message you want to convert to morse code:\n\n";
cin.ignore(1);
getline(cin,text);
This is a good example of why not to use usingnamespace std in your program.
"size" is defined as a member function for: vectors, lists, string and maybe some others. When the compiler runs it automatically puts "std::" in front of "size" and is expecting more than what you have. I changed "size" to "MAXSIZE" in the #define and in the for loop and it worked. Also Enoizat's first point is needed. Having an empty for loop like you have does not make an effective pause. You might want to consider one of these:
Hi, @Handy Andy.
Your analisys is perfect, as usual, and
This is a good example of why not to use using namespace std in your program.
I agree 100%!
In this case I think the problem was that, if you write #define size 550000; ,
then "size" becomes equal to "550000;". Therefore the line for(longint j=0;j<size;j++){
is turned into: for(longint j=0; j<550000;; j++){
where there're three semicolons instead of two.
So, if I may, I would add that in general preprocessor macros are 'wild beasts' :-) not easy to deal with, and the classical constint SIZE = 55000; or the 'new' constexprint SIZE = 55000;
should be preferred.
n this case I think the problem was that, if you write
#define size 550000; ,
then "size" becomes equal to "550000;". Therefore the line
for(long int j=0;j<size;j++){
is turned into:
for(long int j=0; j<550000;; j++){
Actually no, what I was seeing and my understanding is that when the preprossor sees "#define size 550000" it is changing "size" to "std::size" which is "std::size(s)" where the "s" is a std::string and the function returns the size of the string. So when you get to j < size; it is expecting the "size" function to return an answer and it is not. There is no defining size as a number as the preprossor and compiler is seeing "std::size" as an incomplete member function of "std::string".
In my Visual Studio the second ";" of the for loop was flagged as a problem and the compile gave me an error because what would turn into "std::size" is not a complete expression and did not return a number. Defining constint SIZE = 55000; or the 'new' constexprint SIZE = 55000; did work although I felt that "MAXSIZE" is more descriptive name. Even as #define MAXSIZE 550000 it worked.
When usingusingnamespace std; it all comes down to what name you choose for variables and "#define"s.
Based on what I know I hope that clears things up,
The problem with the attempted "busy idle" with the pause function as written (after the compile-times errors have been fixed) is that the entire function would be optimised away into a single return statement.
1 2 3 4 5 6 7 8 9 10 11
constexprlong size = 550'000 ;
void pause( int p ) {
for( int i=0; i<p ; i++ ) {
for( long int j=0; j<size; j++ ) {
;
}
}
}
By making access/modifications to the loop counter variables part of the observable behaviour of the program; we can get the busy-idle effect that was intended by the programmer.
1 2 3 4 5 6 7 8 9 10 11
constexprlong size = 550'000 ;
void pause( int p ) {
for( volatile int i=0; i<p ; i++ ) {
for( volatile long int j=0; j<size; j++ ) {
;
}
}
}
Needles to say, a busy-wait is required only if the caller of pause can't afford to yield (for example, in a device driver); otherwise strongly favour std::this_thread::sleep_for as suggested earlier.
Actually no, what I was seeing and my understanding is that when the preprossor sees "#define size 550000" it is changing "size" to "std::size" which is "std::size(s)" where the "s" is a std::string and the function returns the size of the string. So when you get to j < size; it is expecting the "size" function to return an answer and it is not. There is no defining size as a number as the preprossor and compiler is seeing "std::size" as an incomplete member function of "std::string".
Actually to preprocessor is rather dumb. It doesn't know anything about C or C++.
It just replaces size with 550000 like the search & replace function in an editor.
@JLBorges
This is the very first time that I chance upon an example of actual usage of the 'volatile' qualifier (I mean, not made for didactic purposes). I really appreciate your solution, even if I'm totally unable to understand the assembly code you showed us.