How to read the getline from txt into arrays?

HELP! I'm Stuck!
How to read the subject names(sn)from txt into arrays?
Besides, I also want to store the credit hour and the grade into 2 separate arrays.
I can not add a comma to txt file because I have to write a code that accept that particular txt file. Any help will be appreciated.

1
2
3
4
5
Computer Programming	3	C
Network Programming	4	A
Database Programming	4	B
Interface Design	2	B
Software Engineering	3	A


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ifstream indata;
	string sn;    // subject name
	double ch;    // credit hour
	char g;       // grade earned
	double gp;    // grade point
	indata.open("input1.txt");
	int num=0;
	
	while ( getline(indata,sn,'\t') )
	{
		indata >> ch; indata.ignore();
		indata >> g; indata.ignore();
		
		//What can I add here so that the names(sn) can be stored into an array?

                //what to do to store credit hour and grade to 2 different arrays?
	
	}

	
return 0;
}
Last edited on
I can not add a comma to txt file because I have to write a code that accept that particular txt file.

Seemingly, the input file sample you posted is delimited by the tab character '\t', so that is not an issue.

Here I store everything in a single array. Rather than using incomprehensible variable names and then adding explanatory comments, I just used the relevant naming from the start.

If you want three separate arrays, it will be almost the same, except line 33 will become three separate lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

struct Data {
    string subject_name;
    double credit_hour;
    char   grade_earned;    
};

int main()
{
    const int size = 20;
    Data array[size];
    

    string subject_name;
    double credit_hour;
    char   grade_earned;
    
    ifstream indata("input1.txt");
    
    int num = 0;
    
    while ( num < size && getline(indata, subject_name, '\t') && indata >> credit_hour >> grade_earned)
    {
    
        indata.ignore(10, '\n');  // discard rest of line
        
        array[num] = {  subject_name, credit_hour, grade_earned };
        
        num++;  
    }
    
    for (int i=0; i<num; ++i)
    {
        cout << left << setw(30) << array[i].subject_name << right
             << setw(4) << array[i].credit_hour
             << setw(4) << array[i].grade_earned
             << '\n';
    }
    
}

Last edited on
I got [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 when trying to compile the code you provided. Why is this happening?
The compiler is telling you that it may have the capability to compile that line of code, but it is configured (probably by default) to use an older version.

If you go to the menu and select Tools->Compiler Options there should be a tab named General
Tick the box labelled "Add the following commands when calling the compiler:" and type in the box:
 
-std=c++11


Recommended settings are something like
 
-std=c++11 -Wextra -Wall -pedantic-errors

(and if you have a more recent compiler it may accept c++14 or c++17 which are more recent updates to the standard).


However, if you use three separate arrays, you will want to change this line:
 
    array[num] = {  subject_name, credit_hour, grade_earned };

to something such as
 
    name_array[num] = subject_name;

and then it should compile even with the older version.


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;


//======================================================================


string trim( string s, string junk )                       // Remove unwanted leading and trailing characters
{
   int i = s.find_first_not_of( junk );
   if ( i == string::npos ) return "";

   int j = s.find_last_not_of( junk );
   return s.substr( i, j - i + 1 );
}


//======================================================================


int main()
{
   const string digits = "0123456789";
   const int SIZE = 48;                                    // 4-year degree; 12 modules per year
   string module[SIZE];
   int hours[SIZE];
   char grade[SIZE];
   string line, rest;
   int num = 0;


   ifstream in( "input1.txt" );
   while ( getline( in, line ) && num < SIZE )
   {
      int pos = line.find_first_of( digits );              // Index of first number
      module[num] = line.substr( 0, pos );                 // First part is the module name
      module[num] = trim( module[num], " \t" );            // Remove leading and trailing blanks or tabs

      rest = line.substr( pos );                           // Second part contains the numbers
      stringstream( rest ) >> hours[num] >> grade[num];    // Stringstream the rest of the line into variables

      num++;                                               // Index for next; also indicates number read
   }
   in.close();


   int wM = 25, wH = 6, wG = 6;
   cout << setw( wM ) << left << "Module" << setw( wH ) << "Hours" << setw( wG ) << "Grade" << '\n';
   cout << string( wM + wH + wG, '-' ) << '\n';
   for ( int i = 0; i < num; i++ ) cout << setw( wM ) << left << module[i]
                                        << setw( wH ) << hours[i] << setw( wG ) << grade[i] << '\n';
}

Module                   Hours Grade 
-------------------------------------
Computer Programming     3     C     
Network Programming      4     A     
Database Programming     4     B     
Interface Design         2     B     
Software Engineering     3     A     


Assumptions (before any nitpicking starts):
- The module name doesn't contain any digits; e.g. Computer Programming 1, etc.
- Hours is an integer, so no 2.5-hour programs etc.
- Grade is a single character, so no A+, N/A, NFC etc.
Last edited on
Registered users can post here. Sign in or register to post.