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;
}
const ints by reference by the way.