You are here:

C++/Where's my logic gone wrong?


Hey Ralph, hope all is good,

First off I'd like to introduce myself to you. I'm 26, residing in Malaysia working for an airline office in the capital city.

I'm trying to code for my computer a short program that lets me view the seats I have booked in the top floor of the plane. Im new to hardcore programming, so I found a C++ book plus some websites and learned the basics till lets say about strings, loops, and a bit of arrays. The program is something like an output showing

1 A B  C D
2 A B  C D
3 A B  C D
4 A B  C D
5 A B  C D
6 A B  C D
7 A B  C D

and I marked desired seats with an "X".

I got the program to work, but I'm trying to set it as such that I can repeat the process, say I mark one seat with a "X" (which I've done), I can go back and mark another one.

I've added a loop in my program that does ask for repetition, but its logic is incorrect in such that it erases already marked spaces. Can you please look at my source code and tell me where my logic error is? Also, is there any way in C++ to prevent reentering the same marked seat? I'm relatively new to C++ so I apologize if my code is too "childish"
#include <iostream>
#include <string>
using namespace std;
int main ()
   int row;
   string letter;
   char letter2;
   int num = 1;
   int num2 = 2;
   int num3 = 3;
   int num4 = 4;
   int num5 = 5;
   int num6 = 6;
   int num7 = 7;

   string row1 = "1  A B  C D";
   string row2 = "2  A B  C D";
   string row3 = "3  A B  C D";
   string row4 = "4  A B  C D";
   string row5 = "5  A B  C D";
   string row6 = "6  A B  C D";
   string row7 = "7  A B  C D";
   cout << row1 << endl;
   cout << row2 << endl;
   cout << row3 << endl;
   cout << row4 << endl;
   cout << row5 << endl;
   cout << row6 << endl;
   cout << row7 << endl;

   cout << "Enter the row number where you would like to reserve a seat" << endl;
   cin >> row;
   cout << "Now enter the letter in that row where you would like to reserve a seat" << endl;
   cin >> letter;
   if (row == num && letter == "A")
   row1 = "1  X B  C D";
   else if (row == num && letter == "B")
   row1 = "1  A X  C D";
   else if (row == num && letter == "C")
   row1 = "1  A B  X D";
   else if (row == num && letter == "D")
   row1 = "1  A B  C X";
   if (row == num2 && letter == "A")
   row2 = "2  X B  C D";
   else if (row == num2 && letter == "B")
   row2 = "2  A X  C D";
   else if (row == num2 && letter == "C")
   row2 = "2  A B  X D";
   else if (row == num2 && letter == "D")
   row2 = "2  A B  C X";
   if (row == num3 && letter == "A" )
   row3 = "3  X B  C D";
   else if (row == num3 && letter == "B")
   row3 = "3  A X  C D";
   else if (row == num3 && letter == "C" )
   row3 = "3  A B  X D";
   else if (row == num3 && letter == "D")
   row3 = "3  A B  C X";
   if (row == num4 && letter == "A" )
   row4 = "4  X B  C D";
   else if (row == num4 && letter == "B" )
   row4 = "4  A X  C D";
   else if (row == num4 && letter == "C")
   row4 = "4  A B  X D";
   else if (row == num4 && letter == "D" )
   row4 = "4  A B  C X";
   if (row == num5 && letter == "A" )
   row5 = "5  X B  C D";
   else if (row == num5 && letter == "B")
   row5 = "5  A X  C D";
   else if (row == num5 && letter == "C" )
   row5 = "5  A B  X D";
   else if (row == num5 && letter == "D")
   row5 = "5  A B  C X";          
   if (row == num6 && letter == "A" )
   row6 = "6  X B  C D";
   else if (row == num6 && letter == "B")
   row6 = "6  A X  C D";
   else if (row == num6 && letter == "C")
   row6 = "6  A B  X D";
   else if (row == num6 && letter == "D")
   row6 = "6  A B  C X";
   if (row == num7 && letter == "A")
   row7 = "7  X B  C D";
   else if (row == num7 && letter == "B")
   row7 = "7  A X  C D";
   else if (row == num7 && letter == "C")
   row7 = "7  A B  X D";
   else if (row == num7 && letter == "D")
   row7 = "7  A B  C X";
   cout << "Updated Seating Arrangement" << endl;
   cout << row1 << endl;
   cout << row2 << endl;
   cout << row3 << endl;
   cout << row4 << endl;
   cout << row5 << endl;
   cout << row6 << endl;
   cout << row7 << endl;
   cout << endl;
   cout << "Would you wish to repeat? Y/N" << endl;
   cin >> letter2;
   if ( letter2 == 'y' || letter2 == 'Y')    
   cout << "End of program." << endl;

system ("pause");
return 0;

Thank you in advance for reading my question. I understand how you do not do student's homework for them and it is completely understandable if you would like verification of my identity and that this is personal work. I can provide you with evidence should you need it.


No, I'll help you. Even if you were a student and this is homework you have at least got some code and got it running and working to some extent. That is more than most such student questioners do. They usually just send me their assignment question verbatim and expect me to either provide a solution they can hand in as their own or provide help with some aspect of C++ or the question that I presume I am suppose to deduce telepathically!

