-7

I'm new to C++ programming, just can't figure out why it is giving me an "else without a previous if" error.

class Math {
    public:
        void m_syllabus() {
            string math1;

            if  (math1 == "math"){
                cout << "\tHere are the important information for the math syllabus:\n"<<endl;
                cout << "\tInstructor name: \n" <<"\tDr. Zhong J"<<endl;
                cout << "\tOffice Hours: \n" <<"\tMonday and Wednesday from 10:30am-11:30am and by appointment"<<endl;
                cout << "\tContact: \n" <<"\tPhone: 245-9966"<< "\tEmail: zjj10@txstae.edu"<<endl;
                cout << "\tTextbook: \n" <<"\tPrecalculus, Sullivan, 10th edition"<< endl;
                cout << "\tGrading: \n" <<"\tTest 1, 2, 3 are 15% each"<< endl;
                cout << "\tAttendance:\n" <<"\t10% "<< endl;
                cout << "\tQuizzes:\n" <<"\t25%"<< endl;
                cout << "\tFinal Exam:\n" <<"\t20%"<< endl;
                cout << "\tTest 1: 9/21(Wed)"<<endl;
            }
        }
};

// the English class

class English {
    public:
        void e_syllabus() {
            string english1, math1;
            else if (english1 == "english")
            {
                cout << "\tHere are the important information for the english syllabus:\n"<<endl;
                cout << "\tInstructor name: \n" <<"\tHeather "<<endl;
                cout << "\tOffice Number & Hours: \n" <<"\tTuesday and Thursday from 11:00am-1:30pm and by appointment @ Flowers Hall: 210"<<endl;
                cout << "\tContact: \n" <<"\tPhone: x53783"<< "\tEmail: hr@txstate.edu"<<endl;
                cout << "\tTextbook: \n" <<"\tReading the World: Ideas that Matter & The Bedford Handbook";
            }
        }
};

int main() {
    string math1, english1, computer_science1, philosophy1, us1100_1;
    math1 = "math"; english1 = "english"; computer_science1 = "computer_science";
    philosophy1 = "philosophy";
    us1100_1 = "us1100";
    cout << "What syllabus do you need?\n";
    cin >> math1, english1, computer_science1, philosophy1, us1100_1;

    Math retrieve_m;
    retrieve_m.m_syllabus();

    English retrieve_e;
    retrieve_e.e_syllabus();

    return 0;
}
user94559
  • 59,196
  • 6
  • 103
  • 103
Chico
  • 7
  • 1
  • 3
    `else if (english1 == "english")` in class English. – Robert Prévost Sep 18 '16 at 03:55
  • Please fix your formatting, this code is exceedingly hard to read. – shuttle87 Sep 18 '16 at 03:56
  • Look at your "English" class. – PM 77-1 Sep 18 '16 at 03:56
  • 2
    Pretty straightforward syntax error. You're typing "else if" in your English class. An 'if' statement in C++ must be of the form if (test-condition) { ...} else { .. }. Just remove the stray 'else' and that will fix that part. 'else' in C++ tells the program what to do when 'if' tests false instead of true – SomeDude Sep 18 '16 at 03:57
  • I removed the stray else from the english class, which works, but when i run the code, and I type in "math" for example, for it to bring up the information on the math syllabus, nothing happens. any idea why? @SomeDude – Chico Sep 18 '16 at 04:05
  • The variable named `english1` (which is a *terrible* name), defined in the member function `e_syllabus` of the class `English` is not affected by doing formatted input to the variable named `english1` (which is still a terrible name) in the free function `main`. Please try to get a good book/tutorial on C++: http://stackoverflow.com/q/388242/1116364 – Daniel Jour Sep 18 '16 at 06:05
  • Why the down votes? He clearly stated **new to C++** thus he came here for help and provided a clear question with the code. – ArtiomLK Aug 05 '17 at 17:04

1 Answers1

1

You cannot have an else if by itself.

In this part of the code you have;

void e_syllabus() {
        string english1, math1;
        else if (english1 == "english")
        {
            cout << "\tHere are the important information for the english syllabus:\n"<<endl;
            cout << "\tInstructor name: \n" <<"\tHeather "<<endl;
            cout << "\tOffice Number & Hours: \n" <<"\tTuesday and Thursday from 11:00am-1:30pm and by appointment @ Flowers Hall: 210"<<endl;
            cout << "\tContact: \n" <<"\tPhone: x53783"<< "\tEmail: hr@txstate.edu"<<endl;
            cout << "\tTextbook: \n" <<"\tReading the World: Ideas that Matter & The Bedford Handbook";
        }

Zooming you have;

else if (english1 == "english")

The correct way to implement it would be something like:

if(A == B){
    //do something
}
else if (C == D){
    //do something here
}

In your case would be;

    if(A == B){
        //do something
    }
    else if (english1 == "english")
    {
        cout << "\tHere are the important information for the english syllabus:\n"<<endl;
        cout << "\tInstructor name: \n" <<"\tHeather "<<endl;
        cout << "\tOffice Number & Hours: \n" <<"\tTuesday and Thursday from 11:00am-1:30pm and by appointment @ Flowers Hall: 210"<<endl;
        cout << "\tContact: \n" <<"\tPhone: x53783"<< "\tEmail: hr@txstate.edu"<<endl;
       cout << "\tTextbook: \n" <<"\tReading the World: Ideas that Matter & The Bedford Handbook";
     }
ArtiomLK
  • 2,120
  • 20
  • 24