How to make exception in for loop? - c++

The code below prints a box with the intergers the user inputs. I need to make it hollow to only display the full length of the first and last line of the box. like width = 5 height = 4
Example Output:
00000
0 0
0 0
00000
Source:
int main ()
{
int height;
int width;
int count;
int hcount;
string character;
cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;
for (hcount = 0; hcount < height; hcount++)
{
for (count = 0 ; count < width; count++)
cout << character;
cout << endl;
}
}
I do not know how to change the loop condition for the width to make it work.

I think you can test whether you are in the first or last row, and first or last column.
Example:
#include <string>
#include <iostream>
int main ()
{
using namespace std; // not recommended
int height;
int width;
string character;
cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;
for (int i = 0; i < height; i++)
{
// Test whether we are in first or last row
std::string interior_filler = " ";
if (i == 0 || i == height - 1)
{
interior_filler = character;
}
for (int j = 0; j < width; j++)
{
// Test whether are in first or last column
if (j == 0 || j == width -1)
{
cout << character;
} else {
cout << interior_filler;
}
}
// Row is complete.
cout << std::endl;
}
}
Here is the output:
$ ./a.out
input width
10
input height
7
input character
*
OUTPUT
**********
* *
* *
* *
* *
* *
**********

Add an if to the cout << character line. If we're not in the first row or column, output a space instead of the character.

Related

How to use "*" to make letters using rows and columns

Here is the code of which I'm writing to create the letter "Z" for example just cant figure out how to connect the rows and columns. Also don't know why its re-writing the code as many times by the user input. ex: user inputs 5 rows, it produces the "letter" 5 times over. if anyone is able to help me resolve this issue, it would be greatly appreciated!
#include <iostream>
#include <string>
#include <cmath>
#include <random>
#include <time.h>
using namespace std; //when compiler sees a name it does not recognize, assume std;
//Program 2 by anon
int main() {
cout<<"This program draws characters. Select character, then height(s)\n";
const string PROMPT{"how many rows tall? (0 to quit): "};
bool running{true};
while (running) {
cout << "\nOption: a)Z b)H /)/ \\)\\ q)quit? (q to quit): ";
char option{}; cin>>option;
if (option=='a') { // box
while (true) {
cout << "([z]) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int col{1}; col<=rows; ++col) { // for each column
for (int row{1}; row<=rows; ++row)
cout << string(rows-row, ' ') << "*\n";
cout << "*";
}
cout<<endl;
}
}
if (option=='b') { // forward slash
while (true) {
cout << "(H) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) {
cout << string(row-row, ' ') << "*\n";
for (int col{1}; col<=rows; ++col);
}
cout<<endl;
}
cout<<endl;
}
if (option=='/') { // forward slash
while (true) {
cout << "(/) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) {
cout << string(rows-row, ' ') << "*\n";
}
cout<<endl;
}
}
else if (option=='\\') { // back slash
while (true) {
cout << "(\\) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) { // backslash
cout << string(row-1, ' ') << "*\n";
}
cout<<endl;
}
}
else if (option=='q') {
break;
}
else {
cout<<" Invalid option, try again.\n";
}
}
cout<<"Goodbye\n";
return 0;
}
desired output:
a) Z b) H /)/ \\)\\ q) quit:a
How many rows tall? (0 to quit): 8
********
*
*
*
*
*
*
*
*
********
how many rows tall? (0 to quit): // loop reruns.
In order to draw the Z you need to add the horizontal "*" line at the beginning and the end. In addition you have to remove the outer loop, to avoid the letter to be drawn multiple times. This loop needs to be seperate at the beginning and the end, to draw the horizontal line:
if (option == 'a')
{ // box
while (true)
{
cout << "([z]) " << PROMPT;
int rows = 0;
cin >> rows;
if (rows <= 0) break;
for (unsigned int i = 0; i < rows; ++i)
cout << "*";
cout << endl;
for (int row{ 1 }; row <= rows; ++row)
cout << string(rows - row, ' ') << "*\n";
for (unsigned int i = 0; i < rows; ++i)
cout << "*";
cout << endl;
}
}

Print out the alphabet using for loop and setw

When I compile my program, run it and enter values I will get a rather strange and unexpected output:
So if I enter:
Enter width and height: 9 5
Enter characters: X O
1 XOXOXOXOX
2 OXOXOXOXO
3 XOXOXOXOX
4 OXOXOXOXO
5 XOXOXOXOX
A BCDEFGHI
When it's supposed to be:
Enter width and height: 9 5
Enter characters: X O
1 XOXOXOXOX
2 OXOXOXOXO
3 XOXOXOXOX
4 OXOXOXOXO
5 XOXOXOXOX
ABCDEFGHI
When do my void print_alphabet in another program it will work out just fine so I don't know the problem. I believe it has something to do with my other function but I can not seem to get it to work. Why does it act that way? Why does it print out A and then it does setw and prints out the rest?
This is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void print_chess_board (int const height,
int const width,
char const char_1,
char const char_2)
{
int index {};
for (int i = 1; i <= height; ++i)
{
if (i%2)
{
index = 0;
}
else
{
index = 1;
}
cout << left << setw(3) << i;
for (int j {}; j < width; ++j)
{
if (++index%2 == 0)
{
cout << char_2;
}
else
{
cout << char_1;
}
}
cout << endl;
}
}
void print_alphabet (int const width)
{
cout << setfill(' ') << setw(4);
for (int i {}; i < width; ++i)
{
cout << char('A' + i);
}
}
int main()
{
int width {};
int height {};
char char_1 {};
char char_2 {};
cout << "Enter width and height: ";
cin >> width >> height;
cout << "Enter characters: ";
cin >> char_1 >> char_2;
print_chess_board(height,width,char_1,char_2);
print_alphabet(width);
return 0;
}
You need to change
cout << setfill(' ') << setw(4);
to
cout << right << setfill(' ') << setw(4);

