Prompts for input when not supposed to

I am writing a simple program for practice for my final exam, but seem to have come across a weird error. This code compiles, but when I run it is just blank. It doesn't even reach first line of main, as I have tested in my main.cpp. Then if I type something, for example an s, it just says:
1
2
s
"<stdin>", line 1: incomplete name definition


What could be causing this? Here is my code below.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>
#include "flex.h"


using namespace std;

int main()
{
   cout << "first line of main" << endl;
   Flex a;
   cout << "made it" << endl;

   cout << a;
   cout << "made it past cout << a";
   return 0;
}


flex.cpp
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
#include <iostream>
#include <cstring>
#include "flex.h"

using namespace std;

ostream& operator <<(ostream& outStream, const Flex& f)
{
   outStream << " made it to overlaod " << endl;

   outStream << "*";
   for(int i = 0; i < f.currentSize; i++)
        outStream << f.word[i];
   outStream << "*";

   return outStream;
}

Flex::Flex()
{
   currentSize = 1;
   word = new char[currentSize + 1];
   word[0] = ' ';
}

Flex::Flex(const char *w)
{
   currentSize = strlen(w);

   word = new char[currentSize + 1];
   strcpy(word, w);
}

Flex::~Flex()
{
   delete [] word;
}

void Flex::cat(Flex w)
{


}

void Flex::Grow(int newSize)
{


}



flex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

class Flex
{

   friend ostream& operator <<(ostream& outStream, const Flex& f);

   public:

   Flex();
   Flex(const char *s);
   ~Flex();
   void cat(Flex f);

   private:

   void Grow(int newSize);
   int currentSize;
   char* word;
};


makefile
1
2
3
4
5
6
7
8
9
10
11
12
flex : main.o flex.o
        g++ -o flex main.o flex.o

main.o : main.cpp flex.h
        g++ -c main.cpp

flex.o : flex.cpp flex.h
        g++ -c flex.cpp

clean :
        rm *.o


This is not related to C++, the code seems fine and the program compiles and behaves as expected for me:
$ make
$ ./flex
first line of main
made it
 made it to overlaod 
* *made it past cout << a


What's your OS, compiler version, etc.?
hmm, I'm on a linux server provided by my school using g++
It's likely that flex.h is not your flex.h, so you could try renaming it.
There's also no need to compile headers, so you can remove that from your makefile.
Edit: there is a program called flex, which was the one you executed. Make sure to execute your program
from the current directory with ./flex, not flex.
Last edited on
I don't know what the issue was, I compiled in another compiler and it worked. I guess I'll just use it instead for now, thanks for lookin into it.
There IS a program named flex! That was annoying lol, changed it to flexy and it worked. Thanks.
Last edited on
Topic archived. No new replies allowed.