What have I done wrong in this C++ code?

+1 Tom Weekes · February 27, 2015
I am using CodeBlocks 13.12. I wanted to try Bucky's 35th C++ video tutorial but I got a completely different result.
When I run the code it's listing 4285920 endlessly.
I looked through the code many times but couldn't spot the error. Can anyone help, please?

#include

using namespace std;

void printArray(int theArray[], int sizeOfArray);

int main()
{
    int box[3] = {2,45,789};
    int pack[6] = {1,123,8,954,7,3};

    printArray(box, 3);

    return 0;

}

void printArray(int theArray[], int sizeOfArray){

    for(int x = 0;x=sizeOfArray; x++){
        cout

Post a Reply

Replies

Oldest  Newest  Rating
0 Nehuen Diez · February 27, 2015
Hi! First of all the include is not complete: #include <iostream>

And second you are missing part of the code...
+1 K D · February 27, 2015
You are setting x equal to sizeOfArray you are not comparing them so you created an infinite loop because x will always equal sizeOfArray.

it should read:
for (int x = 0; x < sizeOfArray; x++){
+1 Habib Nuhu · February 27, 2015
for your loop, you equated x to the size of array instead of being less than the size of the array,so that x would be increased continuously by 1 and during each increment, it would print out theArray[x], which would be box[0] for the first, box[1] for the second and box[2] for the third.......your  code should be exactly like this..



#include
using namespace std;


void printArray(int theArray[], int sizeOfArray);

int main(){

int box[3] = {2,45,789};
   int pack[6] = {1,123,8,954,7,3};

   printArray(box, 3);
   
   return 0;


}

void printArray(int theArray[], int sizeOfArray){

   for(int x = 0;x
0 Tom Weekes · February 28, 2015
Thank you for your help guys. I have changed ' = ' to ' < ' as K D and Habib mentioned. The code works now:-) I really slipped through  this tiny error which. In regard to the missing bits: When I copied, pasted it appeared right but when submitted it some parts disappeared for some reason. I tried to copy and paste it to notepad too and the whole code appeared fine.
  • 1

C++

107,232 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator