i have this

Tracy 80 91 67

and it is read from a file, making it fstream.

i want the computer to reconize the numbers and add them and divide them, excluding the name.

how do i do it?

i can't use fuction so please make it easy.

Recommended Answers

All 4 Replies

//if the file is exactly like that then you can do this :

ifstream iFile("file.txt");
string str ;
float num1;
float num2;
float num3;

iFile >> str; //read the string
iFile >> num1 >> num2 >> num3; //read the number

If you wan't something else be clearer.

//if the file is exactly like that then you can do this :

ifstream iFile("file.txt");
string str ;
float num1;
float num2;
float num3;

iFile >> str; //read the string
iFile >> num1 >> num2 >> num3; //read the number

If you wan't something else be clearer.

i have multiple of these things, how would i do this in a while statement?
this is what i have done

while (myin)
{
getline(myin, name);
cout << name;
while (myin)
{

}
cout << endl;
}

1. create an ifstream object
2. attempt to open a .txt file
3. perform error checking to see if file even exists
4. read in the entire file to an array of strings
5. close the ifstream object
6. using the <sstream> library create a ostringstream object
7. perform string-to-int conversions on the elements of the string array you wish to use
9. perform desired operations "add and divide numbers"
10. exit program

i have multiple of these things, how would i do this in a while statement?
this is what i have done

while (myin)
{
getline(myin, name);
cout << name;
while (myin)
{

}
cout << endl;
}

Not quite, something like this :

while(iFile.isgood()){
 //get data
}

The get data could be of the form thats in my previous post.

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.