Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:19:01+00:00 2026-05-26T16:19:01+00:00

This is a program written in C++. It is using a pointer to initialize

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:19:01+00:00Added an answer on May 26, 2026 at 4:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I've written a simple GUI program in python using Tkinter. Let's call this program
I have written a program that uses qhttp to get a webpage. This works
This is the situation : I mostly program C# and have written types in
This program is meant to generate a dynamic array, however it gives an access
For this program #include <iostream> using std::cout; struct C { C() { cout <<
Consider this program int main() { float f = 11.22; double d = 44.55;
I have a program written in Java using JOGL with which you can draw
I have written a program with C#, that creates a logfile and fills this
I have written a program with C#, that creates a logfile and fills this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.