I think I've changed the console language from ASCII to unicode and now when I run a code that goes like c = 'A'+1; it spits out emojis. (c being a char character)
I once clicked on something, I did not know what it meant but I have no idea how to search for a fix now and I've been looking for one for the past 3 hrs...
code example:
#include <iostream>
using namespace std;
int n, i, j;
char a[22][22];
int main() {
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
if (i == j)
a[i][j] = 'A';
else
if (i < j)
a[i][j] = a[i][j] + 1;
else
a[i][j] = a[i - 1][j + 4];
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++)
cout << a[i][j] << ' ';
cout << endl;
}
return 0;
}
output
A ☺ ☺ ☺ ☺
☺ A ☺ ☺ ☺
☺ A ☺ ☺
☺ A ☺
☺ A
Related
I wanna print two diamonds side by side, but my code prints 1 diamond.
I spent lots of time on it and really do not know what else to do.
Any help would be appreciated.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
for ( j = 0; j < spaceCount; j++)
cout << " ";
for (j = 0; j < starCount; j++)
cout << "*";
for (j = 0; j < spaceCount; j++)
cout << " ";
cout << endl;
}
}
input = odd numbers
desired output =
* *
*** ***
**********
*** ***
* *
You forgot to print you second diamond shape. Each iteration, you should print first a few spaces, then the stars, then the double of the spaces (to finish the first diamond and set spaces for the second diamond) and then you should draw you stars for your second diamond.
This is an example of code that prints two diamonds:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int i,j,n, middle, spaceCount, starCount;
cin >> n;
middle = (n - 1) / 2;
for ( i = 0; i < n; i++)
{
spaceCount = abs(middle - i);
starCount = n - 2 * abs(middle - i);
// print a row of the first diamonds
// spaces at the beginning
for ( j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// finish the rectangle of the first diamond
for (j = 0; j < spaceCount; j++)
cout << " ";
// print a row of the second diamond
// spaces at the beginning
for (j = 0; j < spaceCount; j++)
cout << " ";
// the stars itself
for (j = 0; j < starCount; j++)
cout << "*";
// spaces at the end are not necessarily required for the last diamond
cout << endl;
}
}
It would be even better to create a function to print one diamond (row) and call this function two times (this prevents duplicate code).
As a challange I don't use array indexes to assigning or searching. What I want to do is reading characters from a text file and put them in an array that is 15x15. I created a 2d array using indexes but I couldn't do the assigning without them. I tried two things but I couldn't come up with a working idea. My code is basically like that:
char **puzzleArray;
puzzleArray = new char*[15];
for (int i = 0; i < 15; i++)
puzzleArray[i] = new char[15];
char *c;
FILE* puzzle;
puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
fread(&c, sizeof(char), 1, puzzle);
if (*c != ' ' && *c != '\n')
strcpy(*(puzzleArray + (i * 15)) + j , c);
else
j--;
}
I also tried assignin like this:
char **puzzleArray;
puzzleArray = new char*[15];
for (int i = 0; i < 15; i++)
puzzleArray[i] = new char[15];
char c;
FILE* puzzle;
puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
fread(&c, sizeof(char), 1, puzzle);
if (c != ' ' && c != '\n')
*(puzzleArray + (i * 15)) + j = c;
else
j--;
}
But none of them worked and when I do the second one compiler gives me the error "lvalue required as left operand of assignment".
Edit:
When I change the second code as:`
*(*(puzzleArray + (i * 15)) + j)
It compiled but when I tried to print the characters in array using this:
for (int i = 0; i < 15; i++) {
cout << endl;
for (int j = 0; j < 15; j++)
cout << *(*(puzzleArray + (i * 15)) + j);
}
//or using this
for (int i = 0; i < 15; i++) {
cout << endl;
for (int j = 0; j < 15; j++)
cout << puzzleArray[i][j];
}
It does not print characters and program crashes. In visual studio, it says an exception occurs at the assigning part.
You don't need nested allocations.
What's more, your code does not work because you assume your nested allocations will result in contiguous storage.
const int size = 15;
char* puzzleArray = new char[size*size];
// i'm not going to change file reading logic,
// but it should be done better
// ideally by one read to a buffer
// Also the style seems like a bad mix of C and C++,
// but I can't do much about it with only this little fragment of code.
FILE* puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < size; i++) {
for (int j = 0; j < size; j++) {
char c;
fread(&c, 1, 1, puzzle);
if (c != ' ' && c != '\n')
puzzleArray[i*size + j] = c;
/*
// this does not seem right, i don't know what you're trying to do here
// i'm gonna assume that you want to move to the next line
else
j--;
*/
else
break;
}
}
There are two problems for your second try. And whether the memory is continous or not is not relevant. You can still use 2D array and using nested allocations.
(note that, although somebody downvoted my answer, but I think he/she did it wrong).
Here is the fully tested code using 2D array and nested memory allocations.
#include <iostream>
#include <cstdlib>
int main() {
using namespace std;
char **puzzleArray;
puzzleArray = new char*[15];
for (int i = 0; i < 15; i++)
puzzleArray[i] = new char[15];
char c;
FILE* puzzle;
puzzle = fopen("puzzle.txt", "r");
for (int i=0; i < 15; i++) {
for (int j = 0; j < 15; ) {
fread(&c, sizeof(char), 1, puzzle);
if (c != ' ' && c != '\n') {
*(*(puzzleArray + i) + j) = c; // you can see the difference if you compare with your original code.
++j;
}
}
}
for (int i = 0; i < 15; ++i) {
for (int j = 0; j < 15; ++j) {
cout << puzzleArray[i][j]; // you could use pointer arithmatic here too. using index is just convenient to show the result.
}
cout << std::endl;
}
}
I am new in SFML and working on game "Memory Match" 4x4. To simplify I am loading 2 different image.Each time I get the last image on array.Can anyone say me what I am doing wrong with this?
string s[] = {
"C:/Users/Eldar/Desktop/sfml/image.png",
"C:/Users/Eldar/Desktop/sfml/redDie.png"
};
int k = 0;
for (int i = 0; i <= 3; i++){
for (int j = 0; j <= 3; j++){
cout << k << endl;
Cards[i][j].setScale(1, 1);
Cards[i][j].setPosition(0 + ioffset, 0 + joffset);
textureCard.loadFromFile(s[k]);
Cards[i][j].setTexture(textureCard);
k++;
if (k == 2) k = 0;
}
}
At the start of my program I declare the variables i and j.
int i, j;
In the course of execution, I use the variable names i and j as a oop index variables too. I realize this is probably not the best choice in terms of clarity, but as this is a toy project I thought it wouldn't matter.
The problem is, after the following loop that prints the contents of an array to a txt file, i == N == 29:
ofstream a_file("2d_array.txt");
for (int i = 0; i<N; ++i) {
for (int j = 0; j<N; ++j)
a_file << m[i][j] << ' ';
a_file << endl;
}
When I try to use i again later in the program:
for (int num_slices_processed = h + 1; num_slices_processed < N;
num_slices_processed++){
i = 0;
j = num_slices_processed;
...
i remains set to 29, even after the line that should set it to 0. j is set correctly though. What is happening here?
Here is all the code up to the problem section:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int smallest(int x, int y, int z);
bool basesMatch(char b1, char b2);
int main() {
const int N = 29;
int h, l, mm;
int i, j;
l = 10;
mm = 2;
h = 5;
int m[N][N];
//initialize m
for (int i = 0; i < N; i++){
for (int j = i; j < N; j++) {
if (j-i <= h)
m[i][j] = 0;
}
}
ofstream a_file("2d_array.txt");
for (int i = 0; i<N; ++i) {
for (int j = 0; j<N; ++j)
a_file << m[i][j] << ' ';
a_file << endl;
}
for (int num_slices_processed = h + 1; num_slices_processed < N; num_slices_processed++){
//while j is in bounds, ie j < N. This fills in one diagonal slice of m from L->R top -> bottom.
i = 0; //************ i is not being set here
j = num_slices_processed;
while (j < N) {
if (basesMatch(seq[i], seq[j])) {
t = m[i + 1][j - 1];
m[i][j] = m[i + 1][j - 1];
}
else {//bases don't match
m[i][j] = smallest(m[i + 1][j] + 1, m[i][j - 1] + 1, m[i + 1][j - 1] + 1);
}
i++;
j++;
}
}
Check to see if it even enters the loop. num_slices_processed could be null or already < N, so you are never hitting the assignment statement.
I want to create a shape like this:
ccccccc
cccccc
ccccc
cccc
ccc
cc
c
My code is:
#include <iostream>
using namespace std;
int main(){
int i, j;
for(i = 0; i < 7; i++){
for(j = 7; j > 7; j--){
cout << 'c';
}
cout << endl;
}
return 0;
}
But in terminal the output I get is some blank lines.
What am I doing wrong?
for(j = 7; j > 7; j--){ This expression is always false.
You need to write for(j = 7; j > i; j--){
You want this:
#include <iostream>
using namespace std;
int main(){
int i, j;
for(i = 7; i > 0; --i){
for(j = i; j > 0 ; j--){
cout << 'c';
}
cout << endl;
}
return 0;
}
live example
Your original code had a logic error in the inner loop
for(j = 7; j > 7; j--){
here j is 7 but j will never be greater than 7 so it never executes, but even if this was fixed to
for(j = 7; j > 0; j--){
This will just cout 7 'c' 7 times, so what I modified was to change your inner loops starting value so that it then decrements correctly.
for(i = 7; i > 0; --i){
for(j = i; j > 0 ; j--){
^ now initialised by outer loop
So what would happen is that the inner loop never executed but you executed cout << endl; 7 times hence the blank lines
The condition of the loop
for(j = 7; j > 7; j--){
is wrong. That is it always is equal to false because initially i is set to 7 and it can not be greater than 7.:)
I think you mean something like
for(j = 7 - i; j > 0; j--){
The program can be written simpler.
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0-exit): ";
size_t n = 0;
std::cin >> n;
if ( !n ) break;
const char c = 'c';
std::cout << std::setfill( c );
while ( n ) std::cout << std::setw( n-- ) << c << std::endl;
}
return 0;
}
The program output is
Enter a non-negative number (0-exit): 7
ccccccc
cccccc
ccccc
cccc
ccc
cc
c
Enter a non-negative number (0-exit): 0