hullo am sory but i have some thing trigaring my mind can you please help me out find the solution for this
1write a function
2write aprogram
"to readstudent's name ,examinationscore for 4 subjects ,compute their average score"
please help me out with that using c++
thanks AMIRSHAMI
V.V.Raman -7 Newbie Poster
hullo am sory but i have some thing trigaring my mind can you please help me out find the solution for this
1write a function
2write aprogram
"to readstudent's name ,examinationscore for 4 subjects ,compute their average score"
please help me out with that using c++
thanks AMIRSHAMI
Amirshami, I am sorry for my late response. Since you have not mentioned whether you want to read the information for all students or a student of your choice, I have included both the options.
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
void read_score(char* input_file)
{
string student_name;
string line_string;
double subject1;
double subject2;
double subject3;
double subject4;
double average;
ifstream input_stream;
input_stream.open(input_file);
while (!input_stream.eof())
{
subject1 = 0.0;
subject2 = 0.0;
subject3 = 0.0;
subject4 = 0.0;
average = 0.0;
char line_chars[256];
input_stream.getline(line_chars, 256, '\n');
// Make sure that we are not looking at a line with no character
if (strlen(line_chars) < 1)
continue;
// We do not know whether the name and numbers are
// comma-seperated or not. To be on the safer side,
// we will replace all commas with blank space.
int i = 0;
while (line_chars[i] != '\n' && line_chars[i] != '\0')
{
if (line_chars[i] == ',')
line_chars[i] = ' ';
i++;
}
line_string = string(line_chars);
int j = 0;
// Remove the non-numeric parts from the line.
// For that, first locate the index form which numbers begin.
do
{
student_name = line_string.substr(0,j);
j++;
}while((line_string.c_str()[j] < '0' ||
line_string.c_str()[j] > '9'));
// After locating the index, truncate the string object.
line_string = line_string.substr(j);
// We need to extract numbers from the stream. For that
// create a string stream using the remaining string object.
stringstream stream;
stream.str(line_string);
// Make sure that data is available in the line
if (! stream.eof())
stream >> subject1;
if (! stream.eof())
stream >> subject2;
if (! stream.eof())
stream >> subject3;
if (! stream.eof())
stream >> subject4;
cout << student_name << '\t';
// Set output to be of one decimal
cout << setprecision(3);
cout << subject1 << '\t'
<< subject2 << '\t'
<< subject3 << '\t'
<< subject4 << '\t'
<< (subject1 + subject2 + subject3 + subject4)/4 << endl;
stream.str("");
}
input_stream.close();
}
// Overloaded function to read information for a specific student
void read_score(char* input_file, char* name)
{
string line_string;
double subject1;
double subject2;
double subject3;
double subject4;
double average;
ifstream input_stream;
input_stream.open(input_file);
while (!input_stream.eof())
{
subject1 = 0.0;
subject2 = 0.0;
subject3 = 0.0;
subject4 = 0.0;
average = 0.0;
char line_chars[256];
input_stream.getline(line_chars, 256, '\n');
// Make sure that we are not looking at a line with no character
if (strlen(line_chars) < 1)
continue;
// We do not know whether the name and numbers are
// comma-seperated or not. To be on the safer side,
// we will replace all commas with blank space.
int i = 0;
while (line_chars[i] != '\n' && line_chars[i] != '\0')
{
if (line_chars[i] == ',')
line_chars[i] = ' ';
i++;
}
line_string = string(line_chars);
// Apply the filter for specific name
if (line_string.find(name) < 0 ||
line_string.find(name) > line_string.length())
continue;
int j = 0;
// Remove the non-numeric parts from the line.
// For that, first locate the index form which numbers begin.
do
{
//student_name = line_string.substr(0,j);
j++;
}while((line_string.c_str()[j] < '0' ||
line_string.c_str()[j] > '9'));
// After locating the index, truncate the string object.
line_string = line_string.substr(j);
// We need to extract numbers from the stream. For that
// create a string stream using the remaining string object.
stringstream stream;
stream.str(line_string);
// Make sure that data is available in the line
if (! stream.eof())
stream >> subject1;
if (! stream.eof())
stream >> subject2;
if (! stream.eof())
stream >> subject3;
if (! stream.eof())
stream >> subject4;
cout << name << '\t';
// Set output to be of one decimal
cout << setprecision(3);
cout << subject1 << '\t'
<< subject2 << '\t'
<< subject3 << '\t'
<< subject4 << '\t'
<< (subject1 + subject2 + subject3 + subject4)/4 << endl;
stream.str("");
}
input_stream.close();
}
int main(int argc, char* argv[])
{
switch (argc)
{
case 2:
// Get scores for all students
read_score(argv[1]);
break;
case 0:
// Never is this the case. The executable itself is an argument.
case 1:
return -1;
default:
// Both file name and student name are given
read_score(argv[1], argv[2]);
break;
}
return 0;
}
tux4life commented: You're providing him with free code ! +0
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.