Problems with my first switch statement.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freddyjones39
    New Member
    • Feb 2008
    • 5

    #1

    Problems with my first switch statement.

    I am new to C++, and using gcc for a compiler. This is my first switch statement I have written, but it will not compile, and I can't figure out why (I have stared at it for a while now). The errors messages say there are syntax errors on line 15, and 35, but I can't find them! Sorry about the obvious question.
    Thanks for your help.

    here is the code

    [code=cpp]
    #include <iostream>
    using namespace std;

    char intro(char ans);
    //Requires: The user must input one of the following a char values:
    //q,Q,m,M,i,I.
    //Promises: Returns the input to main.

    int main()
    {
    char ans= intro(char ans);
    do
    {
    switch (ans)
    {
    case 'q':
    case 'Q':
    cout<<"Q"<<endl ;
    break;
    case 'm':
    case 'M':
    cout<<"M"<<endl ;
    break;
    case 'i':
    case 'I':
    cout<<"I"<<endl ;
    break;
    default:cout<<" improper data"<<endl;
    //intro runs good data.
    }
    }while (ans!=="q"||ans !=="Q"||ans!==" m"||ans!=="M"|| ans!=="i"||ans! =="I");

    }

    char intro(char ans)
    {
    cout<<"What the program does, and select one of the 3 options (m,i,q)"<<endl;
    cin>> ans;
    return ans;
    }

    [/code]
    Last edited by Ganon11; Mar 1 '08, 03:51 PM. Reason: Fixing [CODE] tags.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    The errors messages say there are syntax errors on line 15, and 35,
    Here's a hint. It's very useful to tell us what those syntax errors are, as we don't have an inbuilt compiler in our heads...

    I see a mistake in ans!=="q" (in the while loop) and the similarly written not-equal comparisons. For one, the not equal operator is !=, not !==. Next, there is a difference in using ' ' and " " (single quotes vs. double quotes). As a more knowledgeable C++ programmer, you will eventually understand the implications of using a "".

    For now, understand this. Whenever you have a single character, it should be surrounded by '. Never by ".

    Comment

    Working...