Undefined symbol error in Turbo C++

Pages: 123
AbstractionAnon:

Don't have enough context to evaluate that statement.


Should I give you the complete code?

Lines 30,48. If the user enters S, you will execute both iScience() and iSports(). I pointed this out to you once before.

Lines 62-80. Ditto.


I've corrected both the lines but I'm getting the same error:

identifier bScience cannot have a type qualifier in-

1
2
void Quiz::bScience()
{here clrscr();
................}

And also: Declaration missing ; at the end of the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Quiz::bArts()
  { clrscr();
    char userAns1, ch1[50];
    cout<<"\n\n\n\n You're interested in 'Arts'";
    cout<<"\n and you think you're a 'Beginner'";
    cout<<"\n So, let's start...";
    for(int i=0;i<10;i++)
  {
  ifstream f3;
  f3.open("bArtQues.txt");
  while (!f3.eof())
  {f3.getline(ch1, 50, '\n');
  cout<<ch1<<endl;}
  cout<<"\n\n What's your answer?:";
  cin>>userAns1;
  ifstream f4;
  f4.open("bArtAns.txt");
  while (!f4.eof())
  {f4.getline(ch1, 50, '\n');
  cout<<ch1<<endl;}
  void quizComp();}
  }
}here



identifier bScience cannot have a type qualifier

I'd look at the preceeding lines for a syntax error.

Declaration missing ; at the end of the following code:

If i counted correctly, you have an extra } in that function.
Your indentation style does NOT help to identify such errors.
Here is the same function whith a resonable indentation style.
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
void Quiz::bArts()
{   clrscr();
    char userAns1, ch1[50];
    cout<<"\n\n\n\n You're interested in 'Arts'";
    cout<<"\n and you think you're a 'Beginner'";
    cout<<"\n So, let's start...";
    for(int i=0;i<10;i++)
    {   ifstream f3;
        f3.open("bArtQues.txt");
        while (!f3.eof())
        {   f3.getline(ch1, 50, '\n');
            cout<<ch1<<endl;
        }
        cout<<"\n\n What's your answer?:";
        cin>>userAns1;
        ifstream f4;
        f4.open("bArtAns.txt");
        while (!f4.eof())
        {   f4.getline(ch1, 50, '\n');
            cout<<ch1<<endl;
        }
    void quizComp();
    }
}  // <- extraneous }
}

You can see immediately that there is an extra }.

This function also has some of the same errors as previously pointed out in bScience().

Line 9-10: You're opening f3 10 times. You're always going to be reading from the front of the file as previously explained.

Line 21: This is a function declaration, not a function call. Again, prevously pointed out.





AbstractionAnon:

Here you can download my program file as I can't post the whole code :

http://bytes.com/attachments/attachment/8107d1419517114/quiz.txt

I'm receiving the same error of declaration missing ; at the end of last } (Line 25 in above post) , identifier bScience cannot have a type qualifier at the same position of before and declaration missing } at the end of last } (Line 25 in above post).

Why is that so?
Check the pairing of your braces in Quiz::Start

192
193
194
    {   if((lchoice=='B')||(lchoice=='b'))      // Extraneous {
             clrscr();                                       // { on wrong line
        {   cout<<userName<<" In which of the following topics you're interested in?";


Only the call to clrscr() is conditional on the if.

314
315
316
317
318
319
320
321
             else
            {    cout<<"\n Wrong selection. Enter again:";
                cin>>tchoice;}
            }
        }
        // <- Missing } ending the function.

void Quiz::bScience()


As I have stated before, your indentation sytle is NOT helping you to find mismatched or unmatched {}. A good IDE should highlight paired {} for you.

Also note that in bScience, you still have a function declaration, not a function call.
 
        void quizComp();

You stated you fixed this.

Note that my line numbers are approximate and may not match the code in your link exactly.

It gets tiresome to point out the same problem to you multiple times.
Last edited on
AbstractionAnon:

Check the pairing of your braces in Quiz::Start


