I get a error
expected initializer befor '/=' token.
I have
const unsigned in array[]
in my loop, I have:
for (int i = 0; i< length; i++)
{
while (array[i] > 0)
{
const unsigned int array[i] /= 10;
}
}
How can I fix it? Thanks!
const unsigned int array[i] /= 10;
should be:
array[i] /= 10;
If you write a type before a variable name, you perform a variable declaration. This is however not your intention here, you simple want to access it.
I suspect you intend to divide each array entry by 10. I also assume that your gave your array a size (in the brackets). I also assume that length is correct.
Still there are multiple errors.
First the array should be defined unsigned int instead of const unsigned in (remove the const and fix the typo) otherwise it can not be modified.
Then remove the type declaration in the loop and use array[i] /= 10; instead of const unsigned int array[i] /= 10;.
Additionally I wonder why you try to use two nested loops? Simply remove the while loop entirely:
for (int i=0; i<length; i++)
{
array[i] /= 10;
}
I think you have to read a little up on both arrays and in more general C.
When you declare a variable with 'const' it declares it as an constant and therefor it cannot be changed later.
const unsigned int array[]
for (int i = 0; i < length; i++)
{
while (array[i] > 0)
{
const unsigned int array[i] /= 10;
}
}
Should be changed to
unsigned int array[];
for (int i = 0; i < length; i++)
{
if (array[i] > 0)
{
// If array[i] is greater than zero, divide it by 10
array[i] /= 10;
}
}
Ok that was the broad error fixing please checkout those links:
How to initialize all members of an array to the same value?
http://en.wikipedia.org/wiki/Constant_(programming)
Thanks.
Related
So I set up a simple array and iterate through it. For each element, array[i] is filled with a value of 100. I do i < 4 because obviously array[5] doesn't exist for an array OF FIVE ELEMENTS.
If arrays index at 0, why is the compiler NOT freaking out at me? array[5] shouldn't exist...
using namespace std;
int main()
{
int array[5];
for (int i = 0; i < 5; i++)
{
array[i] = 100;
}
for (int i = 0; i < 5; i++)
{
cout << array[i] << "\n";
}
}
If i < 5 then it must be 4 or less, meaning the maximum is array[4], which is not out of bounds.
I think you must have confused it with doing i <= 5 which you also see a lot of.
int array[5];
for (int i = 0; i < 5; i++)
{
array[i] = 100;
}
is more or less equivalent to
int array[5];
array[0] = 100;
array[1] = 100;
array[2] = 100;
array[3] = 100;
array[4] = 100;
So there is no array out of bounds in that code.
You are never accessed a[5]. You iterated for a[0],a[1],a[2],a[3],a[4] only.So it will not throw any error.In loop condition you have written i<5 not i<=5.
When you declare int array[5];, the 5 doesn't refer to any indexing.. It refers to number of element. But only at this point... When you declare it. Later on, you can access the array through indices 0-4, which you are doing since you have i < 5.
Even if you were to access array[5] it's not guaranteed your compiler would freak out... But that's maybe another topic, or?
I am new to c++. I need help fixing this error:
Item.cpp: In member function ‘char* ict::Item::sku() const’:
Item.cpp:65:36: warning: comparison between signed and unsigned integer
expressions [-Wsign-compare]
This is the part of the code that is giving the error:
//in header file
char m_sku[MAX_SKU_LEN + 1];
//in cpp file
char* Item::sku() const
{
int length = strlen(m_sku);
char *arr = new char[length]();
for (int i = 0; i <= strlen(m_sku); i++) {
arr[i] = m_sku[i];
}
return arr;
}
The most straightforward way to fix this is to make i an unsigned variable instead of a signed one. You can use size_t to match the return type of strlen:
size_t length = strlen(m_sku);
char *arr = new char[length]();
for (size_t i = 0; i <= length; i++) {
arr[i] = m_sku[i];
}
But be careful since this same replacement doesn't work with loops that count down towards 0.
// oops! This is an infinite loop:
for (size_t i = length-1; i >=0; i--) {
arr[i] = m_sku[i];
}
Write a static cast (int)strlen(m_sku) or vice versa std::size_t i = 0.
So that compared items will be the same.
I'm trying to fill an array with numbers 1111 to 8888, with each integer in the number being between 1 and 8 in c++. However, when I run it, it's only outputting large negative numbers indicating an error. I honestly have clue what the error is so it would be appreciated if you could help me out. Thanks!
int fillArray()
{
int arrayPosition;
int guesses[4096];
arrayPosition = 0;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++)
for (int k = 1; k <= 8; k++)
for (int m = 1; m <= 8; m++)
{
guesses[arrayPosition] = ((i * 1000) + (j * 100) + (k *10) + m);
cout << guesses[arrayPosition];
arrayPosition++;
}
return guesses[4096];
}
Your return type is wrong. int fillArray(), but you're trying to return an int[4096] that was declared on the stack... What you're actually doing with return guesses[4096]; is returning the first memory location after your array in memory, which is probably just garbage, hence your issue with large negative numbers.
You can fix it by allocating your array in the heap, and returning a pointer to the start of that array:
int * fillArray()
{
int arrayPosition;
int * guesses = new int[4096];
// other stuff stays the same...
return guesses;
}
However, since your function is called fillArray, it would make more sense to pass in an array and fill it rather than creating the array in the function. (If you wanted to do that, might call it something like make_1_to_8_array instead, to make it more clear that you're constructing something that will need to be deleted later.) Giving an int* as the first argument would allow you to pass in the base address of your array that you want filled:
void fillArray(int * guesses)
{
int arrayPosition;
// other stuff stays the same...
}
Or, if you want to verify that the you're using an array of the exact size:
void fillArray(int (&guesses)[4096])
{
int arrayPosition;
// other stuff stays the same...
}
Note that the function now returns void since you just update the array that was passed in, and you don't need to return anything new.
Your for-loops look correct, but your array handling is off, as is highlighted by other answers.
It is more usual in C++ to use std::vector and to pass this in by reference as an argument. This saves you having to handle memory allocations and deallocations. Here's an example, including the output in the for-loops:
#include <iostream>
#include <vector>
int fillArray(std::vector<int>& guesses)
{
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++)
for (int k = 1; k <= 8; k++)
for (int m = 1; m <= 8; m++)
{
guesses.push_back((i * 1000) + (j * 100) + (k * 10) + m);
std::cout << guesses.back() << std::endl;
}
return guesses.back();
}
int main()
{
std::vector<int> guesses;
std::cout << fillArray(guesses) << std::endl;
}
You are creating your array locally then attempting to return it. If you try printing (to debug) out the result of your array prior to returning, you will see it is ok. However, once you return, the array is no linger valid. Try passing in an array into your function instead.
I'm completely new to C++.
Bashing my head against this error for over an hour. Probably someone with experience can see right through it.
The following code gives an error:
class TimeTravellingCellar {
private:
public:
int determineProfit (int [] profit, int [] decay) {
int N = sizeof(profit)/sizeof(decay);
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) continue;
if (profit [i] - decay [j] > max)
max = profit [i] - decay [j];
}
}
return max;
}
}
Visual Studio Express puts a red line under profit in the parameters of determineProfit and says:
expected a ')' before identifier profit.
I will appreciate some help.
Thanks!
You are declaring your arrays as if this were c#. It should be
int profit[]
Or
int *profit
You'll hit this one next. You need to terminate your class with a semi-colon.
class Foo {
}; <----
The next problem you have is logical, not syntactic. This does not do what you think it does:
int N = sizeof(profit)/sizeof(decay);
You are taking the sizeof two pointers, not the size of the arrays. You actually have:
int N = 4/4 /* assumes sizeof int == 4 */
You need to pass in the size of your to the function as well (or, better yet; stop using arrays and use a vector<T>.)
When you take an "array" as an argument to your function it actually decays to a pointer to the array type (an array proper cannot be passed to a function). So it follows that:
void Foo( int array[] ) {
size_t arrSize = sizeof(array);
// arrSize == 4 for a 32-bit system, i.e., sizeof(int*)
int a[100];
size_t actualSizeInBytes = sizeof(a);
// actualSizeInBytes == 400, i.e., 4 * 100 as an int occupies 4 bytes
}
Next, this line causes your first iteration to always be skipped. Not sure if that is intentional:
if (i == j) continue;
You don't declare arrays like that in C++, the [] needs to go after the name.
Also note you need to have a semicolon after the class declaration.
class TimeTravellingCellar {
private:
public:
int determineProfit (int profit[], int decay[]) {
int N = sizeof(profit)/sizeof(decay);
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) continue;
if (profit [i] - decay [j] > max)
max = profit [i] - decay [j];
}
}
return max;
}
};
Edit: also remember that sizeof(pointer) will return the number of bytes of the pointer type, not the number of elements in the array. So if you have an int array, sizeof(array) == sizeof(int). Your N value will always equal 1.
This line is wrong:
int determineProfit (int [] profit, int [] decay) {
Change it into:
int determineProfit (int profit[], int decay[]) {
or
int determineProfit (int* profit, int* decay) {
and add a closing ;
If you do that and add a main, of course:
int main() {}
then you can compile your code - I just tried it with g++.
Try int determineProfit (int* profit, int* decay) because for formal arguments, arrays and pointers are almost alike.
Brackets are associated with the variable name, not the type. The first line should be
int determineProfit (int profit[], int decay[]) {
A tutorial on arrays in C may be enlightening, especially as regards the passing of array parameters.
int determineProfit (int[] profit int [] decay
here is your error - the above statement is wrong; it should be like this
int determineProfit (int profit[], int decay[])
This following code gets this compile error: "invalid types 'int[int]' for array subscript". What should be changed?
#include <iostream>
using namespace std;
int main(){
int myArray[10][10][10];
for (int i = 0; i <= 9; ++i){
for (int t = 0; t <=9; ++t){
for (int x = 0; x <= 9; ++x){
for (int y = 0; y <= 9; ++y){
myArray[i][t][x][y] = i+t+x+y; //This will give each element a value
}
}
}
}
for (int i = 0; i <= 9; ++i){
for (int t = 0; t <=9; ++t){
for (int x = 0; x <= 9; ++x){
for (int y = 0; y <= 9; ++y){
cout << myArray[i][t][x][y] << endl;
}
}
}
}
system("pause");
}
You are subscripting a three-dimensional array myArray[10][10][10] four times myArray[i][t][x][y]. You will probably need to add another dimension to your array. Also consider a container like Boost.MultiArray, though that's probably over your head at this point.
What to change? Aside from the 3 or 4 dimensional array problem, you should get rid of the magic numbers (10 and 9).
const int DIM_SIZE = 10;
int myArray[DIM_SIZE][DIM_SIZE][DIM_SIZE];
for (int i = 0; i < DIM_SIZE; ++i){
for (int t = 0; t < DIM_SIZE; ++t){
for (int x = 0; x < DIM_SIZE; ++x){
Just for completeness, this error can happen also in a different situation: when you declare an array in an outer scope, but declare another variable with the same name in an inner scope, shadowing the array. Then, when you try to index the array, you are actually accessing the variable in the inner scope, which might not even be an array, or it might be an array with fewer dimensions.
Example:
int a[10]; // a global scope
void f(int a) // a declared in local scope, overshadows a in global scope
{
printf("%d", a[0]); // you trying to access the array a, but actually addressing local argument a
}
int myArray[10][10][10];
should be
int myArray[10][10][10][10];
You're trying to access a 3 dimensional array with 4 de-references
You only need 3 loops instead of 4, or int myArray[10][10][10][10];
I think that you had intialized a 3d array but you are trying to access an array with 4 dimension.
I just forgot to put [] to the function parameter with array data type like this:
🚨 Instead of this:
void addToDB(int arr) {
// code
}
✅ Do this:
void addToDB(int arr[]) {
// code
}