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;
}

Recommended Answers

All 6 Replies

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

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;
}

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.

wow thanks!!!!! working now!!!!!

What fixed it?

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.