• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Reading numbers and characters from a file

I have a program that contains female and male student examination
results. The letter code f is used for female students and m for male students. Every file
entry consists of a letter code followed by an examination result. Each line has one entry. The number of entries
in the file is unknown. Write a program that computes and outputs the average examination result for both male
and female students. Prompt the user for the name of the file that contains the examination results that have to
be processed.
Example:
m 25
f 100
f 14
m 32


I have written the program structure as below. I need help in reading and manipulating the text from the file - i.e. in reading the marks line by line and computing them.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
int main()
{
    string line;
    ifstream mymarks ("marks.txt");
 
    if (mymarks.is_open())
    {
      while (getline (mymarks,line))
      {
       for (int i=0; i < line.length(); i++)
        {
       
            cout << line << endl;
       
        }
      }
      mymarks.close();
    }
    else cout << "Unable to open file";
     
      return 0;
}

Open in new window

0
emmagaya
Asked:
emmagaya
  • 4
  • 3
  • 2
1 Solution
 
Infinity08Commented:
Up till the while loop with getline, your code is ok.

Just the while loop body needs a bit more work.

What you need to do now, is extract the 'f' or 'm' character from the beginning (the first character of the line string). And after that, extract the integer value from the line.
atoi could help for this :

        http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

or alternatively, you could make use of a stringstream.

Once you have the gender, and the integer value, you can update the statistics data.
0
 
itsmeandnobodyelseCommented:
>>>> or alternatively, you could make use of a stringstream.

feed an istringstream with the input line and try to read from that stream like

 istringstream iss(line);  // feeds the iss with line
 if (iss >> gender >> age)
 {
       // coming here all is ok
 }
 else
 {
       // either gender or age was bad
  }

The istringstream needs <sstream> to include.

0
 
Infinity08Commented:
emmagaya,

When accepting an answer, it's important to choose those post(s) that answered the question (note that you can choose more than one post from more than one expert).

It is also important to choose an appropriate grade, which reflects the usefulness of the answer. If the question was fuly answered, then an A grade is appropriate. If something was missing, it's generally advised to continue working with the experts, and allow them to earn an A grade.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
emmagayaAuthor Commented:
The answer by itsmeandnobodyelse: gave me ideas but it did not fully help me solve the problem which I had. But thanks for the advice infinity08
0
 
Infinity08Commented:
You can still choose to continue this question. If you have further doubts/questions about this, we'll be glad to answer them.

Since it apparently did not fully help you solve the problem, there's no reason to close the question. After all, we're here to help you :)
0
 
emmagayaAuthor Commented:
Ok, thanks for that. I have tried using streams but did not manage getting the program to read the characters differently - the character first and then the number and then manipulate this data.

Thats were my main challange is.
0
 
Infinity08Commented:
Can you show the code you tried ?
0
 
itsmeandnobodyelseCommented:
Putting your code and mine together you should have somewaht like
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
 
 
int main()
{
    string line;
    ifstream mymarks ("marks.txt");
 
    if (mymarks.is_open())
    {
      while (getline (mymarks,line))
      {
       for (int i=0; i < line.length(); i++)
        {
       
            cout << line << endl;
            istringstream iss(line);  // feeds the iss with line
            int points = 0;
            char gender = ' ';
            if (iss >> gender >> points)
            {
              // coming here all is ok with reading
              if (gender != 'm' && 'gender != 'f') continue;
              // now you could store both in an array you defined outside
              //   arrMarks.push_back(pair<char, int>(gender, points));
              // or maybe you want to build the counts and sums like
              // if (gender == 'm') { count_m++; total_m += points; }
              // if (gender == 'f') { count_f++; total_f += points; }
            }
            else
            {
               // either gender or points was bad
               // ignore it
               continue;
            }
 
       
        }
      }
      mymarks.close();
    }
    else cout << "Unable to open file";
     
      return 0;
}

Open in new window

0
 
itsmeandnobodyelseCommented:
As it was an assignment I didn't give full code but only a strong hint.

If that was selected as an solution, a B grade was quite appropriate cause you would need some own efforts to solve the problem.

But if the question wasn't solved you shouldn't accept but ask more questions. Then, of course, if you finally got a full solution, an A grade was appropriate.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now