Help me fix this program please!

It is supposed to output permissions based off of a binary or sinlge digit code they enter.
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int ipt1, ipt2, ipt3, prms1, prms2, prms3, opt1, opt2, opt3;
string none, r, w, x, rw, wx, rwk;


cin >> ipt1;
cin >> ipt2;
cin >> ipt3;




if (ipt1 = 1)
{
prms1 = 'x';
opt1 = 001;
}
else if (ipt1 = 2)
{
prms1 = 'w';
opt1 = 010;
}
else if (ipt1 = 3)
{

prms1= 'wx';
opt1 = 011;
}
else if (ipt1 = 4)
{
prms1 = 'r';
opt1 = 100;
}
else if (ipt1 = 5)
{
prms1 = 'rx';
opt1= 101;
}
else if (ipt1 = 6)
{
prms1 ='rw';
opt1 = 110;
}
else if (ipt1 = 7)
{
prms1 = 'rwk';
opt1 = 111;
}
else if (ipt1 = 0)
{
prms1 = 'none';
opt1 = 000;
}
if (ipt2 = 1)
{
prms2 = 'x';
opt2 = 001;
}
else if (ipt2 = 2)
{
prms2 = 'w';
opt2 = 010;
}
else if (ipt2 = 3)
{
prms2= 'wx';
opt2 = 011;
}
else if (ipt2 = 4)
{
prms2 = 'r';
opt2 = 100;
}
else if (ipt2 = 5)
{
prms2 = 'rx';
opt2= 101;
}
else if (ipt2 = 6)
{
prms2 ='rw';
opt2 = 110;
}
else if (ipt2 = 7)
{
prms2 = 'rwk';
opt2 = 111;
}
else if (ipt2 = 0)
{
prms2 = 'none';
opt2 = 000;
}
if (ipt3 = 1)
{
prms3 = 'x';
opt3 = 001;
}
else if (ipt3 = 2)
{
prms3 = 'w';
opt3 = 010;
}
else if (ipt3 = 3)
{
prms3= 'wx';
opt3 = 011;
}
else if (ipt3 = 4)
{
prms3 = 'r';
opt3 = 100;
}
else if (ipt3 = 5)
{
prms3= 'rx';
opt3= 101;
}
else if (ipt3 = 6)
{
prms3 ='rw';
opt3 = 110;
}
else if (ipt3 = 7)
{
prms3 = 'rwk';
opt3 = 111;
}
else if (ipt3 = 0)
{
prms3 = 'none';
opt3 = 000;
}
if (ipt1= 001)
{
prms1 = 'x';
opt1= 1;
}
else if (ipt1 = 010)
{
prms1 = 'w';
opt1 = 2;
}
else if (ipt1 = 011)
{
prms1= 'wx';
opt1 = 3;
}
else if (ipt1 = 100)
{
prms1 = 'r';
opt1 = 4;
}
else if (ipt1 = 101)
{
prms1 = 'rx';
opt1= 5;
}
else if (ipt1 = 110)
{
prms1 ='rw';
opt1 = 6;
}
else if (ipt1 = 111)
{
prms1 = 'rwk';
opt1 = 7;
}
else if (ipt1 = 000)
{
prms1 = 'none';
opt1 = 0;
}
if (ipt2 = 001)
{
prms2 = 'x';
opt2 = 1;
}
else if (ipt2= 010)
{
prms2 = 'w';
opt2 = 2;
}
else if (ipt2 = 011)
{
prms2= 'wx';
opt2 = 3;
}
else if (ipt2 = 100)
{
prms2 = 'r';
opt2 = 4;
}
else if (ipt2 = 101)
{
prms2 = 'rx';
opt2= 5;
}
else if (ipt2 = 110)
{
prms2 ='rw';
opt2 = 6;
}
else if (ipt2 = 111)
{
prms2 = 'rwk';
opt2 = 7;
}
else if (ipt2 = 000)
{
prms2 = 'none';
opt2 = 0;
}
if (ipt3 = 001)
{
prms3 = 'x';
opt3 = 1;
}
else if (ipt3 = 010)
{
prms3 = 'w';
opt3 = 2;
}
else if (ipt3 = 011)
{
prms3= 'wx';
opt3 = 3;
}
else if (ipt3 = 100)
{
prms3 = 'r';
opt3 = 4 ;
}
else if (ipt3 = 101)
{
prms3 = 'rx';
opt3= 5;
}
else if (ipt3 = 110)
{
prms3 ='rw';
opt3 = 6;
}
else if (ipt3 = 111)
{
prms3 = 'rwk';
opt3 = 7;
}
else if (ipt3 = 000)
{
prms3 = 'none';
opt3 = 0;
}


cout << prms1 << endl;
cout << prms2 << endl;
cout << prms3 << endl;





}


in each condition, use "==" for equal test
"=" means assignment
VERY VERY IMPORTANT!!!: 010 is the number 8! That's because when you start a number with 0, you tell the compiler that the number is in base 8!!

Also, change your single quotes to double quotes. For example: 'rw' should be "rw".

Do you know about binary operators? This could be done in about 10 lines of blazing fast code with them.

Okay, looking at what you have,

Any time you see lots of repeated code you should think to yourself "Can I make this a function?" For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string getPrm(int ipt)
{
    string result;
    if (ipt == 1) {
       result = "x";
    } else if (ipt == 2) {
        result = "w";
    etc.
    return result;
}

int getOpt(int ipt)
{
    int result = 0;
    if (ipt == 1) {
        opt1 = 001;
    } else if (ipt1 = 2) {
        opt1 = 010;
    etc.
    return result;
}


Then in main you can do:
1
2
3
4
5
6
prms1 = getPerm(ipt1);
prms2 = getPerm(ipt2);
prms3 = getPerm(ipt3);
opt1 = getOpt(ipt1);
opt2 = getOpt(ipt2);
opt3 = getOpt(ipt3);



Topic archived. No new replies allowed.