string compare problem

Being ask to code a seat selection program whereby whenever a seat is selected, it turns X.

1)At line 159 to 164, i use a string compare whereby if a user key in A1, variable num.seat1 will turn X, but why it didn't turn X??
2)At line 167, i try second method using "if" , but error pop out shows that multi-character constant?? What does it mean?? Anyway to fix it??
3)This code seems to be very long, more than 85 variable, anyway to shorten this code??

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<iostream>
#include<cctype>
#include<iomanip>
#include<cstdlib>
#include<string.h>

using namespace std;

int main() 
{
	char seats[5];
	int m,i;
	struct seat
	{
		wchar_t seat1,seat2,seat3,seat4,seat5,seat6,seat7,seat8,seat9,seat10
		,seat11,seat12,seat13,seat14,seat15,seat16,seat17,seat18,seat19,
		seat20,seat21,seat22,seat23,seat24,seat25,seat26,seat27,seat28,
		seat29,seat30,seat31,seat32,seat33,seat34,seat35,seat36,seat37,
		seat38,seat39,seat40,seat41,seat42,seat43,seat44,seat45,seat46,seat47,seat48,seat49,seat50,
		seat51,seat52,seat53,seat54,seat55,seat56,seat57,seat58,seat59,seat60,seat61,seat62,seat63,
		seat64,seat65,seat66,seat67,seat68,seat69,seat70,seat71,seat72,seat73,seat74,seat75,seat76,
		seat77,seat78,seat79,seat80,seat81,seat82,seat83,seat84;
		
	};
	
	seat num;
	num.seat1=1;
	num.seat2=2;
	num.seat3=3;
	num.seat4=4;
	num.seat5=5;
	num.seat6=6;
	num.seat7=1;
	num.seat8=2;
	num.seat9=3;
	num.seat10=4;
	num.seat11=5;
	num.seat12=6;
	num.seat13=1;
	num.seat14=2;
	num.seat15=3;
	num.seat16=4;
	num.seat17=5;
	num.seat18=6;
	num.seat19=1;
	num.seat20=2;
	num.seat21=3;
	num.seat22=4;
	num.seat23=5;
	num.seat24=6;
	num.seat25=1;
	num.seat26=2;
	num.seat27=3;
	num.seat28=4;
	num.seat29=5;
	num.seat30=6;
	num.seat31=1;
	num.seat32=2;
	num.seat33=3;
	num.seat34=4;
	num.seat35=5;
	num.seat36=6;
	num.seat37=1;
	num.seat38=2;
	num.seat39=3;
	num.seat40=4;
	num.seat41=5;
	num.seat42=6;
	num.seat43=1;
	num.seat44=2;
	num.seat45=3;
	num.seat46=4;
	num.seat47=5;
	num.seat48=6;
	num.seat49=1;
	num.seat50=2;
	num.seat51=3;
	num.seat52=4;
	num.seat53=5;
	num.seat54=6;
	num.seat55=1;
	num.seat56=2;
	num.seat57=3;
	num.seat58=4;
	num.seat59=5;
	num.seat60=6;
	num.seat61=1;
	num.seat62=2;
	num.seat63=3;
	num.seat64=4;
	num.seat65=5;
	num.seat66=6;
	num.seat67=1;
	num.seat68=2;
	num.seat69=3;
	num.seat70=4;
	num.seat71=5;
	num.seat72=6;
	num.seat73=1;
	num.seat74=2;
	num.seat75=3;
	num.seat76=4;
	num.seat77=5;
	num.seat78=6;
	num.seat79=1;
	num.seat80=2;
	num.seat81=3;
	num.seat82=4;
	num.seat83=5;
	num.seat84=6;

	
				cout << "  --------------------------------"<<endl;
				
                cout << "  \t~C O C K P I T~\t\t"<<endl;
                
                cout << "  --------------------------------"<<endl;
                
                cout << "A:_"<<num.seat1<<"_::_"<<num.seat2<<"_::_"<<num.seat3<<"_::   ::_"<<num.seat4<<"_::_"<<num.seat5<<"_::_"<<num.seat6<<"_:"<<endl;
                
                cout << "B:_"<<num.seat7<<"_::_"<<num.seat8<<"_::_"<<num.seat9<<"_::   ::_"<<num.seat10<<"_::_"<<num.seat11<<"_::_"<<num.seat12<<"_:"<<endl;
                
                cout << "C:_"<<num.seat13<<"_::_"<<num.seat14<<"_::_"<<num.seat15<<"_::   ::_"<<num.seat16<<"_::_"<<num.seat17<<"_::_"<<num.seat18<<"_:"<<endl;
                
                cout << "D:_"<<num.seat19<<"_::_"<<num.seat20<<"_::_"<<num.seat21<<"_::   ::_"<<num.seat22<<"_::_"<<num.seat23<<"_::_"<<num.seat24<<"_:"<<endl;
                
                cout << "E:_"<<num.seat25<<"_::_"<<num.seat26<<"_::_"<<num.seat27<<"_::   ::_"<<num.seat28<<"_::_"<<num.seat29<<"_::_"<<num.seat30<<"_:"<<endl;
                
                cout << "F:_"<<num.seat31<<"_::_"<<num.seat32<<"_::_"<<num.seat33<<"_::   ::_"<<num.seat34<<"_::_"<<num.seat35<<"_::_"<<num.seat36<<"_:"<<endl;
                
                cout << " :                                 :"<<endl;
                
                cout << "G:_"<<num.seat37<<"_::_"<<num.seat38<<"_::_"<<num.seat39<<"_::   ::_"<<num.seat40<<"_::_"<<num.seat41<<"_::_"<<num.seat42<<"_:"<<endl;
                
                cout << "H:_"<<num.seat43<<"_::_"<<num.seat44<<"_::_"<<num.seat45<<"_::   ::_"<<num.seat46<<"_::_"<<num.seat47<<"_::_"<<num.seat48<<"_:"<<endl;
                
                cout << "I:_"<<num.seat49<<"_::_"<<num.seat50<<"_::_"<<num.seat51<<"_::   ::_"<<num.seat52<<"_::_"<<num.seat53<<"_::_"<<num.seat54<<"_:"<<endl;
                
                cout << "J:_"<<num.seat55<<"_::_"<<num.seat56<<"_::_"<<num.seat57<<"_::   ::_"<<num.seat58<<"_::_"<<num.seat59<<"_::_"<<num.seat60<<"_:"<<endl;
                
                cout << "K:_"<<num.seat61<<"_::_"<<num.seat62<<"_::_"<<num.seat63<<"_::   ::_"<<num.seat64<<"_::_"<<num.seat65<<"_::_"<<num.seat66<<"_:"<<endl;
                
                cout << "L:_"<<num.seat67<<"_::_"<<num.seat68<<"_::_"<<num.seat69<<"_::   ::_"<<num.seat70<<"_::_"<<num.seat71<<"_::_"<<num.seat72<<"_:"<<endl;
                
                cout << "M:_"<<num.seat73<<"_::_"<<num.seat74<<"_::_"<<num.seat75<<"_::   ::_"<<num.seat76<<"_::_"<<num.seat77<<"_::_"<<num.seat78<<"_:"<<endl;
                
                cout << "N:_"<<num.seat79<<"_::_"<<num.seat80<<"_::_"<<num.seat81<<"_::   ::_"<<num.seat82<<"_::_"<<num.seat83<<"_::_"<<num.seat84<<"_:"<<endl;
                
                cout << "Enter Your Seat Choice:";
                cin >> seats;
                cout << endl;
                
                 for(int i=0 ; i<100 ; i++)
         		{
            		seats[i] = toupper(seats[i]);
        	    }

             	if(seats!='\0')
     			 {
				  	m=strcmp("A1",seats);
              	 	if(m==0)
                	{
                                   num.seat1='X';
                	}
                 }
			 
	           if(seats=='A1')  
			   {
			     num.seat1='X';
			   }	
			    
			
				cout << num.seat1;
	system("pause");
	return 0;
}
Well, this program is in desperate need of arrays and loops.

