-
May 12th, 2013, 01:01 PM
#1
Read from file
Hello.
I want my program to read variables from file "input.txt":
Please choose the crystal lattice. Type 1 for BCC, 2 for FCC, and 3 for HCP: 2
Please enter lattice parameter (a): 1
For HCP please enter second lattice parameter (c): 1
Please enter the number of translated cells along X axis: 2
Please enter the number of translated cells along Y axis: 2
Please enter the number of translated cells along Z axis: 2
Moreover, I want program to read only the numbers after colon. How can I do it?
Code:
#include <iostream>#include <fstream> // to read from file
using namespace std;
int main ()
{
int latticeType;
int dimX, dimY, dimZ; // number of translated cells along each axis
float d; // lattice parameter
float h; // second lattice parameter for HCP
ifstream inputFile; // object of class 'ifstream' to read from it
inputFile.open("input.txt", ios::in); // open 'input.txt' only for reading
if (!inputFile){
cerr << "Unable to open input file";
return -1;
}
while (!inputFile.eof()){
inputFile >> latticeType;
cout << latticeType << endl;
if ( (latticeType!=1) && (latticeType!=2) && (latticeType!=3) ){
cerr << "Invalid value of lattice type" << endl;
return -1;
}
inputFile >> d;
cout << d << endl;
if (latticeType==3){
inputFile >> h;
cout << d << endl;
}
inputFile >> dimX;
cout << dimX;
inputFile >> dimY;
cout << dimY;
inputFile >> dimZ;
cout << dimZ;
}
inputFile.close();
return 0;
}
Last edited by ted_kingdom; May 12th, 2013 at 01:26 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|