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 7737253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:56:55+00:00 2026-06-01T07:56:55+00:00

Despite being a Project Euler program, the following code doesn’t actually concern it much.

  • 0

Despite being a Project Euler program, the following code doesn’t actually concern it much. I want to add 50 100-digit numbers, and I’m assigning each digit of each number to an element in the array addends[100][50]. I’d then add up each digit/place individually, and carry over extra digits. The numbers are being read in from a text file named Input.txt, and it merely contains all the numbers. http://projecteuler.net/problem=13

I’m having trouble assigning characters to elements of a string array (string numbers[100][50]) from a file input stream (<fstream>). The problem is described more completely in the comments:

“[for the 1st loop] This loop assigns a number to every string in the string array. Even though the second number (50) doesn’t do anything (it seems to be overridden by std::string; see variable declaration), it needs to be there for the loop to work. Same “logic” for the loop; “j” doesn’t do anything but needs to be there for the loop to work?”

And also, (for the 2nd loop) “This loop fills in the “addends[100][50]” array from the corresponding string array element. If I try to call “char_to_int()” with the array”numbers[i][j]”, the compiler complains that the input isn’t of the right data type. Adding a variable “k” makes the loop work for one run, but eventually crashes on the second loop (using “numbers[i][j][k]”). So I tried “char_to_int((numbers[i][j]).c_str())”, but the compiler complains that “const char *” is incompatible with “char”. Adding a pointer resolves the issue (“char_to_int( *( (numbers[i][j]).c_str() ) )”), but the program still crashes later.” I took out some code that doesn’t matter to make it more readable.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int char_to_int(char chInput);

int main()
{

    int placeholder;    //so console doesn't close immediately upon finish
    int sum[102] = {0}; // 100+2, 100 places + 2 places from carrying over
    int addends[100][50] = {0};
    string numbers[100][50];
    ifstream input("Input.txt");

    /* This loop assigns a number to every string in the string array. Even
     * though the second number (50) doesn't do anything (it seems to be
     * overridden by std::string; see variable declaration), it needs to be
     * there for the loop to work. Same "logic" for the loop; "j" doesn't do
     * anything but needs to be there??? Confused :-\
     */
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 1; j++)
            getline(input, numbers[i][j]);

    /* This loop fills in the "addends[100][50]" array from the corresponding
     * string array element. If I try to call "char_to_int()" with the array
     * "numbers[i][j]", the compliler complains that the input isn't of the
     * right data type. Adding a variable "k" makes the loop work for one run,
     * but eventually crashes on the second loop (using "numbers[i][j][k]").
     * So I tried "char_to_int((numbers[i][j]).c_str())", but the compiler
     * complains that "const char *" is incompatible with "char". Adding a
     * pointer resolves the issue ("char_to_int( *( (numbers[i][j]).c_str() ) )"),
     * but the program still crashes on the second loop through.
     */
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 50; j++)
            for (int k = 0; k < 1; k++) //used when the variable "k" was being used
                addends[i][j] = char_to_int( (numbers[i][j]).c_str() );

    return 0;
}

The code isn’t finished; I decided against going on since I (obviously) need to fix this first.

  • 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-06-01T07:56:56+00:00Added an answer on June 1, 2026 at 7:56 am

    It compiles and runs fine with

    string numbers[100];
    
    for (int i = 0; i < 100; i++)
            getline(input, numbers[i]);
    
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 50; j++)
                addends[i][j] = char_to_int( (numbers[i][j]));
    

    after removing the stdafx.h include and defining char_to_int.

    A std::string contains an array of characters itself, so you only need a one-dimensional array of std::strings. You can then access the characters of a string by [] indexing,

    numbers[i][j]
    

    gets the j-th character (byte, rather) of the i-th string in the array numbers.

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

Sidebar

Related Questions

When running the below code a type is never returned, despite there being a
First let me state that, despite being a fairly new practitioner of TDD, I'm
Despite primarily being a windows user, I am a huge fan of rsync. Now,
Despite this being one of the best error messages I've ever seen (second only
Despite an earlier question ( asked here ), our project is constrained to using
Despite being in the /public directory, if I visit http://site.example.com/favicon.ico I get the 404
I'm trying to send an email via some C# ASP.NET code. This is being
I am building a simple Scala project with SBT 0.11. All the code files
Despite this project having worked for me for a while, I'm now getting an
I am trying to add the LVL library project to my application's project. It

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.