OK your mistake is calling main again. This is a no no for two reasons.

The first reason applies specifically to main. It is the main entry point into your program and only the start up code that launches your program and sets up the C/C++ environment is allowed to call main, the C++ standard explicitly forbids calling main from within a program.

Secondly, you are already in main when you call it. This is not necessarily bad, you can call functions from themselves - it is a technique called recursion. However such uses have to be well thought out in advance and cannot be allowed to go on forever - there must be a way of breaking the sequence of calls.

Also making a function call is more than just jumping back to the start of the function. Each call has its own context. All local automatic variables (like those in your program) are allocated for each function call (note: function call, not each function) and are separate instances for each function call. Each time a function call returns it returns to the point where it was called. In your case the second call to main will return to just after the call to main in your code for the initial call to main. In fact this will not be problematic other than you going through the:

   cout << "End of program." << endl;
   system ("pause");

at the end of each call (so the more times you request another go the more times "End of program." and "Press any key to continue..." will be displayed with the wait for the key press).

Other than the fact the standard prevents you from calling main, your real problem is what main does. It does every thing. So calling it again does everything again (recursively as mentioned above). Each call gets its own set of row, num and other variables so it is like you re-ran you program from within your program.

What you need is a proper loop that only re-executes the parts of main you need. This is the bottom part after you have setup the variables and displayed the initial grid layout:

   cout << row6 << endl;

   cout << row7 << endl;
// Another go needs to only do stuff from here on down I think

   cout << "Enter the row number where you would like to reserve a seat" << endl;

   cin >> row;

There are several loop constructs in C++. The most basic is the while loop:

  while (condition_is_true)
  // repeat stuff

This loop is tested at the top of the repeated block hence it may not be executed at all if the condition tests false to start with. Conditions here are the same sort of expressions you would use in an if statement.

A variation on the while loop that will be useful here is the do..while loop:

   // repeat stuff
   while (condition_is_true);

In this case the loop is tested at the bottom so the repeated code is executed at least once, which is what you need here.

   cout << row6 << endl;

   cout << row7 << endl;

// Start looped section here
   // Repeat the following statements until the user
   // enters something other than 'y' or 'Y':

       cout << "Enter the row number where you "
         "would like to reserve a seat" << endl;

       cin >> row;


       cout << "Would you wish to repeat? Y/N" << endl;

       cin >> letter2;
   while ( letter2 == 'y' || letter2 == 'Y');

// End the loop if the entered letter is other than 'y' or 'Y'

   cout << "End of program." << endl;

   system ("pause");

   return 0;

Here are some more book suggestions and online links (some you might have already). Note that I do not expect you to obtain all these books at the same time - in fact some of them you should obtain when or if you feel up to the level of their content. I have tried to indicate in a basic way the level each book is pitched at. However I would try to find print version to look at before buying to see if you get on with the style and/or content, or at least see if there is a sample chapter available online, or at least a review or three!:

For absolute beginners there are a couple of books:

"You Can Do It - A Beginner's Introduction to Computer Programming" by Francis Glassborow and Roberta Allen.

And the second book in the series:

"You Can Program in C++" by Francis Glassborow

People often recommend the following if you already have some programming experience:

"Accelerated C++" by Koenig and Moo

although I have not read any of these.

There are many books on programming in general that are good. I found

"The Practice of Programming" by Kernighan and Pike

to be particularly useful.

For general good C++ usage you should look at

"Effective C++" "More Effective C++" and "Effective STL" by Scott Meyers

and the more advanced

"Exceptional C++" and "More Exceptional C++" and "Exceptional C++ Style" by Herb Sutter (See also
"Modern C++ Design" by Andrei Alexandrescu
"Ruminations on C++" by Koenig and Moo
"C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu

For C++ reference you should look at:

"The C++ Programming Language" 3rd or special Ed. by Bjarne Stroustrup

the C++ reference book from the man himself. You should also take a look at his book "The Design and Evolution of C++".

In addition you will most likely find that a couple of additional reference works will be of use. For the standard C++ library there is:

"The C++ Standard Library - a Tutorial and Reference" by Nicolai M. Josuttis - one of my most referred to books.

and for C++ templates there is:

"C++ Templates the Complete Guide" by David Vandevoorde and Nicolai M. Josuttis.
As to online resources the site of the ACCU, an organisation for professionalism in programming, at has many book reviews. It also has many resource links to web sites that may be of interest to you. If you become a member you have access to mailing lists and mentored projects (which are run from time to time) among other benefits.

There is a decent C++ FAQ at Posters of questions to the comp.lang.c++.moderated newsgroup are supposed to refer to this FAQ before they post a question as it answers a lot of the basic questions that are asked repeatedly.

There are various articles at They seem to have a C++ tutorial but I have not read it.

There is some online documentation for some parts of the C++ standard library at However it should be noted that some of this documentation concerns SGI specific non-standard extensions to the standard C++ library.

There are a few papers on C++ at

Finally I would like to point out that there are several encyclopaedia sites on the web. One of the best is Wikipedia at The article on the C++ programming language is at (oddly searching for "C++ programming" does not get you where you would think, its just titled "C++"). You will note that many of the articles have useful cross links to other Wikipedia articles and other web sites.

Hope this is of use. Have fun.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]