Print out the alphabet using for loop and setw - c++

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);

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 a text in the middle of a rectangle

I want to print out a rectangle and in the middle I want to output a preferred text.
So In the terminal it should look like:
Enter width and height: 5 6
+-----+
| |
| |
| |
| |
| |
+-----+
Then in the middle of the rectangle (height/2) it should print out "Hey". It doesn't fit in this example because the format on reddit is a bit different than my compiler.
However I wonder how I could make this happen? I can create the rectangle but I cannot seem to create a "when" statement, as in "when mid occurs, print out "Hey".
I need help finding a way to write such statement.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void print_row (int const width)
{
cout << '+' << '-';
for (int i = 1; i < width; ++i)
{
cout << '-';
}
cout << '+' << endl;
}
void print_rectangle (int const width, int const height)
{
int mid = height/2;
print_row(width);
for (int i {}; i < height; ++i)
{
cout << '|' << setw(width+1) << '|' << endl;
if (mid)
{
cout << "Hey" << endl;
}
}
print_row(width);
}
int main()
{
int width {};
int height {};
cout << "Enter width and height: ";
cin >> width >> height;
print_rectangle(width,height);
return 0;
}
first of all validate your width and height in your print_rectangle
String output = "hey";
if(width < output.length() || height <= 0) return;
and change the for loop like this
for (int i = 0; i < height; ++i) {
if (i == mid) {
int spacing = width - output.length();
cout <<'|'<< setw(width-spacing/2) << output << setw(spacing/2+1) << '|' << endl;
continue;
}
cout << '|' << setw(width+1) << '|' << endl;
}

C++ - Reading from a file bug

My program is working as I want it to work (It is on Bosnian language so the cout of the program is not that important). The program is about reading some text from file and then couting what I want to
The only problem I have with this program is that for some reason it is showing the two 0 0 at the end of the text file even though I do not have it in my text file
Here is the code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct proizvod
{
char naziv[100];
char proizvodac[100];
int cijena = 0;
int kolicina = 0;
};
bool poredjenje(proizvod a, proizvod b)
{
if (a.cijena != b.cijena )
return a.cijena > b.cijena;
}
void sortiranje(proizvod a[], int n)
{
sort(a, a+n, poredjenje);
}
int main()
{
ifstream datoteka;
datoteka.open("proizvodi.txt.txt");
int brojStvari = 0;
int sumaProizvoda = 0;
int ukupnaVrijednost = 0;
char* spisakProizvoda[100];
int brojFIAT = 0;
int spisakCijena = 0;
proizvod automobili[100];
if (datoteka.fail())
{
cout << "Ne postojeca datoteka";
exit(1);
}
while (datoteka.good() && !datoteka.eof())
{
datoteka >> automobili[brojStvari].naziv >> automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >> automobili[brojStvari].kolicina;
++brojStvari;
++sumaProizvoda;
}
for (int i = 0; i < brojStvari; i++)
{
cout << automobili[i].naziv << " " << automobili[i].proizvodac << " " << automobili[i].cijena << " " << automobili[i].kolicina << endl;
}
for (int i = 0; i < brojStvari; i++)
{
ukupnaVrijednost += automobili[i].cijena;
if (automobili[i].kolicina == 0)
{
spisakProizvoda[i] = automobili[i].proizvodac;
}
else if (automobili[i].proizvodac == "FIAT")
{
brojFIAT++;
}
}
char pomocna[100];
cout << endl;
cout << "Ukupan broj proizvoda u datoteci: " << sumaProizvoda << endl;
cout << "Ukupan vrijednost proizvoda u datoteci: " << ukupnaVrijednost << endl;
cout << "Spisak automobila sa cijenom 0 su: ";
for (int i = 0; i < brojStvari; i++)
{
if (!spisakProizvoda[i])
{
cout << "Ne postoje ti proizvodi " << endl;
break;
}
else
cout << spisakProizvoda[i] << endl;
}
cout << "Broj prozivoda koji proizvodi FIAT: " << brojFIAT << endl;
cout << "Sortirani proizvodi prema cijeni: " << endl;
sortiranje(automobili, brojStvari);
for (int i = 0; i < brojStvari; i++)
{
cout << automobili[i].proizvodac << endl;
}
return 0;
}
And here is the cout
Golf Volskwagen 5000 5
AudiRS5 Audi 50000 3
0 0
Ukupan broj proizvoda u datoteci: 3
Ukupan vrijednost proizvoda u datoteci: 55000
Spisak automobila sa cijenom 0 su: Ne postoje ti proizvodi
Broj prozivoda koji proizvodi FIAT: 0
Sortirani proizvodi prema cijeni:
Audi
Volskwagen
Can anybody tell me what is the problem ?
P.S : Sorry if you do not understand the program itself I apologize sincerely
You are seeing the extra 0 0 at end most likely due to an extra empty line at end of proizvodi.txt.txt file. This happens because the new line is also accepted as input but since there are no entries, the entries in struct proizvod retain default 0 values for cijena and kolicina which gets printed out as 0 0.
Update your main() code to reading file like follow and it will work as expected:
while (datoteka >> automobili[brojStvari].naziv >>
automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >>
automobili[brojStvari].kolicina) {
++brojStvari;
++sumaProizvoda;
}

