std::string Array Element Access

Go To StackoverFlow.com

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.

2012-04-04 22:04
by Ernest3.14
Is there a reason to not use C++ STLs like vector, map, etc instead of an array (and access them using iterators later) - enthusiasticgeek 2013-12-03 12:49


3

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.

2012-04-04 22:23
by Daniel Fischer
Thanks! It does work. However, when I try to view strings other than the first one in the array, my "watch" window doesn't show them. I'm using Visual Studio Express 2010 - Ernest3.14 2012-04-05 00:27
That's odd. I can cout << numbers[i] << endl; without problems (but then, I'm using gcc on linux). How do you try to display the strings - Daniel Fischer 2012-04-05 00:30
In "debug mode", Visual Studio tells me (via the watch window) that numbers[0] is 3, numbers[1] is 7, and so on - Ernest3.14 2012-04-05 02:38
Hmm, each numbers[i] should be 50 digits long, since it's the result of a getline(). What does it show if you cout << numbers[i] << endl; for a couple of i - Daniel Fischer 2012-04-05 10:17
Ads