The 84 seats could be represented by a single dimension array
 
    char seats[84];

or perhaps a 2-D array
 
    char seats[14][6];


The user input could be done like this:
1
2
3
4
    cout << "Enter seat number e.g. F5:  "; 
    char letter = ' ';
    int  number = 0;
    cin >> letter >> number;

(Or perhaps get the input as a string and use a stringstream to get the letter, number parts)
Last edited on
I treated this question as a challenge and have rewritten the code to take as little lines as possible, while being completely functional and provide fool-proof 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
#include <iostream>
int main()
{
    char seats[84];
    for(int i = 0; i < 84; i++)
    {
        seats[i] = i%6 + 1 + '0';
    }
    while (true)
    {
        std::cout << "  --------------------------------"<< std::endl;
        std::cout << "\t   ~C O C K P I T~" << std::endl;
        std::cout << "  --------------------------------"<< std::endl;
        char row = 'A';
        for(int i = 0; i < 84; i++)
        {
            if(i%6 == 0) std::cout << row++;
            std::cout << ":_" << seats[i] << "_:";
            if(i%6 == 2) std::cout << ":   :";
            if(i%6 == 5)
            {
                std::cout << std::endl;
                if(row == 'G') // You had 'F' but I go for symetry
                std::cout << " :                                 :"<< std::endl;
            }
        }
        int seat;
        std::cout << "Enter Your Seat Choice: ";
        std::cin >> row >> seat;
        std::cin.clear();
        std::cin.ignore();
        row = toupper(row) - 'A';
        if(row < 0 || row > 13 || seat < 1 || seat > 6)
        {
            std::cout << "\tNO SUCH SEAT!" << std::endl << std::endl;
            continue;
        }
        int seatIndex = (row * 6) + (seat - 1);
        seats[seatIndex] = 'X';
    }
}
Last edited on
@JockX
Oh man, Thank you very much :D...i love you so much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
for(int i = 0; i < 84; i++)
        {
            if(i%6 == 0)

                std::cout << row++;
                std::cout << ":_" << seats[i] << "_:";

            if(i%6 == 2)

                 std::cout << ":   :";


            if(i%6 == 5)
            {
                 std::cout << std::endl;
                if(row == 'G') // You had 'F' but I go for symetry
                 std::cout << " :                                 :"<<  std::endl;
            }
        }