Moving elements in an array by certain amount

So I have a program that reads in a certain number of keys. An example would be
5 1 10 21 9 6 21 11 13 16 20
The text file example would be
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2
I want my program to start at Java and depending on first key, which in this case is 5, would move over 5 elements leading to "red". And so on and so on. However, I have managed to read in all the data and put them into arrays, I am just having trouble figuring out how to move the pointer in the array.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct pieces {
char word[5];
int jump;
} ;
// Main Function
int main ()
{
// declare variables
int wordCount[2];
int keyCount[2];
int numKeys, numKeys2;
int numWords;
int keyAmount = 1;
int wordAmount = 23;
int keyarr[11];
pieces cypher[wordAmount];
char filename[10];
ifstream inData;
int temp[10];
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
inData.open(filename);
if(inData.is_open());
{
// read in data
for ( numKeys = numWords = 0; numKeys < keyAmount; numKeys++){
// read in num of words and keys
inData >> wordCount[numKeys] >> keyCount[numKeys];
//read in words followed by jump key
for( numWords = 0; numWords < wordAmount; numWords++){
inData >> cypher[numWords].word >> cypher[numWords].jump;
}
// read in the actual keys
for( numKeys2 = 0; numKeys2 < wordCount[numKeys]; numKeys2++){
inData >> keyarr[numKeys2];
}
}
//print out data
for( int j = 0; j < numKeys; j++){
cout << wordCount[j] << "\n";
cout << keyCount[j] << "\n";
}
cout << "\n";
for ( int i = 0; i < wordAmount; ++i){
cout << cypher[i].word << " ";
cout << cypher[i].jump << " ";
}
cout << "\nKeys: " << "\n";
for(int k = 0; k < 11; k++){
cout << keyarr[k] << " ";
}
cout << "\n";
}
inData.close();
return 0;
}

How do I get for loop to repeat for input number of lines?

The program below will repeat the width, but I also want in to repeat for the height input too.
int main ()
{
int height;
int width;
int count;
int hcount;
string character;
cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;
for (hcount = 0; hcount < height; hcount++);
{
for (count = 0 ; count < width; count++)
cout << character;
cout << endl;
}
Do I need add another for loop? I cant figure out how to make it repeat. I've tried adding another for loop but changed the order of height and width:
for (count = count < width; count++)
{ (count = count < height; hcount++)
cout < character;
cout << endl;
But no luck.
A for loop operates on the single statement that follows it so
for (hcount = 0; hcount < height; hcount++);
is equivalent to
for (hcount = 0; hcount < height; hcount++)
;
so your first loop does nothing. If you remove the trailing semi-colon
for (hcount = 0; hcount < height; hcount++)
you'll get the nested loops you want.
Note also that
for (count = 0 ; count < width; count++)
cout << character;
cout << endl;
is actually
for (count = 0 ; count < width; count++)
cout << character;
cout << endl;
If you want both lines to execute for the inner loop, you need to place them inside braces {}
for (hcount = 0; hcount < height; hcount++) {
for (count = 0 ; count < width; count++) {
cout << character;
cout << endl;
}
}

Simple C++ Program with Multidimensional Array Errors?

When running the following code, I am attempting to update a Tic Tac Toe game board.
When you type in 3 as a column, it sets 2 X's or O's in the game board.
Here is an example of the output
* * *
* * *
* * *
X: Select a Row: 1
X: Select a Col: 3
* * X
X * *
* * *
Here is the desired output
* * *
* * *
* * *
X: Select a Row: 1
X: Select a Col: 3
* * X
* * *
* * *
Here is the code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int rowSelect = 0;
int colSelect = 0;
char turn = 'X';
char rowcol[2][2];
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
rowcol[i][j] = '*';
}
}
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
cout << rowcol[i][j] << " ";
}
cout << endl;
}
cout << endl;
while (true)
{
cout << turn << ": Select a Row: ";
cin >> rowSelect;
while (rowSelect < 1 || rowSelect > 3)
{
cout << "I cannot accept that value, try again!" << endl;
cout << endl;
cout << turn << ": Select a Row: ";
cin >> rowSelect;
}
cout << turn << ": Select a Col: ";
cin >> colSelect;
while (colSelect < 1 || colSelect > 3)
{
cout << "I cannot accept that value, try again!" << endl;
cout << endl;
cout << turn << ": Select a Col: " << endl;
cin >> colSelect;
}
rowcol[rowSelect-1][colSelect-1] = turn;
if (turn == 'X')
{
turn = 'O';
}
else
{
turn = 'X';
}
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
cout << rowcol[i][j] << " ";
}
cout << endl;
}
}
system("PAUSE");
return 0;
}
Thanks!
-Mike
The problem is the array. Although arrays are accessed using zero based indices, the definition requires the actual number of elements for which to reserve space.
You defined rowcol as:
char rowcol[2][2]; // This defines a 2 x 2 array
You should have defined rowcol as:
char rowcol[3][3]; // This defines a 3 x 3 array
Hope this helps!
Keith
Your rowcol array needs to be 3x3:
char rowcol[3][3];
char rowcol[2][2];
In all the cases, i, j must iterate only until < 2 since it is a 2x2 array.
Your array only holds 2 elements per row, while your loop runs through three rows and three columns. You seem to be confused on how arrays are numbered, an array with 2 elements would be accessed using elements[0] and elements[1], because 0 is the first number in programming(not 1). you need to declare an array of THREE elements, and access them using [0] [1] and [2].
FIX: change to char Array[3][3];