0

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.

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by firstPerson
0

//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.

0

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

Edited by mymyzzz: n/a

0

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

0

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.

Edited by firstPerson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.