1
2
3
{   if((lchoice=='B')||(lchoice=='b'))      // Extraneous {
             clrscr();                                       // { on wrong line
        {   cout<<userName<<" In which of the following topics you're interested in?";



Only the call to clrscr() is conditional on the if.


1
2
3
4
5
6
7
8
  else
            {    cout<<"\n Wrong selection. Enter again:";
                cin>>tchoice;}
            }
        }
        // <- Missing } ending the function.

void Quiz::bScience()


I'm taking a print out of my entire code to check my missing or extra braces. I think that will solve.

Also note that in bScience, you still have a function declaration, not a function call.

void quizComp();


I've already corrected the code to quizComp(); only.

As I have stated before, your indentation sytle is NOT helping you to find mismatched or unmatched {}. A good IDE should highlight paired {} for you.


It's my school project where I work and the teach us in Turbo C++ only. I know compilers like Visual C++ highlight the brace pairs.

Thanks.
@AbstractionAnon:

I've printed out the code and found that some braces weren't in their order while some were misplaced. I did the correction and the compiled. I'm getting just one error of Declaration terminated incorrectly at the code:

1
2
3
4
5
6
7

//same lines above

if(//here in the first bracket (tchoice=='E')||(lchoice=='e'))
     
   {clrscr();
    cout<<userName<<" In which of the following topics you're interested in?";


Can you point out where I have left a termination?

Thanks.
The message says "Declaration terminated incorrectly". There is no declaration in that code snippet. You need to back up at least to the previous declaration.

Line 4 is a problem. There is no condition or closing ) for the if statement.

they teach us in Turbo C++ only. I know compilers like Visual C++ highlight the brace pairs.

That's no excuse for not using a reasonable indentation style.

In thinking about your program, it occurs to me that you have 21 functions that are nearly identical (7 subjects x 3 levels). The only thing different between those 21 functions is the name of the question and answer files. That is what arguments are for. Write ONE function and pass the name of the question and answer files as arguments to that function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Quiz::AskQuestions (const char * quest_fn, const char * answer_fn)
{  ifstream f3, f4;
    clrscr();
    char userAns1, ch1[50], anskey;
    //  m_subject and m_level are member variables.
    cout<<"\n\n\n\n You're interested in " << m_subject << endl;
    cout<<"\n and you think you're a " << m_level << endl;
    cout<<"\n So, let's start...";
    f3.open(quest_fn);
    f4.open(answer_fn);
    while (f3)
    {   f3.getline(ch1, 50, '\n');
         cout<<ch1<<endl;
         cout<<"\n\n What's your answer?:";
         cin>>userAns1;
         f4.getline(ch1, 50, '\n');
         if (ch1[0] == userans1)
             cout << "That's correct" << endl;
         else
             cout << "Incorrect.  The correct answer is: " << ch1 << endl;         
    }
    quizComp();
}


Edit: Changed use of string arguments to const char *
Last edited on
AbstractionAnon:

The message says "Declaration terminated incorrectly". There is no declaration in that code snippet. You need to back up at least to the previous declaration.


There is no error in previous declarations as you say, I've checked it all.

Line 4 is a problem. There is no condition or closing ) for the if statement.


I think you misunderstood the code snippet I gave you (because of the format, I guess). Let me give it to you again:

1
2
3
4
5
6
//same lines above

if((tchoice=='E')||(lchoice=='e'))
     
   {clrscr();
    cout<<userName<<" In which of the following topics you're interested in?";


The compiler is pointing out the error in the bold ) I've highlighted above.

AbstractionAnon:

In thinking about your program, it occurs to me that you have 21 functions that are nearly identical (7 subjects x 3 levels). The only thing different between those 21 functions is the name of the question and answer files. That is what arguments are for. Write ONE function and pass the name of the question and answer files as arguments to that function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Quiz::AskQuestions (const string & quest_fn, const string answer_fn)
{  ifstream f3, f4;
    clrscr();
    char userAns1, ch1[50], anskey;
    //  m_subject and m_level are member variables.
    cout<<"\n\n\n\n You're interested in " << m_subject << endl;
    cout<<"\n and you think you're a " << m_level << endl;
    cout<<"\n So, let's start...";
    f3.open(quest_fn.c_str());
    f4.open(answer_fn.c_str());
    while (!f3.eof())
    {   f3.getline(ch1, 50, '\n');
         cout<<ch1<<endl;
         cout<<"\n\n What's your answer?:";
         cin>>userAns1;
         f4.getline(ch1, 50, '\n');
         if (ch1 == userans1)
             cout << "That's correct" << endl;
         else
             cout << "Incorrect.  The correct answer is: " << ch1 << endl;         
    }
    quizComp();
}


