Is there any problem with my compiler?

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.

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
  #include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
using namespace std;
#define size 550000;

class morsecode{
private:
    string text;
    char msg[10001];

public:

    void choose();
    void convert();
    void translate();
    void exit();

    void pause(int p){
        for(int i=0;i<p;i++){
            for(long int j=0;j<size;j++){
                ;
            }
        }
    }

};
void morsecode::choose(){
    system("CLS");
    cout << "1.Convert message\n";
    cout << "2.Translate message\n";
    cout << "3.Exit\n";
    cout << "\nEnter the number you want to choose:";
    int a;
    cin >> a;
    if(a==1){
        convert();
    }
    else if(a==2){
        translate();
    }
    else if(a==3){
        exit();
    }
    else{
        system("CLS");
        cout << "Wrong input\n";
        pause(3);
        choose();
    }
}

void morsecode::convert(){
    system("CLS");
    cout << "Enter the message you want to convert to morse code:\n\n";
    getline(cin,text);
    int len=text.length();
    cout << len;

}

void morsecode::translate(){
;
}
void morsecode::exit(){
;
}



int main(){
    morsecode obj1;
    obj1.choose();



    cout << "\n\n\n\n\n";
return 0;
}
1
2
3
4
5
6
7
 void pause(int p){
        for(int i=0;i<p;i++){
            for(long int j=0;j<size;j++){
                ; //What's this ninja semicolon doing here?
            }
        }
    }
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.
What exactly isnt working. Can you post your errors?
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);

Thanks for your help. Its working right now.
Hello r13ayan,

This is a good example of why not to use using namespace 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:

std::this_thread::sleep_for(std::chrono::seconds(5));

std::this_thread::sleep_for(std::chrono::milliseconds(5000));

They both pause for the same amount of time and require the header files "chrono" and "thread".

Hope that helps,

Andy
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(long int j=0;j<size;j++){

is turned into:
for(long int 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
const int SIZE = 55000; or the 'new' constexpr int SIZE = 55000;
should be preferred.
Last edited on
Hello @Enoizat,

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 const int SIZE = 55000; or the 'new' constexpr int SIZE = 55000; did work although I felt that "MAXSIZE" is more descriptive name. Even as #define MAXSIZE 550000 it worked.

When using using namespace 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,

Andy
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
constexpr long size = 550'000 ;

 void pause( int p ) {
        
        for( int i=0; i<p ; i++ ) {
            
            for( long int j=0; j<size; j++ ) {
                ; 
        }
    }
} 

https://godbolt.org/g/SF2BRu


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
constexpr long size = 550'000 ;

 void pause( int p ) {
        
        for( volatile int i=0; i<p ; i++ ) {
            
            for( volatile long int j=0; j<size; j++ ) {
                ; 
        }
    }
} 

https://godbolt.org/g/uYqo1g

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.
@Handy Andy
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.

@Thomas1965
I think you got the point, IMHO.
Topic archived. No new replies allowed.