Zenadia Groenewald
Zenadia Groenewald

Reputation: 117

Trouble outputting a copied string

This has been giving me trouble for some time now and no adjustments I make to the code seem to make a difference. I am attempting to locate the digits in a line of text read from a file, and store said digits into another string to be used later. the initial copying seems successful, but when attempting to output the string the digits are stored in, the only output is a blank line.

Here is the code and included header files:

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

using namespace std;

int main()
{
    ifstream inFile;
    string temp;
    short count = 0;
    char fileName[20];  
    string info1;

    cout << "Enter the name of the file to be used: " << endl;
    cin >> fileName;

    inFile.open(fileName);

    if(!inFile)
    {
        cout << "Error opening file." << endl;      
    }
    else
    {           
        getline(inFile, info1);
        cout << info1 << endl;

        for(short i = 0; i < info1.length(); i++)
        {
            if(isdigit(info1[i]))
            {   
                temp[count] = info1[i];
                cout << temp[count] << endl;
                count++;
            }
        }
        cout << temp << endl;
    }   
    inFile.close();
    return 0;
}

And the output is as follows:

Enter the name of the file to be used:
input.txt
POPULATION SIZE: 30
3
0

Evidently, it is not outputting temp as expected. Any assistance or advice would be appreciated.

Upvotes: 0

Views: 69

Answers (3)

raina77ow
raina77ow

Reputation: 106375

Actually, it does output the temp value - only this value is an empty string. Consider this:

  string str = "A";
  for (int i=0; i < 2; i++)
  {
    str[i] = 'B';  
    cout << str[i] << endl;
  }
  cout << "And the final result is..." << str;

This will output two Bs (by the inner loop's cout), but the final result's string will be only a single B. The reason for this is that operator[] does not 'expand' a string - it can be used as a setter to replace that string's characters, but only for indexes that are in a string already: it won't allocate additional memory for that string in case of index overflow.

So to build your string, you may use another operator - += (concatenation assignment):

  string str;
  for (int i=0; i < 2; i++)
  {
    str += 'B';  
    cout << str[i] << endl;
  }
  cout << "And the final result is..." << str;

That will print BB as a final result.

Upvotes: 1

fasked
fasked

Reputation: 3645

The problem is that temp isn't simple char array. It is std::string class. And initially temp is empty. It means we don't know how much memory was allocated for string. It can be even 0. So reference on what symbol should be returned, when you use std::string::operator[] as applied to empty string?

You should use std::string::operator+= or char array instead.

Upvotes: 1

Srinivas
Srinivas

Reputation: 1790

Use this,
temp+=info1[i];
instead of
temp[count] = info1[i];

Upvotes: 0

Related Questions