There are some lines which I can't understand of your code.

1. What's quest_fn or answer_fn that you passed as an argument?
2. You say m_subject and m_level are member variables. Does it mean that these two should be defined in the class declaration?
3. What these two lines means?:

1
2
f3.open(quest_fn.c_str());
f4.open(answer_fn.c_str());


I'm this much sure that these two open files of questions and answers respectively but I can't understand what .c_str() do?

Above all is this code to small the length of my program code? If yes, then please change the .c_str() to something simpler which I can understand clearly. I've not learnt about it.

Thanks.
1) quest_fn and answer_fn are std::strings containing the file names of the questions and the answers for a particular subject and level.

2) Yes.

3) c_str() is member function of the std::string class which returns a null terminated character array, which is what ifstream.open is expecting. c_str() is documented here: http://www.cplusplus.com/reference/string/string/c_str/

Above all is this code to small the length of my program code?

I don't understand what you're asking.

You could change the function to use C style strings instead of C++ strings as follows:
1
2
3
4
void Quiz::AskQuestions (const char * quest_fn, const char * answer_fn)
{ ...
    f3.open(quest_fn);
    f4.open(answer_fn);

C++ strings are always preferred over C style strings.



AbstractionAnon:

I don't understand what you're asking.


I am asking that why there is a need to write the function you gave me above? The format of code I'm writing isn't suiting you? I can't change the algorithm of the program that I've thought of. Please, don't provide me with the code you gave.

And yes, I've learnt till now that files can be opened as:

1
2
//as an example to above
f3.open("fileName.txt");


Sorry, but I can't use f3.open(quest_fn);

I repeat my query, of why I'm getting Declaration terminated incorrectly in the code:

1
2
3
4
5
6
//same lines above

if((tchoice=='E')||(lchoice=='e'))
     
   {clrscr();
    cout<<userName<<" In which of the following topics you're interested in?";


The compiler is pointing out the error in the bold ) I've highlighted above.

Thanks.
I am asking that why there is a need to write the function you gave me above?

Good programmers recognize repetitive code and write a function once instead of writting essentially the same function 21 different times. If your professor is requiring you to write the same function 21 times, you are being taught poorly.

The format of code I'm writing isn't suiting you?

I'm trying to help you improve your code. If you want to turn in sloppy code for your project, that is up to you. I'm not trying to write your project for you.

I can't change the algorithm of the program that I've thought of.

Why not? I'm not suggesting you change your algorithm, only that you don't need to write it 21 different times.

Please, don't provide me with the code you gave.

Huh? You want me to delete it from my previous post? If you don't want my help, there are others on this forum that do.

Sorry, but I can't use f3.open(quest_fn);

Why not?

I repeat my query, of why I'm getting Declaration terminated incorrectly in the code:

And I repeat me previous response. There is no declaration in that snippet. Your problem is BEFORE that snippet.

There is a problem with that if statement. Your two variables names don't agree, but that is not the cause of the error you're exepriencing. Check any preceding declarations for a missing ;



Last edited on
AbstractionAnon:

I'm not saying that you shouldn't provide me any further help. It's just that I didn't even thought of the type of function you gave.

Just tell me one thing, how does that function works?

Sorry, but I can't use f3.open(quest_fn);


Why not?


Because I haven't been taught of this type of file opening method.

And I repeat me previous response. There is no declaration in that snippet. Your problem is BEFORE that snippet.

There is a problem with that if statement. Your two variables names don't agree, but that is not the cause of the error you're exepriencing. Check any preceding declarations for a missing ;


I'll recheck it from beginning for a missing ;

But let me tell you a thing that before I wrote theif statements for Beginner, Intermediate and Expert it didn't gave me that error!

Thanks.
just tell me one thing, how does that function works?

I tried to follow the algorithim you were using in bScience() & bArts(), although I did correct a couple of errors in your logic.

Line 2: Declare both ifstream objects.
Line 3-5: Unchanged.
Lines 6,7: m_subject and m_level. These variables are members of the class and are set when you ask for the subject and the level. By doing this, the cout statements work for any subject and level.
Lines 9-10: The question and answer files are opened using the file names that were passed in as arguments.
Line 11: while (f3) tests that the status of the ifstream is good. Testing eof() is not the best practice. Note that your bArts() above reads the entire file, stopping only when eof is encountered. This results in only the last question in the ch1 buffer.
Line 13-15: unchanged.
Line 16: reads the next line from the answer file. We want to read one record from each file each time through the loop keeping the two files synchronized. Note that your bArts() read the entire file leaving the last answer in the ch1 buffer.
Lines 17-20: Test if the user answer matchs the answer read from the file.
Line 22: Unchanged.

Sorry, but I can't use f3.open(quest_fn);
Why not?
Because I haven't been taught of this type of file opening method.

That is no different that passing a literal string to open. open expects a const char * argument. There are many ways to supply such an argument. Here the filename variable contains the literal string and the variable is passed to open. This only difference in what I suggested is that filename is an argument and not a local variable.
1
2
3
4
5
 
  const char * filename = "bartsques.txt";    // Local variable
  ifstream f3;
  f3.open (filename);           // pass const char * variable
  f4.open ("bartsans.txt");  //  passes const char * also  


I've updated my post http://www.cplusplus.com/forum/beginner/147294/3/#msg791044 to correct a couple of errors and change the use of std::string to const char *



Last edited on
AbstractionAnon:

Okay, understood. So, now I should delete my bScience() and bArts() functions and instead of them (and others) I should write the AskQuestions() function.

There are few more things which aren't clear to me:

1. What about the name of the files bScQues.txt, bArtQues.txt, bScAns.txt and bArtAns.txt? How can your quest_fn and answer_fn open them? How should I modify these text files?

2. The while (f3) statement should follow for f4 also?

3. Are f3 and f4 streams you declared here http://www.cplusplus.com/forum/beginner/147294/3/#msg791044 are for all subjects/levels?

Thanks.
now I should delete my bScience() and bArts() functions and instead of them (and others) I should write the AskQuestions() function.

That is up to you. If you follow my suggestion, then yes, AskQuestions() replaces your 21 individual functions.

1) You don't modify your files at all. Where you would have called each of your 21 individual functions, you now call AskQuestions() passing the name of the question and answer files as arguments. e.g.
1
2
3
  AskQuestions("bScQues.txt", "bScAns.txt");
  // or 
  AskQuestions("bArtQues.txt", "bArtAns.txt");

