mercredi 28 octobre 2015

Can't compare string with while and if statements

I'm trying to create a command menu where the user will be able to perform as many commands as he/she wants until pressing "q" which will end the loop. I think I have everything I need to do this except I realized mid-way that my professor asked to use string. When I included string into the program, I began to get error messages saying "could not convert string to bool" wherever there was a while or if statement. What can I do to fix this problem and get my program working. Thanks in advance.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char option; 
    char number=0; 
    string s;
    string n;
    string p;
    string q;
    char number2; 

    cout << " Please enter a number: "<< endl; 
    cin >> number;

    do {

        cout << " Please enter a command: " << endl;
        cout << " s- square the number " << endl;
        cout << " n- add the number and (number +1) " << endl; 
        cout << " p- add the number and (number -1) " << endl; 
        cout << " q- quit" << endl; 
        cin >> option;

        if (option=s) {
            s= number*number;
            cout << "Square of this number is : " << s; 
        }
        else if ( option=n){
            number2= number+1;
            n= number+number2;
            cout << "Sum of" << number << "+" << number2 << "is: " << n;
        }
        else if (option=p) {
            number2= number-1;
            p= number+number2;
            cout << "Sum of" << number << "+" << number2 << "is" << p;
        }
        else if (option=q)
            cout << "Terminating Program";
    } while(option);
    return 0; 
}

Aucun commentaire:

Enregistrer un commentaire