If statements not executing when calling function

I don't understand why my if statements are not executing when calling "displayErrors" function. the output should be "Please complete:\n name\n phone\n address\n"

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <cstdlib>
using namespace std;

bool isOrderComplete(string s1, string s2, string s3);
void displayErrors (string s1, string s2, string s3);
void displayOrder (string s1, string s2, string s3);
void displayMainMenu ();
void displayCustomerInfoMenu ();
string getName ();
string getPhone();
string getAddress();

int main()
{
    string name = "";
    string phone = "";
    string address = "";
    string missingInfo = "";
    int choice;
    int subChoice;
    do
    {
        displayMainMenu();
        cin >> choice;
        switch (choice)
        {
        case 1:
            do
            {
                displayCustomerInfoMenu();
                cin >> subChoice;
                switch (subChoice)
                {
                case 1:
                    name = getName();
                    break;
                case 2:
                    phone = getPhone();
                    break;
                case 3:
                    address = getAddress();
                    break;
                case 4:
                    break;
                default:
                    cout << "\nNot a valid choice.\n"
                         << "Choose again.\n\n";
                }
            }while (subChoice != 4);
            break;
                case 2:
                    if (isOrderComplete (name, phone, address))
                        displayOrder(name, phone, address);
                    else
                        displayErrors(name, phone, address);
                    break;
                case 3:
                    break;
                default:
                    cout << "\nNot a valid choice.\n"
                         << "Choose again.\n\n";
        }
    }while (choice != 3);

    cout << "\nThank you.\n";
    system("PAUSE");

    return 0;
}
void displayMainMenu()
{
    cout << "\n1. Input Customer Information\n"
         << "2. Display Order Information\n"
         << "3. Exit\n"
         << "\nYour selection: ";
}
void displayCustomerInfoMenu()
{
    cout << "\n1. Name\n"
         << "2. Telephone Number\n"
         << "3. Address\n"
         << "4. Main Menu\n"
         << "\nYour selection: ";
}
void displayOrder (string s1, string s2, string s3)
{
    cout << "Name: " << s1 << endl;
    cout << "Phone: " << s2 << endl;
    cout << "Address: " << s3 << endl;
}
bool isOrderComplete (string name, string phone, string address)
{
    bool status1 = true;
    bool status2 = true;
    bool status3 = true;
    if (name == "")
        status1 = false;
    if (phone == "")
        status2 = false;
    if (address == "")
        status3 = false;
    return status1, status2, status3;
}
void displayErrors (string name, string phone, string address)
{
    string error1, error2, error3;
        cout << "\nPlease complete:\n";
        if(name == "")
            error1 = "Name\n";
        if(phone == "")
            error2 == "Phone\n";
        if(address == "")
            error3 == "Address\n";
        cout << error1;
        cout << error2;
        cout << error3;
}
string getName()
{
    string customerName;
    cout << "Enter name: ";
    cin >> customerName;
    return customerName;
}
string getPhone()
{
    string customerPhone;
    cout << "Enter telephone number: ";
    cin >> customerPhone;
    return customerPhone;
}
string getAddress()
{
    string customerAddress;
    cout << "Enter address: ";
    cin >> customerAddress;
    return customerAddress;

}
On line 112 and 114 you are using == (comparison) instead of = (assignment).


Line 103 doesn't do what you think. The comma operator only returns the value of the rightmost element. This means only the value of status3 is returned. If you want to return true if and only if all the status variables are true you should use the && operator instead.

 
return status1 && status2 && status3;

Note that the function can be simplified a lot by testing if the strings are empty directly inside the same expression without using all the bool variables and if statements.

 
return !name.empty() && !phone.empty() && !address.empty();
Last edited on
Oh youre totally right, I didn't even notice that. thanks man!
Topic archived. No new replies allowed.