Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My filename is "bca.txt" and below is my file data

Name  salary  Department
john   12000     IT
jack   14000    sales



I write program to read this file in my file. First word is employee name.
I read first word in string variable and second word is salary.
I read salary in integer variable but integer do not read salary.

This is my code
C++
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <stdlib.h>

void main()
{
    clrscr();
    char  name[50];
    int sal;        // this creates problem 
    char dept[30];
    
    ifstream infile;
    infile.open("bca.txt");
    
    while(!infile.eof())
    {
        infile>>name>>sal>>dept;
        cout<<name<<"  "<<sal<<"  "<<dept<<endl;   
    }

    infile.close();
    getch();
}
Posted
v2
Comments
Andreas Gieriet 2-Jun-13 2:25am    
So, what is the question?
Cheers
Andi
Richard MacCutchan 2-Jun-13 3:05am    
You need to read the salary field into a string variable and then convert it to an integer with atoi(), or similar function.

Skip the first line before reading the rest of the data: the column header "salary" is not a number.
Cheers
Andi
 
Share this answer
 
v2
Indeed you have to drop the first line.

C++
// skip the head line
infile>>name>>name>>name;
while(!infile.eof())
{
  // read the record
  infile>>name>>sal>>dept;
  // drop if read the line fails
  if(infile.rdstate()) break;
  // display the readen data
  cout<<name<<"  "<<sal<<"  "<<dept<<endl;
}

good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900