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
#include <iostream>
#include <fstream>
usingnamespace 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;
}
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.
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:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
usingnamespace 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";
constint 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.