Restricting string length and issues with string not being outputted

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Car
{
 public:
        void setUp(string, int, string, bool, string);
        void output();
private:
    string reportingMark;
    int carNumber;
    string kind;
    bool loaded;
    string destination;
};
void input (Car *ptr);
int main()
{
    Car *ptrCar = new Car;
    string reportingMark = " ";
    int carNumber=0;
    string kind ="business";
    bool loaded= true;
    string destination =" ";
    Car *ptr = new Car;
    input(ptr);
    ptr->setUp(reportingMark, carNumber, kind, loaded, destination);
    ptr->output();


}
void input (Car *ptr)
{

    string reportingMark;
    int carNumber;
    string kind;
    bool loaded;
    string destination;

      cout << "Please input your AAR reporting mark" << endl;
        cin >> reportingMark;
    do
    {
        if (reportingMark.length() <2 || reportingMark.length() >4);
        {
            cout << "Invalid. Please try again."<< endl;
            cout << reportingMark.length();
            cin >> reportingMark;
        }
    }while(reportingMark.length() >= 2 || reportingMark.length() <= 4);
    
    cout<< reportingMark << endl;
    cout<< "Please input your car number." << endl;
    cin >> carNumber;
    cout << carNumber<<endl;
    cout << "What kind of car is it?" << endl;
    cin.ignore();
    getline(cin,kind);
    cout << kind << endl;
    cout <<"Is your car loaded? (1 - yes or 0 - no)" <<endl;
    cin >> loaded;
    cout << loaded << endl;

    if(loaded == 0)
    {
        cout << "Do you have a destination? If so, where? If not, type NONE" << endl;
        cin.ignore();
        getline(cin,destination);
    }else if (loaded == 1)
    {
    cout << "Where is your destination?" << endl;
    cin.ignore();
    getline(cin,destination);
    cout << destination << endl;
    }





}
void Car::setUp(string rMark, int cNumber, string cKind, bool cLoaded, string dest)
{
   reportingMark = rMark;
   carNumber = cNumber;
   kind = cKind;
   loaded = cLoaded;
   destination = dest;

}
void Car::output()
{
    cout << "AAR Reporting Mark:" <<  reportingMark << endl;
    cout << "Car Number:" << carNumber << endl;
    cout << "Kind:" << kind << endl;
    cout << "Your car is:" << loaded << endl;
    cout << "Destination:" << destination << endl;
}


What I'm struggling with is specifically that my lab asks for
A string named reportingMark to contain two to four characters

Every input I enter keeps giving me the invalid option when the number of characters in the string isn't 2-4. Even when I try inputs of 2-4 characters. My other issue is "destination" The input I give isn't outputting my input correctly, it just appears to what I have in int main which is blank space.
Last edited on
Every input I enter keeps giving me the invalid option when the number of characters in the string isn't 2-4. Even when I try inputs of 2-4 characters.


Line 47:
if (reportingMark.length() <2 || reportingMark.length() >4); // <- you have a semicolon here

That semicolon is terminating your if statement, which effectively nullifies this entire line and makes the following code block happen always.


The input I give isn't outputting my input correctly, it just appears to what I have in int main which is blank space.


This is a side-effect of mixing the >> operator with getline(). <short rant>One of the many poor design decisions of iostream.</short rant>

See this thread for details/solutions:
http://www.cplusplus.com/forum/beginner/128692/#msg695385
Topic archived. No new replies allowed.