Program 10

This program is so ridiculously long, I wanted to know if there is shorter way to make this program work. Also, the program was over 30000 characters long so what's missing doesn't really matter I just want to know if this part of the program can be shortened. Btw don't mind the "gotoxy" or "txtcolor" it's part of a header file "iocolors" it's made to make the program look nice.

#include <iostream.h>
#include <conio.h>
#include <apstring.cpp>
#include <iocolors.h>




numbus = numbus++;
gotoxy (1, 1);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 2);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 3);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 4);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 5);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 6);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
break;
case 7:
numbus = numbus++;
gotoxy (1, 1);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 2);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 3);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 4);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 5);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 6);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 7);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
break;
case 8:
numbus = numbus++;
gotoxy (1, 1);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 2);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 3);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 4);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 5);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 6);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 7);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 8);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
break;
case 9:
numbus = numbus++;
gotoxy (1, 1);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 2);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 3);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 4);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 5);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 6);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 7);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 8);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 9);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
break;
case 10:
numbus = numbus++;
gotoxy (1, 1);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 2);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 3);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 4);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 5);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 6);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 7);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 8);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 9);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
numbus = numbus++;
gotoxy (1, 10);
cout << "Bus " << numbus;
cout << " is assigned to " << groupname << ".";
break;

getch ();


cin.ignore (80, '\n');
runProgram = runProgram - 1;
}
while (runProgram >= 1);


}
Most of what you have there could be replaced by a simple loop, something like this:
1
2
3
4
5
6
    for (int row=1; row<=n; row++)
    {
        gotoxy (1, row);
        cout << "Bus " << ++numbus 
             << " is assigned to " << groupname << ".";
    }


n in my code is whatever variable you use in the switch-case

Or of you want to really reduce the number of lines (but probably less readable), the entire switch-case block of over 240 lines or more than 6000 characters of text could be replaced with just two lines:
1
2
    for (int row=1; row<=n; row++)
        gotoxy (1, row), cout << "Bus " << ++numbus << " is assigned to " << groupname << ".";

Last edited on
How would I put that code into my program without getting any errors?
Nevermind, Thanks so much! You have saved me from tons of lines of code.
Glad that was useful. It did occur to me based in this part, that there may be other parts of your code which could be shortened too.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.