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

  • Home
  • SEARCH
  • 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 84071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:52:24+00:00 2026-05-10T21:52:24+00:00

I am reading in numbers from a file and then trying to add them

  • 0

I am reading in numbers from a file and then trying to add them to an array. My problem is how do you increase the size of the array? For example I thought might be able to just do:

#include <iostream> using namespace std;  int main() {     double *x;     x = new double[1];     x[0]=5;     x = new double[1];     x[1]=6;     cout << x[0] << "," << x[1] << endl;     return 0; } 

But this obviously just overwrites the value, 5, that I initially set to x[0] and so outputs 0,6. How would I make it so that it would output 5,6?

Please realize that for the example I’ve included I didn’t want to clutter it up with the code reading from a file or code to get numbers from a user. In the actual application I won’t know how big of an array I need at compile time so please don’t tell me to just make an array with two elements and set them equal to 5 and 6 respectively.

  • 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. 2026-05-10T21:52:24+00:00Added an answer on May 10, 2026 at 9:52 pm

    You don’t want to work with arrays directly. Consider using a vector, instead. Then, you can call the push_back function to add things to the end, and it will automatically resize the vector for you.

    #include <iostream> #include <vector>  int main() {     double value;     std::vector<double> values;      // Read in values     while (std::cin >> value) {         values.push_back(value);     }      // Print them back out     for (std::size_t i(0), len(values.size()); i != len; ++i) {         std::cout << values[i];     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading some numbers from a local file and then trying to sort
I'm trying to read a list of numbers from a file and sort them
I am trying to add multiple contacts from my application. I am reading in
I am reading a huge number of Integers from a file, and at the
Hi I am reading two numbers from html data-tags stored in a div. When
I am reading a text file which contains numbers in the range [1, 10^100].
When trying to read tokens from a file in C++, I receive a seg
I am trying to read in from stdin (passing in value from a file).
I'm trying to print out an wave from a wav file, but I'm kinda
I am trying to extract level data from a PCM audio file using core

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.