Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

c++ - filling 2D array with dummy data and input form txt file not working

hi so i need to fill an 2D array of dummy values like x with values form txt file, the "x" are there to fill in empty cols if file.txt has little or no data my file.txt looks like this

1 23 3 42 5 63 4 . 5
-2 1 43 95 55 5 43 2 -6
. 2 3 -4 5 43 -4 4 35
82 61 3 5 -5 65 . 2 6

my c++ looks like this

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main(){
    ifstream file;
    ofstream newfile;
    string filename;
    string text;
    const int n = 10;
    char x = 'x';
    double A[n][n];


    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            A[i][j] = x;
            cout << A[i][j];
        }
    }
    


    do{
        cout << "Podaj nazwe pliku(wraz z rozszerzeniem): ";
        cin >> filename;
        file.open(filename.c_str());
    }while (!file.is_open());

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (!file.eof()){
                getline(file, text);
                A[i][j] = stod(text, 0);
                
                
            }else{
                break;
            }
        }
    }
    file.close();

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
           cout << A[i][j]; 
        }
        cout << endl;
    }
}

when i print out this array i get output like 127.77098e-3184 i can't copy my input because it's not printing right now i don't know why


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem:

  1. When you use std::getline you take the whole line instead of taking one number.
  2. "." is not a valid argument for std::stod so it will throw an std::invalid_argument exception.

Solution:

  1. Use the std::ifstream::operator>> instead.
  2. Reformat the file and change the "." to the value it should represent (I guess it is 0). Even semantically a point by itself does not represent a number.

Additional information:

  1. using namespace std; is considered a bad practice.
  2. The newFile variable is unused.
  3. The x variable should be a double.
  4. The <cmath> header is unused.
  5. You may use std::array.

Full code:

file.txt

1 23 3 42 5 63 4 0 5
-2 1 43 95 55 5 43 2 -6
0 2 3 -4 5 43 -4 4 35
82 61 3 5 -5 65 0 2 6

main.cpp

#include <iostream>
#include <fstream>

int main(){
    std::string filename;
    std::string text;
    constexpr int n = 10;
    double x = -1;
    double A[n][n];

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            A[i][j] = x;
            std::cout << A[i][j] << " ";
        }
        std::cout << std::endl;
    }

    std::ifstream file;
    do{
        std::cout << "Podaj nazwe pliku(wraz z rozszerzeniem): "; //Insert filename.
        std::cin >> filename;
        std::cout << std::endl;
        file.open(filename);
    }while (!file.is_open());

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if(file >> text){
                A[i][j] = std::stod(text, 0);
            }
            else{
                break;
            }
        }
    }
    file.close();

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
           std::cout << A[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...