How to make an output file of n*n matrix,by taking n from the user, with the numbers generated in pairs?

Currently I have a pre-made 6X6 matrix in a text file like this:
2 6 3 1 0 4
4 2 7 7 2 8
4 7 3 2 5 1
7 6 5 1 1 0
8 4 6 0 0 6
1 3 1 8 3 8
and I made a code that is reading from a file i have made. However I want to have user make a grid for themselves (i.e. 3X3 or 10X10). Which then writes to a text file automatically like in similar fashion and then have that read-in instead. It is a basic memory match card game, so I need to have rand() which generates equal pair so the game can be over when every pair in the grid has been found. Thank you so much for your time!
/*Here are the snippets of my code*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <string>
#include <numeric>
#include <limits>
using namespace std;
//global 2d vectors that are associated with the game
vector<vector<int> > game_grid;
vector<vector<int> > hidden_grid;
vector <vector<int> > guessed;
void initialize_grid() {
ifstream input_file;
input_file.open("grid.txt");
int num;
if (input_file) {
for (int i = 0; i < 6; ++i) {
vector<int> row; // game grid
vector<int> row2; // hidden grid
vector<int> row3; // guessed grid
for (int j = 0; j < 6; ++j) {
if (input_file >> num)
row.push_back(num);
row2.push_back(-1);
row3.push_back(0);
}
game_grid.push_back(row);
hidden_grid.push_back(row2);
guessed.push_back(row3);
}
cout << "Get is ready, Challenger!" << endl << endl;
}
else {
cout << "Womp. File open failed!";
}
return;
}
void print_grid() {
cout << "Game grid" << endl;
cout << " -------------------------" << endl;
for (int i = 0; i < 6; ++i) {
cout << " | ";
for (int j = 0; j < 6; ++j) {
cout << game_grid[i][j] << " | ";
}
cout << endl << " -------------------------" << endl;
}
cout << endl;
}
void print_hidden_grid(int r1 = -1, int r2 = -1, int c1 = -1, int c2 = -1) {
cout << "Attempt:" << endl;
if (r1 != -1) {
hidden_grid[r1][c1] = game_grid[r1][c1];
}
if (r2 != -1) {
hidden_grid[r2][c2] = game_grid[r2][c2];
}
for (int i = 0; i < 6; ++i) {
cout << " | ";
for (int j = 0; j < 6; ++j) {
if (hidden_grid[i][j] > -1)
cout << hidden_grid[i][j] << " | ";
else
cout << " | ";
}
cout << endl << " -------------------------" << endl;
}
cout << endl;
if (r1 != -1) {
if (game_grid[r1][c1] == game_grid[r2][c2]) {
guessed[r1][c1] = 1;
guessed[r2][c2] = 1;
cout << "You have a match!" << endl << endl;
}
else {
hidden_grid[r1][c1] = -1;
hidden_grid[r2][c2] = -1;
}
}
cout << endl << endl;
}
void print_current_grid() {
cout << "Current Grid:" << endl;
cout << " -------------------------" << endl;
for (int i = 0; i < 6; ++i) {
cout << " | ";
for (int j = 0; j < 6; ++j) {
if (hidden_grid[i][j] > -1)
cout << hidden_grid[i][j] << " | ";
else
cout << " | ";
}
cout << endl << " -------------------------" << endl;
}
cout << endl << endl;
}
.......
If I well understand you want to auto detect the size of the matrix when you read it ? If yes you can do something like that in initialize_grid :
void initialize_grid() {
ifstream input_file;
input_file.open("grid.txt");
int num;
if (input_file) {
// detect size
int size = 0;
string line;
if (!getline(input_file, line))
return;
istringstream iss(line);
while (iss >> num)
size += 1;
input_file.clear();
input_file.seekg(0);
for (int i = 0; i < size; ++i) {
vector<int> row; // game grid
vector<int> row2; // hidden grid
vector<int> row3; // guessed grid
for (int j = 0; j < size; ++j) {
if (input_file >> num)
row.push_back(num);
row2.push_back(-1);
row3.push_back(0);
}
game_grid.push_back(row);
hidden_grid.push_back(row2);
guessed.push_back(row3);
}
cout << "Get is ready, Challenger!" << endl << endl;
}
else {
cout << "Womp. File open failed!";
}
}
and else where you replace 6 by game_grid.size() (using size_t rather than int to type the indexes)

How to make exception in for loop?

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.