Search code examples
c++lnk2019

How to correctly use a void function with no input parameters in C++


I am quite new to C++. I need to make a small application which reads the content of a txt file and then displays the content in the console. I have three points forming a triangle that I will plot later on. I want to do all this operation in a function called read2dFile, so my main is actually empty (except for the call of the function). When I tried this code in a main in another project, everything was working fine. It seems like my function is not properly declared. Here is my code :

**Test.cpp** (FOR THE MAIN)

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "read2dFile.h"

int main()
{
    read2dFile();
    return 0;
}

read2dfile.h (FOR THE FUNCTION HEADER)

#ifndef READ2DFILE_H_
#define READ2DFILE_H_

#include <iostream>
#include <iomanip>
#include <array>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>

using namespace std;

void read2dFile();

#endif

read2dFile.cpp (FOR THE FUNCTION CODE)

#include "read2dFile.h"

int row = 0;
int col = 0;

void read2dFile() {

    string line;
    int x;
    int array[100][100] = { { 0 } };
    string filename;

    ifstream fileIN;

    // Intro
    cout
        << "This program reads the number of rows and columns in your data 
    file."
        << endl;
    cout << "It inputs the data file into an array as well." << endl;
    cout << "\nPlease enter the data file below and press enter." << endl;
    cin >> filename;

    fileIN.open(filename);

    // Error check
    if (fileIN.fail()) {
        cerr << "* File you are trying to access cannot be found or opened *";
        exit(1);
    }

    // Reading the data file
    cout << "\n" << endl;
    while (fileIN.good()) {
        while (getline(fileIN, line)) {
            istringstream stream(line);
            col = 0;
            while (stream >> x) {
                array[row][col] = x;
                col++;
            }
            row++;
        }
    }

    // Display the data
    cout << "# of rows ---> " << row << endl;
    cout << "# of columns ---> " << col << endl;
    cout << " " << endl;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cout << left << setw(6) << array[i][j] << " ";
        }
        cout << endl;
    }

}

Solution

  • I have some recommendations (your code compiles fine for me).

    1. Not specific to your question, but you should learn about class and object oriented programming to make life in C++ easier.
    2. These are the only include files that your read2dFile() function needs. Put them into read2dFile.cpp! The reason is because you don't want headers included in other headers unless it's absolutely necessary.

    These directives will go at the top of read2dFile.cpp

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <sstream>
    
    1. using namespace std; opens the floodgates and can cause namespace collisions. Try to avoid doing this. If you do insist on doing it anyway, do it in the .cpp source files (not in your .h header files). Instead, you can declare usage of specific parts of the standard namespace (only the ones you need).

    These using directives can go in place of your using namespace std; and you will again put them into the read2dFile.cpp source file.

    using std::string;
    using std::ifstream;
    using std::cout;
    using std::endl;
    using std::cin;
    using std::cerr;
    using std::istringstream;
    using std::left;
    using std::setw;
    
    1. Your read2dFile.h has now been reduced to just your function declaration.

    The file now looks like this.

    #ifndef READ2DFILE_H_
    #define READ2DFILE_H_
    
    void read2dFile();
    
    #endif
    

    Your main() can stay as it is, and if it's still not working for you, try removing the precompiled header directive #include "stdafx.h" from your Test.cpp source file. It's not needed here and sometimes can cause compiler errors in certain cases.