i got to do this for my class and i am a total noob!

i don't know what to do from this point on.

first, i need to use fstream to do this.

3
Tracy       80  91  67
Dana        0   66  44
Scott       50  41  64
Jamie       82  90  87
Jennifer                    95  100 90
William     67  77  62
Wendy       90  96  84
Heather     80  91  86
John        100 100 99

here, i want the three to represent the number of score i have up here. is there any way to do it without using a char?

because i get like 51 when i use it as char.

also i want to be able to add these three numbers and divide them by three.

which means i need to read the name, and the scores differently.
i don't know how to!!!!

all i could use on fstream is like getline which gets the line where i want them to, but i can not do the math part of it.

here is what i have done so far.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    char num;
    int count;
    int dashCount;
    int scores;
    float average;
    string fileName;
    string name;
    ifstream myin;

    cout << "Please enter the file name that contains the student records: ";
    cin >> fileName;
    myin.open(fileName.c_str());

    myin.get(num);
    cout << "there are " << num << " scores per studentn";
    cout << endl;
    cout << "Student name ";


    count = 1;
    while (count <= 3)
    {
        cout << "test" << count << "  ";
        count++;
    }

    cout << "final    score   graden";
    cout << endl;

    dashCount = count * 8 + 31;
    count=1;

    while (count<= dashCount)
    {

        cout <<"-";
        count++;
    }

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

        }
        cout << endl;
    }

    cout << "The class average is " << average << endl;
    cout << "There are " << << " studentsn";





    myin.close();
return 0; 
}

i can not use anything higher than fstream, iostream, while loop, if-then-else. i cant use fuctions either.

please help out quickly. i need to turn this in today!!!!

Edited 3 Years Ago by Dani: Formatting fixed

This is what should be running through your head:

1. create an ifstream object
2. attempt to open a .txt file
3. perform error checking to see if file actually opened (may not exist)
4. If open, read in .txt file into <string> class containers
5. close the ifstream object (no longer needed)
6. using the <sstream> library, perform string-to-int convesions
7. perform desired math
8. display stuff to screen
9. exit program


Now turn your thoughts into c++

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;

int main()
{ 
     
     string student_name[20], test1[20], test2[20], test3[20];
     int iTest1[20], iTest2[20], iTest3[20], iAverage[20];

     //1.  create an ifstream object
     ifstream inFile;

     //2.  attempt to open a .txt file
     inFile.open("C:\\myprograms\\documents\\sample.txt");

     //3.  perform error checking to see if file actually opened 
     if(!inFile.open())
     {
          cout << "\a\nUnable to open file!  (does the file exist?) ";
          cin.get(); 
          exit(1);
     }

     //4.  If open, read in .txt file into <string> class containers
     int i=0;
     while(inFile >> student_name[i] >> test1[i] >> test2[i] >> test3[i])
     {
          i++;
     {

     //5.  close the ifstream object (no longer needed; free up resources)
     inFile.close();

     //6. perform string-to-int converstions
     i=0;
     do{  
        
               istringstream str_to_int(test1[i]);
               str_to_int >> iTest1[i];

               istringstream str_to_int(test2[i]);
               str_to_int >> iTest2[i];

               istringstream str_to_int(test3[i]);
               str_to_int >> iTest3[i];

               i++;

     }while(!test1[i].empty());

     //7.  perform desired math
     for(int i=0; i<test1[0].size(); i++)
     {
          iAverage[i] \= (iTest1[i] + iTest2[i] + iTest3[i]);
     }

     //8.  display stuff to screen
     i=0;
     do{
              cout << "\n\nName:  "  << student_name[i] << "     " 
                   << "\nTest #1:  " <<iTest1[i] << "     " 
                   << "\nTest #2:  " << iTest2[i] << "     " 
                   << "\nTest #3:  " << iTest3[i] << "     " 
                   << "\nAverage: "  << iAverage[i] << end;

               i++;

          }while(!student_name[i].empty());

//9.  exit program
return 0;
}

*Untested code - may contain easy to fix errors. (whipped this up in about 5 minutes)

Edited 7 Years Ago by Clinton Portis: yo' mamma.

This article has been dead for over six months. Start a new discussion instead.