i was taught by lecturer that
every single if decision construct has a {} but why yours doesn't have it for the first and the second one?? (correct me if i'm wrong)

and i not really understand this part, could you explain it for me ? Thank you :D...

Besides, i found that there is another fool-proof that what if a user input a seat that is same with the previous one?? like user A input A1, user B input A1 again??

i try to insert another code whereby in the bold part, the end section there,
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
#include <iostream>
int main()
{
    char seats[84];
    for(int i = 0; i < 84; i++)
    {
        seats[i] = i%6 + 1 + '0';
    }
    while (true)
    {
        std::cout << "  --------------------------------"<< std::endl;
        std::cout << "\t   ~C O C K P I T~" << std::endl;
        std::cout << "  --------------------------------"<< std::endl;
        char row = 'A';
        for(int i = 0; i < 84; i++)
        {
            if(i%6 == 0) std::cout << row++;
            std::cout << ":_" << seats[i] << "_:";
            if(i%6 == 2) std::cout << ":   :";
            if(i%6 == 5)
            {
                std::cout << std::endl;
                if(row == 'G') // You had 'F' but I go for symetry
                std::cout << " :                                 :"<< std::endl;
            }
        }
        int seat;
        std::cout << "Enter Your Seat Choice: ";
        std::cin >> row >> seat;
        std::cin.clear();
        std::cin.ignore();
        row = toupper(row) - 'A';
        if(row < 0 || row > 13 || seat < 1 || seat > 6)
        {
            std::cout << "\tNO SUCH SEAT!" << std::endl << std::endl;
            continue;
        }
        int seatIndex = (row * 6) + (seat - 1);
        seats[seatIndex] = 'X';
       
           if(seatIndex == 'X')
         {
            std::cout << "\tSPATA MEN!!" <<  std::endl << std::endl;
             continue;
          }
    }
}
Last edited on
eddytan95 wrote:
every single if decision construct has a {} but why yours doesn't have it for the first and the second one??

