Author Topic: Reading from an external data file in C++  (Read 6973 times)

USF_ENGstudent

  • Guest
Reading from an external data file in C++
« on: April 09, 2015, 05:17:10 pm »
Hello--

I'm working on a term project and I've encountered an issue with my program that neither myself nor my professor can fix.  First, the goal of my program is to give the user the option to perform mathematical operations (addition, subtraction, & multiplication) on a set of numbers.  The user is given the option to enter the numbers manually OR import data from an external file.  My program is working fine taking the user input, but it will read the data from my external file titled "in.dat."  However, when my professor runs the program on his computer, he's able to read the information from the external file via my program.  I am a mac user and my professor is a pc user, but we still cannot figure out why my code::blocks will read the external file when the program is run on his computer, but not my computer.  I am at a loss, any assistance will be appreciated.  See below for the specific file in my code (P.S. I've very new to programming; so my code is very elementary):

#include "complx.h"//refering to complx.h file in this program
using namespace std;

ifstream infile ("in.dat");//creates an object from ifstream class
                           //for file in.dat
#define ARRAY_SIZE 50 //where we define the size of the array [0-49]

void main_Menu(int, int);//to display first prompt for the user
void second_menu(int, int);
void (*functionPointer[2])(int, int); //pointing to each functions I want to use


int main()
{
    //VARIABLE DECLARATION PHASE
    int i = 0;
    int x = 0;
    int y = 0;
    int choice, choice2;
    complx tempComplx;
    complx in[ARRAY_SIZE];
    int num1, num2;
    int result;

    functionPointer[0] = main_Menu;
    functionPointer[1] = second_menu;

    (*functionPointer[0])(x, y);
    cin >> choice;


    while (infile >> in) //if there's an infile, this is where it
                            //is translated to the array 'in'
    {
        cout << in <<endl;
        i++;
    }

    while(choice != 3) {

        if(choice == 1) {

            (*functionPointer[1])(x, y);
            cin >> choice2;

            if(choice2 == 0) {
                cout << "Enter first number: \n" << endl;
                cin >> num1;
                cout << "Enter second number: \n" << endl;
                cin >> num2;
                result = num1 + num2;
                cout << "The sum is \n" << result;
            }
            else if(choice2 == 1) {
                cout << "Enter first number: \n" << endl;
                cin >> num1;
                cout << "Enter second number: \n" << endl;
                cin >> num2;
                result = num1 - num2;
                cout << "The difference is \n" << result;
            }
            else if(choice2 == 2) {
                cout << "Enter first number: \n" << endl;
                cin >> num1;
                cout << "Enter second number: \n" << endl;
                cin >> num2;
                result = num1 * num2;
                cout << "The product is " << result << "\n";
            }
            else if(choice2 == 3) {
                (*functionPointer[0])(x, y);
                cin >> choice;
            }
        }
        else if(choice == 2) {
            while (infile >> in) {
                cout << in <<endl;
                    i++;
            }
            (*functionPointer[1])(x, y);
            cin >> choice2;
            if(choice2 == 0) {
                complx result = in[0] + in[1];
                cout << result;
            }
            else if(choice2 == 1) {
                complx result = in[0] - in[1];
                cout << result;
            }
            else if(choice2 == 2) {
                complx result = in[0] * in[1];
                cout << result;
            }
            else if(choice2 == 3) {
                (*functionPointer[0])(x, y);
                cin >> choice;
            }
        }
    }
    cout << "**ADIOS AMIGOS**" << endl;

    return 0;
}

void main_Menu(int x, int y) {

    cout << "                                                                          " << endl;
    cout << "*****************************************" << endl;
    cout << "************* W E L C O M E *************     " << endl;
    cout << "*****************************************" << endl;
    cout << "                                                                " << endl;
    cout << "1 - Enter data manually                              " << endl;
    cout << "2 - Import data from external file                " << endl;
    cout << "3 - Exit Program                                        " << endl;
    cout << "                                                                " << endl;
    cout << "ENTER ONE OF THE MENU OPTIONS:           " << endl;

}

void second_menu(int x, int y) {

    cout << "                                                                          " << endl;
    cout << "*****************************************" << endl;
    cout << "*******What would you like to do?********        " << endl;
    cout << "*****************************************" << endl;
    cout << "                                                                          " << endl;
    cout << "0 - Addition                                                         " << endl;
    cout << "1 - Subtraction                                                    " << endl;
    cout << "2 - Multiplication                                                  " << endl;
    cout << "3 - Main Menu                                                     " << endl;
    cout << "                                                                          " << endl;
    cout << "ENTER ONE OF THE MENU OPTIONS:                     " << endl;

}


Offline stahta01

  • Lives here!
  • ****
  • Posts: 7785
    • My Best Post
Re: Reading from an external data file in C++
« Reply #1 on: April 09, 2015, 06:01:10 pm »
Post the full compiler log!!!!
Post the log when you try to run.

http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

You likely need to move where the data file is located.

It needs to be in the program/project working folder. This is normally the folder containing the executable file. 

Tim S.

C Programmer working to learn more about C++ and Git.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org