Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to copy data from one vector to another but am getting an error , "Invalid operands to binary expression 'int' and 'Card' " when I try to compile the following for loop:
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Would anyone have any suggestions?
I believe what you meant is
for (int i = 0; i <= vecCapacity; i++)
or even more likely
for (int i = 0; i < vecCapacity; i++)
The error message is clear enough: in this loop
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
i has type int while vectorOne[vecCapacity] has type Card and there is no defined operator <= for these types.
So this loop makes no sense.
Maybe you mean
for (int i = 0; i < vecCapacity; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Also take into account that you have to guarantee that the size of vectorTwo is not less than the size of vectorOne or at least vecCapacity.
You could use standard algorithm std::copy declared in header <algorithm>
For example
#include <algorithm>
//...
std::copy( vectorOne, vectorOne + vecCapacity, vectorTwo );
You should be looping from 0 to vectorOne's size.
for (int i = 0; i < vectorOne.size(); i++) { //step 3
vectorTwo[i] = vectorOne[i];
}`
`
Also, if you're doing it this way, make sure vectorTwo is big enough before the loop.
vectorTwo.resize(vectorOne.size());
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to concatenate two arrays that were initialized from file input into one dynamic array and returning it to an output file but while testing I found out that the returned dynamic array is initialized with random numbers. I'm sorry for my lack of experience as I am a beginner.. These are my input text files
Input1.txt
1
2
3
Input2.txt
4
5
6
and this is my code ..
#include <iostream>
#include <fstream>
using namespace std;
int * concatenate(int *a1,int *a2,int size);
int main() {
int size = 3;
int arr1[size];
int arr2[size];
ifstream fin;
ofstream fout;
fin.open("input1.txt");
for(int i = 0; i < size; i++) {
int value;
fin>>arr1[i];
cout<<arr1[i]<<endl;
}
fin.close();
fin.open("input2.txt");
for(int j = 0; j < size; j++) {
fin>>arr2[j];
cout<<arr2[j]<<endl;
}
int *arr3 = concatenate(arr1,arr2,size);
cout<<arr3[1];
return 0;
}
int * concatenate(int *a1,int *a2,int size) {
int * r = new int[size*2];
for(int i = 0; i > size*2; i++) {
if(i < size)
r[i] = a1[i];
else
r[i] = a2[i-size];
}
return r;
}
If you're using c++ you should consider using vectors instead of arrays.
They make operations like these way easier.
Here's for reference:
https://en.cppreference.com/w/cpp/container/vector
https://www.geeksforgeeks.org/vector-in-cpp-stl/
You can referer to this post to see how to concatenate two vectors. Concatenating two std::vectors
If you want to stick to your method there's an error in your concatenate function.
You never actually enter the for-loop since your condition for stopping is wrong. The condition in your loop is continue iterating while i > size * 2 but, with i = 0 at the start, that will never be true thus you will never enter the loop. You can fix this just by inverting the comparaison like so i < size * 2.
To avoid this kind of errors you can try to setup a debugger and go through your code line by line seeing the values of variables and what lines are actually executed. This method will help you catch a lot of small mistakes like these.
As for the random values in your return array it's because C and C++ don't clean the memory being allocated by your program when creating a new array or calling malloc for example.
you seem to have a small typo in your code in the last for loop,
for(int i = 0; i > size*2; i++)
i think you ment (watch the comparison sign)
for (int i = 0; i < size*2; i++)
you can try to cout values from the code you think doesn't work, or even better, use an IDE like Visual studio to debug your code when it fails for no apparent reason, which allows you to see your code executing line by line in the future.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
i got a 5x5 matrix and i need to hash its values.
so i'm trying to hash the 2nd lines and 3rd columns like this
void hashing(int matris[5][5]) {
int x = 0;
int y = 0;
cin >> x;
cin >> y;
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
if ((i==x-1)|| (k==y-1))
{
matris[i][k] = "*";
}
}
}
}
thats the code but i got a error that follows:
"a value of type "const char*" can not be assigned to an entity of type "int""
does anyone knows how to do it ?
When you declare int matrix[5][5] you are telling your program that you want to store integers in that matrix, basically numbers. So the error says that your can't put into a matrix that stores integers, a char like "*", and infact you can't.
Edit: a possible solutions will be changing your matrix from int to string (parsing integers to strings) as #Some Programmer Dude suggested, but that depends on your needs
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm working on a hangman game for a college asignment and i aleady have most of the work done but for some reason this part here is not working. Is there a problem with my logic? It seems to be incredibly simple.
bool second_check(char user_input) {
char u[3]={'a','r','i'};
for (int i = 0; i <= 3; i++) {
if (user_input==u[i]){
return true;
};
};
return false;
}
int main(){
char o;
cout<<"enter"<<endl;
cin>>o;
if (second_check(o)==true) {
cout << "Correct!" << endl;
}
else
cout << "Wrong! \n Strike one!" << endl;
return 0;
}
The for loop will loop 4 times even though you have 3 elements, causing it to try and access an undefined location in memory, to fix this replace the 'i <=3' with 'i<3'
so the for loop should like this in the end:
for (int i = 0; i < 3; i++) {
if (user_input==u[i]){
return true;
};
};
Answer
Since char u[3]={'a','r','i'}; contains only 3 characters, your for loop will be:
for (int i = 0; i <= 2; i++) or
for (int i = 0; i < 3; i++).
Explanation
This is because, in C/C++ and most of the programming language, the array count starts from 0. Therefore the first element will be array[0] and last will array[n-1] where n is the size of the array used at initialization. (Above, n=3)
So it would be helpful to the community if you state clearly what the problem is (i.e expected vs. actual output).
That said, a couple problems I can see...
for (int i = 0; i <= 3; i++)
if (user_input==u[i]){
Since u is of size 3 (char user[3]), you need to change your for loop to be i < 3 since arrays are 0 based, valid indices are 0,1,2 and you will go out of bounds of the array. I.e. user[3] is not a valid index.
You are not comparing an index of user_input which I suspect you want to. i.e. user_input[i].
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am passing two 2d arrays of char into a function, that will return 1 if the arrays are equal and 0 otherwise.
size of the arrays is [4][4], However when I try to iterate through the array I get an error.
int checkIfWin(char** array, char** array2){
int winner = 0;
for(int i = 0; i < 4; i++){
if(strcmp(array2[i], array[i])==0){
winner = 1;
}
else
winner = 0;
}
return winner;
}
UPDATE:
The reason why I was getting an errorn while strcmp is because I did not malloc the second 2d array.
char[] is different from char *, the first places the value '\0' at the end of the array instead the second does not.
The function strcmp will compare the elements until it finds the value '\0', for that task better compare element to element and if you find a difference returns automatically.
int checkIfWin(char** array, char** array2){
for(int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
if(array2[i][j] != array[i][j]){
return 0;
}
}
}
return 1;
}
Here is a solution using strncmp():
int winner = 1, i;
for(i = 0; i < 4; i++){
if((strncmp(array2[i], array[i], 4)) != 0){
winner = 0;
break;
}
}
return winner;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
So, I need to get the sum of the first 5 even numbers from my array, this is the code that I've got so far, have no clue what to do next. It runs, but the result is incorrect
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int niz[10];
cout << "Unesi 10 brojeva:";
for (int i = 0; i < 10; i++) {
cin >> niz[i];
}
int suma = 0;
int parni[5];
int j = 0;
for (int i = 0; i < 10; i++) {
if (niz[i] % 2 == 0) {
niz[i] == parni[j];
j++;
if (j == 5) {
break;
}
}
}
for (int i = 0; i < 5; i++) {
suma = parni[i] + suma;
}
cout << suma;
system("PAUSE");
return 0;
}
This line:
niz[i] == parni[j];
does nothing. (It tests if niz[i] happens to be equal to the current, uninitialized value of parni[j], and throws away the result of the comparison.)
You want to store niz[i] in parni[j], so do this:
parni[j] = niz[i];
Incidentally, there is a problem if there are fewer than 5 even numbers in the niz array. In that case, you still sum up all five entries of the parni array, using uninitialized values, which is Bad. One way to avoid this is to just sum up the even entries as you find them, without using a secondary array.
IE, do suma += niz[i] at the line in question, and get rid of parni altogether.
Unless you're really required to use arrays here, vectors will work much more nicely.
You can also use a couple of standard algorithms to make your life easier (especially std::copy_if and std::accumulate).
// for the moment I'll ignore the code to read the input from the user:
auto input = read_input();
auto pos = std::remove_if(input.begin(), input.end(),
[](int i) { return i % 2 != 0; });
// assume that `input` always contains at least 5 elements
pos = std::min(pos, input.begin() + 5);
sum = std::accumulate(input.begin(), pos, 0);