Help with strings

I don't know why this code isn't working. It only returns T4741 no matter what I enter.

EDIT: This is the original problem

PROGRAM 2

The office furniture company in Program 1 wants another program to generate inventory code. They sell office chairs and tables in three colors. All inventory codes consist of 5 characters. If the product is a chair, the first three characters are “C47”. If the product is a table, the first three characters are “T47”. The last two characters are used to code color: “41” for red, “25” for black” and “30” for green. Write a program to do the following. First ask the user to enter type of product (e.g. “Enter 1 for table or 2 for chair”). Then ask the user to enter color (e.g. “Enter 1 for red, 2 for black or 3 for green). Generate and display the inventory code based on the information entered by the user. For example, if the user chooses chair and red, the inventory code generated by the program should be “C4741”. If the user enters wrong type or wrong color, display “Invalid input”.



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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Problem 2
// Created By: Tucker Peebles on 12/4/12

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string first = "";
    string last = "";
    const string invalid = "Invalid input";
    int type = 0;
    int color = 0;
    
    cout << "Enter 1 for a table or 2 for a chair: " <<endl;
    cin >> type;
    
    if (type = 1)
    {
         first = "T47";
    }
    else if (type = 2)
    {
          first = "C47";
    }
    else 
    {
         cout << invalid <<endl;
    }
    
    cout << "Enter 1 for red, 2 for black, or 3 for green: " <<endl;
    cin >> color;
    
    if (color = 1)
    {
              last = "41";
    }
    else if (color = 2)
    {
         last = "25";
    }
    else if (color = 3)
    {
         last = "30";
    }
    else
    {
        cout << invalid <<endl;
    }
    
    cout << "Inventory code: " << first + last <<endl;
    
    system("pause");
    return 0;
}
    
Last edited on
if (type = 1)

When checking for equality you must use ==, not =. Otherwise it will assign 1 to "type" and return true.
Last edited on
thanks
Topic archived. No new replies allowed.