I need to write simple program in C++!
I need to write program that read information from file,which contain <50,students name and respective score.My program need to calculate average score and print out students name,who get below average mark to screen!But,i don''t know how to use string to do it....
here's sample code:
***********************************************************
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
ifstream inData;
typedef basic_string<unsigned char> ustring;
string Name;
int iNum[50],i,point,sum_score,maxIndex,highestScore;
char iName;
point =1;
sum_score=0;

int average_score;

inData.open ("C:\\Documents and Settings\\GobiV\\Desktop\\studentresult.txt");
cout<<"No Student Name Score "<<endl;

for (i=0; i<50; i++)
{
inData>>Name>>iNum;
sum_score=sum_score+iNum;
average_score=sum_score/50;


cout<<left<<setw(5)<<point++;
cout<<left<<setw(15)<<Name;
cout<<right<<setw(5)<<iNum;
cout<<setw(12);
cout<<endl;

}
maxIndex=0;
for(i=0;i<50;i++)
if(iNum[maxIndex]<iNum)
maxIndex=i;
highestScore=iNum[maxIndex];


if(iNum>average_score)
{
cout<<strcpy(iName,Name);
}

inData.close ();

cout<<right<<setw(5)<<"\n\nSum of all students score is : "<<sum_score;
cout<<right<<setw(5)<<"\n\nAverage students score is : "<<average_score;
cout<<right<<setw(5)<<"\n\nHighest Students Score is : "<<highestScore;
cout<<endl;

return 0;
}

*********************************************************
Please help me???

>>typedef basic_string<unsigned char> ustring;
above is unnecessary. just use c++ string class. so you might as well delete that line.

>>inData>>Name>>iNum;
If there are spaces in the name then above will not work because extraction operator >> stops at the first space. In that case you need to use getline() to read the whole like then extract the last word as the number.

you will need an array of name strings so that they can be printed later in the program, then read name strings into this array similar to how yu read the iNum array.

std::string Names[50];

It is not necessary to calculate the average until after all reads are complete. Your program only needs to do that once, at the end.

Do not attempt to print out the names of those whose scores are below average until all 50 names have been read because the program doesn't know what the average is until then. After reading is finished, calculate the average then have another loop test each of the scores against this average and print the names when necessary.

Have a look at this:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream> 

using namespace std;

struct Record
{
    int num;
    string name;
    int score;
    Record(int n, string nam, int s):num(n),name(nam), score(s){}
};

int main()
{
    vector <Record> v;
    string tmp, name;
    int num, score;
    
    ifstream infile ( "studentresult.txt" ); 
    
    
    while ( getline ( infile, tmp ) )
    {
        stringstream os ( tmp );
        os >> num >> name >>score;
        v.push_back ( Record ( num,name, score ) );
        
    }
    double avg_score = 0;
    int i;
    
    for (i = 0; i < v.size(); i++)
    {
        avg_score += v[i].score;
    } 
    avg_score = avg_score / v.size();
    cout << "Average score is: " << avg_score << endl;
    
    cout << "Students who have score below average are: " << endl;
    for (i = 0; i < v.size(); i++)
    {
        if ( v[i].score < avg_score )
        {
            cout << v[i].name << endl;
        }
    } 
    
}

My test file contain records in format:
1 Name1 20
2 name2 23
...

I hope you might find it useful and you'll learn something new.
Cheers!

Have a look at this:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream> 
 
using namespace std;
 
struct Record
{
    int num;
    string name;
    int score;
    Record(int n, string nam, int s):num(n),name(nam), score(s){}
};
 
int main()
{
    vector <Record> v;
    string tmp, name;
    int num, score;
 
    ifstream infile ( "studentresult.txt" ); 
 
 
    while ( getline ( infile, tmp ) )
    {
        stringstream os ( tmp );
        os >> num >> name >>score;
        v.push_back ( Record ( num,name, score ) );
 
    }
    double avg_score = 0;
    int i;
 
    for (i = 0; i < v.size(); i++)
    {
        avg_score += v[i].score;
    } 
    avg_score = avg_score / v.size();
    cout << "Average score is: " << avg_score << endl;
 
    cout << "Students who have score below average are: " << endl;
    for (i = 0; i < v.size(); i++)
    {
        if ( v[i].score < avg_score )
        {
            cout << v[i].name << endl;
        }
    } 
 
}

My test file contain records in format:
1 Name1 20
2 name2 23
...

I hope you might find it useful and you'll learn something new.
Cheers!

Thank You so much!I really appreciate your help!Now,i have clear picture about the program structure!Ok.

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.