6 Replies - 796 Views - Last Post: 20 October 2013 - 07:55 PM Rate Topic: -----

#1 cgamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-October 13

How do i read data for this question?

Posted 13 October 2013 - 07:24 PM

Hi guys i am having trouble outputing data from a file.The input file is as follows:
ron weasley
20 15 18 19 12
ron dublin
12 13 21 23 32
bill hiley
12 23 23 24 23
phill rad
34 34 32 100 23
john narc
78 87 76 65 78
sophie dier
56 67 66 67 56
ron nickle
67 67 65 45 76
pat fill
56 77 89 87 77
alex mark
56 67 66 67 56
jeff horn
98 76 67 66 65
max patty
65 56 65 57 45
riley robinson
45 54 54 34 35
ron worn
35 78 79 67 67
johnathan bake
67 67 56 89 68
mariah gomez
65 65 57 76 67
sandra mat
76 78 67 76 78
jesse daun
78 78 67 88 78
robin davidson
57 78 89 76 78
matt jenner
78 78 56 99 45
danny solia
67 65 67 56 56

how do i cout this ?
heres my code so far

#include <iostream>
#include <fstream>
#include <string>
#include <climits>
#include <iomanip>

using namespace std;

const int num_students=20;
const int num_scores=5;

struct StudentType{
string name;
int scores [num_scores];
char grade;
float average;
};

void openfile (fstream &f);
void getdata (fstream &f,string name);
int main()
{
    StudentType student[num_students];
    string name;

    fstream f;

    openfile(f);
    getdata(f,name);

    return 0;
}

void openfile(fstream &f)
{
    string filename;

    cout<<"Enter filename:";
    cin>>filename;
    f.open(filename.c_str());
}

void getdata(fstream &f,string name)
{
int score;

for(int i=0;i<num_students;i++)
{
    getline(f,name);
    cout<<name<<endl;
   
for (int j=0;j<num_scores;j++)
{
f>>[i][j];
}
}
}




dont worry about average and grades and other things..i just need to know how to cout names and scores in the input file.Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How do i read data for this question?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: How do i read data for this question?

Posted 13 October 2013 - 11:03 PM

Isn't line 50 working for you to print out the name? If not you need to be more descriptive about what issues you are running into. Is it printing out the wrong thing? Is your program crashing?
Was This Post Helpful? 0
  • +
  • -

#3 cgamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-October 13

Re: How do i read data for this question?

Posted 13 October 2013 - 11:34 PM

Hi guys i am having trouble outputing data from a file.I usually dont ask others to help me with coding but i have been really stuck on this problem for a while and i really need help.so i would really appreciate if anyone from you can help.Ok before i post my code here is the information about the assignment:

Write a program that reads studentsí names followed by their test scores from a file. The program should output each studentís name followed by their test scores, the average test score, and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score.

Student data should be stored in a struct variable of type StudentType, which has four components: name of type string; scores, an array of type int (score is between 0 and 100), average of type float, and grade of type char. Suppose that the class has 20 students and further suppose there are five tests. Use an array of 20 components of type StudentType. Input comes from a file and you must get the name of the file from the user.

The input file will be in the following format:

FirstName I. LastName
t1 t2 t3 t4 t5
FirstName LastName
t1 t2 t3 t4 t5
The file alternates between student name and test scores. Note: There may be additional 'text' in the file after the test scores of the 20th student. Your program should NOT read this extra text.

Your program must contain at least the following functions:

A function to read the studentsí data into the array.
A function to convert the name into the format: "LastName, FirstName I." or "LastName, FirstName" if no middle initial present.
A function to assign the relevant grade to each student.
A function to find the highest average.
A function to output all student info in a formatted table
A function to print the names of the students having the highest average.


ok below is the input file i made and the code i have written so far..i am having trouble reading and outputting the scores from the below input file. how do i output scores?

The input file is as follows:
ron weasley
20 15 18 19 12
ron dublin
12 13 21 23 32
bill hiley
12 23 23 24 23
phill rad
34 34 32 100 23
john narc
78 87 76 65 78
sophie dier
56 67 66 67 56
ron nickle
67 67 65 45 76
pat fill
56 77 89 87 77
alex mark
56 67 66 67 56
jeff horn
98 76 67 66 65
max patty
65 56 65 57 45
riley robinson
45 54 54 34 35
ron worn
35 78 79 67 67
johnathan bake
67 67 56 89 68
mariah gomez
65 65 57 76 67
sandra mat
76 78 67 76 78
jesse daun
78 78 67 88 78
robin davidson
57 78 89 76 78
matt jenner
78 78 56 99 45
danny solia
67 65 67 56 56

how do i cout scores for each person ?
heres my code so far

#include <iostream>
#include <fstream>
#include <string>
#include <climits>
#include <iomanip>

using namespace std;

const int num_students=20;
const int num_scores=5;

struct StudentType{
string name;
int scores [num_scores];
char grade;
float average;
};

void openfile (fstream &f);
void getdata (fstream &f,string name);
int main()
{
    StudentType student[num_students];
    string name;

    fstream f;

    openfile(f);
    getdata(f,name);

    return 0;
}

void openfile(fstream &f)
{
    string filename;

    cout<<"Enter filename:";
    cin>>filename;
    f.open(filename.c_str());
}

void getdata(fstream &f,string name)
{
int score;

for(int i=0;i<num_students;i++)
{
    getline(f,name);
    cout<<name<<endl;
   
for (int j=0;j<num_scores;j++)
{
f>>[i][j];
}
}
}



Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: How do i read data for this question?

Posted 13 October 2013 - 11:39 PM

Do not open duplicate posts.
Was This Post Helpful? 0
  • +
  • -

#5 cgamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-October 13

Re: How do i read data for this question?

Posted 13 October 2013 - 11:43 PM

ok sorry..i just wanted to post more information as you said :stupid:
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 5459
  • View blog
  • Posts: 16,984
  • Joined: 25-December 09

Re: How do i read data for this question?

Posted 14 October 2013 - 07:10 AM

You may want to study the following tutorials: Arrays, Basic input and output, File input and output.

Quote

.i just wanted to post more information as you said


You do that by adding a post to your existing topic, not by opening a new topic.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: How do i read data for this question?

Posted 20 October 2013 - 07:55 PM

Hey, I made this for you.
You can add whatever features you want to it. This just displays the scores and names from a text file.
Posted Image
It uses vectors to store the students's information in a container.
#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>//Just for CLS
int main()
{
    std::ifstream buffer;
    std::string tmp,tmpvar;
    std::vector<std::string> student;
    std::vector<std::string> score;
    std::cout<<"Hello, please enter the text file name(e.g: students.txt):\n";
    std::getline(std::cin,tmp);
    buffer.open(tmp.c_str());
    while(!buffer.eof())
    {
        std::getline(buffer,tmpvar);
        student.push_back(tmpvar);
        std::getline(buffer,tmpvar);
        score.push_back(tmpvar);
    }
    buffer.close();
    std::vector<std::string>::iterator itr1=student.begin();
    std::vector<std::string>::iterator itr2=student.end();
    std::vector<std::string>::iterator itr3=score.begin();
    std::vector<std::string>::iterator itr4=score.end();
    for(int x=0;itr1<itr2;++itr1)
    {
        x++;
        std::cout<<"\n("<<x<<")"<<*itr1;
    }
    for(int x=0;itr3<itr4;++itr3)
    {
        x++;
        std::cout<<"\n("<<x<<")"<<*itr3;
    }
    std::cout<<"\nPress ENTER to restart.\n";
    std::cin.get();
    system("CLS");
    main();
}
/* scores.txt

ron weasley
20 15 18 19 12
ron dublin
12 13 21 23 32 
bill hiley
12 23 23 24 23
phill rad
34 34 32 100 23 
john narc
78 87 76 65 78 
sophie dier
56 67 66 67 56
ron nickle
67 67 65 45 76 
pat fill
56 77 89 87 77
alex mark
56 67 66 67 56
jeff horn
98 76 67 66 65
max patty
65 56 65 57 45
riley robinson
45 54 54 34 35
ron worn
35 78 79 67 67
johnathan bake
67 67 56 89 68
mariah gomez
65 65 57 76 67
sandra mat
76 78 67 76 78
jesse daun
78 78 67 88 78
robin davidson
57 78 89 76 78
matt jenner
78 78 56 99 45
danny solia
67 65 67 56 56*/


You're welcome!
~Ambitious

This post has been edited by Ambitious: 20 October 2013 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1