1

This is a program written in C++. It is using a pointer to initialize int array and then read the data back. But instead of int values it gives a crap- many letters and digits. What might be the problem? I can't work out it. this is cpp file:

#include<string>
#include<cmath>
#include<sstream>
#include "TwoSeries.hpp"

using namespace std;

TwoSeries::TwoSeries() {
}

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{
    int arrayA [size];
    int arrayB [size];

    arrayA[0] = a0;
    arrayB[0] = b0;

    int *aArray = getArrayA();
    int *bArray = getArrayB();

    for(int x = 1; x < 200; x++)
    {       
        *(aArray + x) = -1;
        *(bArray + x) = -1;   

    }

}

TwoSeries::~TwoSeries() {
}

int TwoSeries::getA(int& index)
{

    return 0;
}
int TwoSeries::getB(int& index)
{

    return 0;
}

string TwoSeries::getArrays()
{
    ostringstream outString;
    string stA;
    string stB;
    string valueA;
    for(int x = 0; x < 200; x++)        
    {
        outString << x;
        string index = outString.str();

        int *arrayA = getArrayA();
        outString << *(arrayA + 1);
         valueA = outString.str();


        int * arrayB = getArrayB();
        outString << getArrayB() + x;
        string valueB = outString.str();


    //    stA += index + ":" + valueA + " ";
    //    stB += index + ":" + valueB + " ";
    }

  //  return "Series A: \n"+stA+ "/n"+"Series B: \n"+ stB;   
    return valueA;
}

int* TwoSeries::getArrayA()
{
    int * pointer = arrayA;

    return pointer;
}
int* TwoSeries::getArrayB()
{
   int * pointer = arrayB;

    return pointer;
}

This is hpp file:

#include<string>
#include<cmath>
#include<sstream>

using namespace std;

#ifndef TWOSERIES_HPP
#define TWOSERIES_HPP

class TwoSeries {
public : 
  TwoSeries();
  TwoSeries(const int&, const int& , const int&);
  int getA(int&);
  int getB(int&);
  string getArrays();
  int* getArrayA();
  int* getArrayB();

    virtual ~TwoSeries();
private:
    int arrayA[200];
    int arrayB[200];
};

#endif  /* TWOSERIES_HPP */

This is main method:

#include<iostream>
#include<string>
#include "TwoSeries.hpp"

using namespace std;

/*
 * 
 */
int main() {
    const int A0 = 1; // the value at index 0 at array A
    const int B0 = 1; // the value at index 0 at array B
    const int SIZE = 200; // the size of array A and B
    const int EXIT = 4; // the digit on which the loop stops    
    int option; // stores the value entered by the user i.e. selected option from the list
    int index;  // stands for the index in either A or B array
    TwoSeries two = TwoSeries(SIZE, A0, B0);

    do
      {
        cout << "You have the following options: \n 1 - Display the current arrays\n";
        cout << " 2 - request the values of A \n 3 - request the values of B\n 4 - exit \n";
        cin >> option;
        if(option == 1)
        {
          cout << two.getArrays();  
        }
        if ((option == 2) || (option == 3))
        {
            do
            {
               cout << "Please enter the position: ";
               cin >> index;
               if((index >= 0) && (index <= (SIZE - 1)))
               {
                  if(option == 2){}
                     // getA(index);                 
                  //else
                   //   getB(index);
               }
            }
            while((index >= 0) && (index <= (SIZE - 1)));

        }
      }
      while((option != EXIT) || option < 1 || option > 4 );

    return 0;
}
4
  • There's not really any point in passing const ints by reference by the way. Commented Nov 5, 2011 at 1:01
  • 2
    Any chance you might be able to narrow this down and provide a minimal, complete example of your problem? Commented Nov 5, 2011 at 1:02
  • Why are you using pointer arithmetic instead of array [] notation? Commented Nov 5, 2011 at 1:17
  • I have to use pointer because this is compulsory for my assignment. Commented Nov 5, 2011 at 12:17

2 Answers 2

1

There are so many things wrong here so lets take it slow :

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{
    int arrayA [size]; //no way this compiles without new [] operator
    int arrayB [size]; //also why you name identical local variable with the same name as your private data members?

    arrayA[0] = a0; //so you initialize the first member with some value
    arrayB[0] = b0; //and then all the other values to -1 is this intended?

    int *aArray = getArrayA(); //why are you doing this?
    int *bArray = getArrayB(); // you can access the data member by using this->arrayA; even without the this, but since you have identical name with locals you need the "this"

    for(int x = 1; x < 200; x++)
    {       
        *(aArray + x) = -1; //avoid pointer arithmetic, it's error prone
        *(bArray + x) = -1; //do this instead bArray[x] = -1;   

    }

}

Next :

int TwoSeries::getA(int& index) { //I guess you want to return an element from the arrayA? return arrayA[index]; //error prone, check if index is between bounds and a positive.. return 0; }

Next :

int* TwoSeries::getArrayA()
{
    //int * pointer = arrayA; //not needed

    return arrayA;
}
int* TwoSeries::getArrayB()
{
   //int * pointer = arrayB; //same as above

    return arrayB;
}

Next :

string TwoSeries::getArrays()
{
    ostringstream outString;
    string stA;
    string stB;
    string valueA;
    for(int x = 0; x < 200; x++)        
    {
        outString << x;
        string index = outString.str();

        //int *arrayA = getArrayA(); //same mistake as before 
        //outString << *(arrayA + 1); //you realize you always access the same element right?
        outString << arrayA[x]; //I guess this is what you wanted?
         valueA = outString.str();


        int * arrayB = getArrayB(); // see above
        outString << getArrayB() + x;
        string valueB = outString.str();


    //    stA += index + ":" + valueA + " ";
    //    stB += index + ":" + valueB + " ";
    }

  //  return "Series A: \n"+stA+ "/n"+"Series B: \n"+ stB;   
    return valueA;
}

Probably there are more bugs hidden in here. But you posted something which does not even compile so we can't really help you.

Sign up to request clarification or add additional context in comments.

2 Comments

This line : int arrayA [size]; cannot possibly be compiling.
getArrayA() and getArrayB() are needed to return pointer of each array. Functions getA() and getB() are used to return value stored at selected cell in the array. The values are calculated according to a special formula. It is not completed yet.
0

This

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{
    int arrayA [size];
    int arrayB [size];

should be just

TwoSeries::TwoSeries(const int& size, const int& a0, const int& b0) 
{

since otherwise the rest of that function will assume the local variables to be used instead of the classes' private members.

Also I don't see the point of the size argument, since you hard-code the array's size in the hpp file?

One last remark:

int * arrayB = getArrayB();
outString << getArrayB() + x;

should just be

int * arrayB = getArrayB();
outString << arrayB[x];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.