All contructions such as if, else, while, for or do require you to put one statement in next line. But if you need more than line of code, you put them inside {} to make a compound statement. I didn't need to, so I skipped them, and saved 2 lines of space;D.

Before you skip {} think about what happens (best never skip them). You indented my code incorrectly: the line 12 will execute only if line 10 is true, but line 13 is not part of compound statement (there is no {}), so it will execute every time (numbers of lines from your post, not mine below):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(int i = 0; i < 84; i++) // I loop through all available seats (84 times)
{
    if(i%6 == 0)
        cout << row++; // this happens only every six seats - start a new row with next letter

    cout << ":_" << seats[i] << "_:"; // This happens for each seat - draw :_number_:

    if(i%6 == 2)
        cout << ":   :"; // this happens every six rows - draws the middle gap

    if(i%6 == 5)  // this happens at the end of each row
    {
        cout << endl; // move cursor to new line
        if(row == 'G') // and if this is end of row G, adds empty one
            cout << " :                                 :"<< endl;
    }
}

And what magic I used - you better learn it, because it is crutial:
i%6 - this operation is modulo, or remainder from division od i by six. So it allows me to do some action every six seats, because the remainder from division by six can only be 1,2,3,4,5 or 0.
So
if (i%6 == 0) I know the seat that is now being processed is first in any row, so I add row letter
if (i%6 == 2) I know I am processing third seat in any row, and after third seat I add gap
if (i%6 == 5) I know I am processing last seat in any row, so it's time to add endl.

Using this trick you may just change number of seats in one place, and the same loop will be able to draw any number arranged in rows of six.

Also, think about what happens in these lines and why:
1
2
3
4
5
seats[i] = i%6 + 1 + '0'; 
// ...
row = toupper(row) - 'A';
// ...
int seatIndex = (row * 6) + (seat - 1);


This won't work:
1
2
3
4
5
6
7
8
9
10
seats[seatIndex] = 'X';
       
if(seatIndex == 'X') // seatIndex is a number, not char.
// You need to check if seats[seatIndex] == 'X'.
// But not here! You have just set seats[seatIndex] to X above, 
// so this would be true every time! Use if-else here.
{
    std::cout << "\tSPATA MEN!!" <<  std::endl << std::endl;
    continue; 
}
Last edited on
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
#include <iostream>
int main()
{
    char seats[84];
    for(int i = 0; i < 84; i++)
    {
        seats[i] = i%6 + 1 + '0';
    }
    while (true)
    {
        std::cout << "  --------------------------------"<< std::endl;
        std::cout << "\t   ~C O C K P I T~" << std::endl;
        std::cout << "  --------------------------------"<< std::endl;
        char row = 'A';
        for(int i = 0; i < 84; i++)
        {
            if(i%6 == 0) std::cout << row++;
            std::cout << ":_" << seats[i] << "_:";
            if(i%6 == 2) std::cout << ":   :";
            if(i%6 == 5)
            {
                std::cout << std::endl;
                if(row == 'G') // You had 'F' but I go for symetry
                std::cout << " :                                 :"<< std::endl;
            }
        }
        int seat;
        std::cout << "Enter Your Seat Choice: ";
        std::cin >> row >> seat;
        std::cin.clear();
        std::cin.ignore();
                     
                             if (seats[seatIndex] == 'X')
                            {
                               std::cout << "SEAT IS TAKEN!";
                                std::cin >> row >> seat;
                                std::cin.clear();
                                std::cin.ignore();
                            }



        row = toupper(row) - 'A';
        if(row < 0 || row > 13 || seat < 1 || seat > 6)
        {
            std::cout << "\tNO SUCH SEAT!" << std::endl << std::endl;
            continue;
        }
        int seatIndex = (row * 6) + (seat - 1);
        seats[seatIndex] = 'X';
       
    }
}


i try to put it there but my program is either hangs or the output shows like this,


example i input 4 times,
A1 (seat A1 become X)
A1(SEAT IS TAKEN!)
A2(SEAT IS TAKEN!)
A3(SEAT IS TAKEN!)

or

A1(seat A1 become X)
A2(SEAT IS TAKEN!)
A3(SEAT IS TAKEN!)
A4(SEAT IS TAKEN!)

whenever second time input, it shows seat is taken. How to fix it??
Do it in this order:
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
// I declare 'seat' now. 'row' has been already declared before.
int seat; 
std::cout << "Enter Your Seat Choice: ";

// First character user inputs goes to 'row'.
// The rest - if are a numbers, go to 'seat'.
std::cin >> row >> seat; 
    
// If user messed up and entered two (or more) letters we need to reset
// our variables (delete these lines, and try to pick seat number 'ee',
// and you'll see why this is needed).
std::cin.clear();   
std::cin.ignore();

// Change row letter into row number (expected numbers are between 0-13)
row = toupper(row) - 'A'; 

// Prevent the user from using non-existent seat:
//    If number of row is not 0-13 or seat is not 1-6
//    signal that there is no such seat, 
//    and go back to the top of the while loop
if(row < 0 || row > 13 || seat < 1 || seat > 6)
{
    std::cout << "\tNO SUCH SEAT!" << std::endl << std::endl;
    continue; // starts over entire loop. 
}

// calculate which seat index selected row and seat correspond to:
int seatIndex = (row * 6) + (seat - 1); // So A1=0, A2=1, B1=6, etc

// Prevent the user from using taken seat:
//    If seat is taken (already marked with X),
//    signal the error to the user, 
//    and go back to the top of while loop
if (seats[seatIndex] == 'X')
{
    std::cout << "\tSEAT IS TAKEN!";  // I think with /t it looks better ;)
    continue; // go to the top and prevent executing the next line.

    std::cin >> row >> seat; // This is not needed. User will be asked to
    std::cin.clear();        // enter another seat in the next go through 
    std::cin.ignore();       // the loop anyway, in line 3 of this snippet
}

// If you think of any more fail-safes about user input 
// create another if here, before executing next line.

// Finally, if nothing bad happened, this will be safely executed:
seats[seatIndex] = 'X';
Last edited on
but still, if i using a loop, the second time input for the seat i gonna get (SEAT IS TAKEN!), why is that so?

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
#include <iostream>
int main()
{
    char seats[84];
    int seatIndex;
    for(int i = 0; i < 84; i++)
    {
        seats[i] = i%6 + 1 + '0';
    }
    while (true)
    {
        std::cout << "  --------------------------------"<< std::endl;
        std::cout << "\t   ~C O C K P I T~" << std::endl;
        std::cout << "  --------------------------------"<< std::endl;
        char row = 'A';
        for(int i = 0; i < 84; i++)
        {
            if(i%6 == 0) std::cout << row++;
            std::cout << ":_" << seats[i] << "_:";
            if(i%6 == 2) std::cout << ":   :";
            if(i%6 == 5)
            {
                std::cout << std::endl;
                if(row == 'G') // You had 'F' but I go for symetry
                std::cout << " :                                 :"<< std::endl;
            }
        }
        int seat;
        std::cout << "Enter Your Seat Choice: ";
        std::cin >> row >> seat;
        std::cin.clear();
        std::cin.ignore();
                     
                             if (seats[seatIndex] == 'X')
                            {
                               std::cout << "SEAT IS TAKEN!";
                                std::cin >> row >> seat;
                                std::cin.clear();
                                std::cin.ignore();
                            }



        row = toupper(row) - 'A';
        if(row < 0 || row > 13 || seat < 1 || seat > 6)
        {
            std::cout << "\tNO SUCH SEAT!" << std::endl << std::endl;
            continue;
        }
        int seatIndex = (row * 6) + (seat - 1);
        
        if (seats[seatIndex] == 'X')
		{
    		std::cout << "\tSEAT IS TAKEN!";  
    		continue; 

    		std::cin >> row >> seat; 
    	   
		}
        seats[seatIndex] = 'X';
       
    }
}
The "SEAT IS TAKEN" message can be issued at two different points in the program, lines 36 and 54. I don't think the code is required at both places, is it?

The variable seatIndex is defined in two different places, at lines 5 and 50. I think line 5 should be deleted and the entire block which uses that variable from line 34 to 40 should also be deleted.
Topic archived. No new replies allowed.