Question:
c++ getting inputs from a file with different data types (int and string)?
chris
2009-09-27 08:44:29 UTC
Hi all,

I am trying to read from a file that has data types int and string(c++), the file data looks like this:

002345467
Mary Jane
34

004566577
Prescott Schultz
86

003455675
Allison Dold
88

003455676
Brian Cantrell
87

The list goes on for a while. It is the student ID, name and score of students (all fictitious btw). The problem I am having is I am trying to assign each of these values to a declared variable in the main program but do not know how to assign variables of different data types from a file. Can anyone point me in the right direction? Thanks in advance
Three answers:
anonymous
2009-09-28 21:52:56 UTC
Hm....



















002345467

Mary Jane

34

These data types :

field-1 = int/long or string (i d like to string)

field-2 = name

field-3 = int



example:

class name{

public:

char text[30];

};



class data{

char ID[9];

name firstname;

name lastname;

int age;

};



#include

void(){

ifstream myFile;

myFile.open("data.txt");

data member;

while(!myFile.eof()){

myFile>>member.ID;

myFile>>member.firstname.text;

myFile>>member.lastname.text;

myFile>>age;

}

myFile.close();

}
?
2009-09-27 08:51:40 UTC
So long as the format of the file is consistent and reliable then there should be no problem, since you always know that data type you are reading on each line. If that's not the case then read each line as a string and do appropriate validation and conversion on the string.
hellyer
2016-12-04 11:22:33 UTC
The above output (incorrect output) I had, whilst the enter record grew to become into no longer created. in line with danger you need to upload a diagnostic saying, no rely if or no longer your enter record opened effectively. that's the output your software produces student call: Joe Doe attempt rankings: 87.50 89.00 sixty 5.seventy 5 37.00 ninety 8.50 widespread attempt score: seventy 5.fifty 5


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...