Reading from Input File

Hello all! I'm in the final stages of debugging a project, but I seem to have hit a wall and would greatly appreciate your assistance. As part of the project, I need to read numbers from an input file and store them into an integer array. There are two wrinkles, however: 1) the professor has forbidden us from including the <string> library, so I can't use the >> operator or stoi function, and 2) my reading tells me that the getline function, which the professor recommended we use to read data, cannot store data as integers. Are there any other methods I could use to read data from a text file and store it as an integer? Thanks in advance for your help!
the professor has forbidden us from including the <string> library, so I can't use the >> operator
The >> operator can be used with an input stream and an int, without use of the <string> header. Are you explicitly not allowed to use the >> operator under any circumstances, or are you simply not allowed to use <string>?

We need to know how your input file is formatted. Perhaps show us an example.
Last edited on
Hello SamIAm3,

I would start by posting the directions that you were given. Then any code you have so far.

The "getline" function that you can not use is for the "string" class. The other alternative is "cin.getline()" to store in a "char" array then you can use the function "atoi" for a C string to "int" conversion.

Without more to look at I not completely sure what is required.

Andy
Thank you both for responding! I'll start by answering Ganado's questions. The specific wording of the assignment is:

- If string objects are used or "#include <string>" is used, you will be penalized 20 points.
- If the stream operator ">>" is used to read information from the input file, you will be penalized 30 points. You can, however, use the stream operator to put information into the output file.


So unfortunately I'm not allowed to use the >> operator under any circumstances. I appreciate your advice! Moving on, here's an example input file:

John Doe 654321 92 99 37 73 87 95
Jane Shepherd 123456 78 86 91 64 90 76
Thane Krios 222222 88 77 91 66 82 93
Tali Normandy 112233 87 99 76 93 95 94


My assignment is to read from an input file the first and last names of several students, along with their ID# and 6 test scores. Then my program is to compute the average score, and output the data to an output file in the format Lastname, Firstname ID# average.

Since I've been using c-strings to store the names and IDs, I think I'll do as Andy suggested and store each test score in a c-string then use the atoi function to convert each c-string to an int to store in the array. Thanks very much to both of you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main ()
{
   const int SIZE = 100, BIG = 1000;
   const int NGRADES = 6;
   ifstream in( "data.txt" );
   char buffer[BIG];
   char firstname[SIZE], lastname[SIZE], id[SIZE];
   while ( in.getline( buffer, BIG ) )
   {
      strcpy( firstname, strtok( buffer, " " ) );
      strcpy( lastname , strtok( NULL  , " " ) );
      strcpy( id       , strtok( NULL  , " " ) );
      double sum = 0.0;
      for ( int i = 1; i <= NGRADES; i++ ) sum += atoi( strtok( NULL, " " ) );
      cout << lastname << ", " << firstname << " " << id << " " << sum / NGRADES << '\n';
   }
}


Doe, John 654321 80.5
Shepherd, Jane 123456 80.8333
Krios, Thane 222222 82.8333
Normandy, Tali 112233 90.6667

Last edited on
Topic archived. No new replies allowed.