Hi again! is there any way a program can read a .txt file without typing the .txt extension name? I need this for my ATM program for accessing an account number, it wouldn't look right if i have type 1234667.txt. I'm using Borland C++. Hope you can help me! Thanks!

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <cstring.h>
#include <string.h>
#include <conio.h>

//using namespace std;

int main() {

      char account[80];

		//char string1[] = "jay";

      char string2[] = ".txt";

     	char string3[17];

      cout <<"Enter Account No.:";
      cin >> account;

      strcpy(string3, account);

     	strcat(string3, string2);


    int bal = 0;
    int x;
    ifstream inFile;

    inFile.open(account);
    if (!inFile) {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }

    while (inFile >> x) {
        sum = sum + x;
    }

    inFile.close();
    cout << bal << endl;
    getch ();
    return 0;
}

Edited 5 Years Ago by WaltP: added CODE Tags -- Please use them

Your program seems to be doing it correctly. But I don't see where you declared variable named sum.

Edited 5 Years Ago by Ancient Dragon: n/a

Oops sorry i forgot to edit it, its not working, i still have to type jay.txt, what i want to happen if possible is to just input jay and the text would append automatically

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <cstring.h>
#include <string.h>
#include <conio.h>

//using namespace std;

int main() {

char account[80];

//char string1[] = "jay";

char string2[] = ".txt";

char string3[17];

cout <<"Enter Account No.:";
cin >> account;

strcpy(string3, account);

strcat(string3, string2);


int bal = 0;
int x;
ifstream inFile;

inFile.open(account);
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}

while (inFile >> x) {
bal = bal + x;
}

inFile.close();
cout << bal << endl;
getch ();
return 0;
}

Edited 5 Years Ago by WaltP: added CODE Tags -- again

Line 26:
Ouput account, string2, and string3. See what the values are.

Also, are string2 and string3 useful names? What are they for? The answer will give you better variable names for them.

i did what you told me i output account, string3, string2 one by one, and string3 worked! thank you! i'm new at this and i really don't know what i'm doing, just trial and error, in my case mostly error. Here's my new code

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <cstring.h>
#include <string.h>
#include <conio.h>

//using namespace std;

int main() {

      char account[80];

	   char string1[] = ".txt";

     	char string2[17];

      cout <<"Enter Account No.:";
      cin >> account;

      strcpy(string2, account);

     	strcat(string2, string1);


    int bal = 0;
    int x;
    ifstream inFile;

    inFile.open(string2);
    if (!inFile) {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }

    while (inFile >> x) {
        bal = bal + x;
    }

    inFile.close();
    clrscr();
    cout << bal << endl;
    getch ();
    return 0;
}
This question has already been answered. Start a new discussion instead.