The literals being passed as arguments are represented by quest_fn and answer_fn in AskQuestions().

2) There is the possibility the open at line 10 or the getline at line 16 could fail. It is best practice to account for that possibility.

3) Yes.


1. What about the name of the files bScQues.txt, bArtQues.txt, bScAns.txt and bArtAns.txt? How can your quest_fn and answer_fn open them? How should I modify these text files?

AbstractionAnon has showed you why already:
That is no different that passing a literal string to open. open expects a const char * argument. There are many ways to supply such an argument. Here the filename variable contains the literal string and the variable is passed to open. This only difference in what I suggested is that filename is an argument and not a local variable.


3. Are f3 and f4 streams you declared here http://www.cplusplus.com/forum/beginner/147294/3/#msg791044 are for all subjects/levels?

If you are asking if f3 and f4 exist everywhere in the program, I suggest you read about scope. And do not stop reading until you understand scope.
http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
http://en.wikibooks.org/wiki/C++_Programming/Scope/Examples
AbstractionAnon:

That is up to you. If you follow my suggestion, then yes, AskQuestions() replaces your 21 individual functions.


I've deleted those functions on your suggestions.

2) There is the possibility the open at line 10 or the getline at line 16 could fail. It is best practice to account for that possibility.


Why? Still confused..

Thanks.
Why? Still confused..

What if an answer file does not exist or is misspelled? What happens to the open? It fails.






Topic archived. No new replies allowed.
Pages: 123