I need help with getline in the for loop,the loop can take the inputs from file in 1st loop,but it skipped the first getline after the 2nd loop...Any help will be appreciated...

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    string name;
    int num_semester;
    int num_subject;
    string student_id;
    string ic_number;
    string programme_name;
    string course_code;
    string course_name;
    string course_credit;

    ifstream infile;
    ofstream outfile;

    infile.open("C:\\Documents and Settings\\User\\Desktop\\dummy.txt");
    outfile.open ("C:\\Documents and Settings\\User\\Desktop\\example3.txt");


    cout << "\n\nPlease enter numbers of semester: ";
    cin >> num_semester;

                getline(infile,name);
                outfile << left << setw(25) << setfill(' ') << "Name :" 
                        << right << setw(8) << setfill(' ') << name << endl;

                getline(infile,student_id);
                outfile << left << setw(25) << setfill(' ') << "Student ID :" 
                        << right << setw(8) << setfill(' ') << student_id << endl;

                getline(infile,ic_number);
                outfile << left << setw(25) << setfill(' ') << "IC Number :" 
                        << right << setw(8) << setfill(' ') << ic_number << endl;

                getline(infile,programme_name);
                outfile << left << setw(25) << setfill(' ') << "Name of the Programme :" 
                        << right << setw(8) << setfill(' ') << programme_name << endl <<endl; 

    for ( int m=1 ; m <= num_semester ; m++)
    {
        cout << "\nPlease enter the number of subject in semester " << m << ": ";
        cin >> num_subject; 

            for ( int n=1 ; n <= num_subject ; n++)
            {
                double pointer[n];
                string grade;
                string meaning;
                double course_mark[n];

                getline(infile,course_code); 
                getline(infile,course_name);
                infile >> course_credit >> course_mark[n];
                cout << course_code;

                if (( course_mark[n] >= 80 ) && ( course_mark[n] <= 100));
               {
                pointer[n] = 4.00;
                grade = "A+";
                meaning = "Excellent"; 
                }
                if (( course_mark[n] >= 76 ) && ( course_mark[n] <= 79));
               {
                pointer[n] = 3.75;
                grade = "A";
                meaning = "Excellent"; 
                }

                outfile <<  course_code << "           " << course_name << "      " << setprecision(3)
                 << showpoint << pointer[m] << "           "  <<   grade << "    " << meaning << endl;

                     } 
                }  
                 getchar();
                 getchar();
                 return 0;
                 }

Recommended Answers

All 3 Replies

When you open a stream and then read to the end, it doesn't magically reset to the beginning at the programmer's convenience. In fact, it stays right where you last stopped reading. You need to clear the error state (because it will be marked as end-of-file) and reset the get pointer to the beginning any time you want to start reading the file from the beginning:

infile.clear();
infile.seekg(0, ios::beg);

Actually I want to continue reading the value from the file where the 1st loop have stopped,but during the 2nd loop,the getline(infile.course_code) is skipped...and course_name have taken the course_code value in the 2nd loop...:(

Your 1st loop 2nd loop terminology is almost unintelligible, but the only other interpretation I can think of is clearly and completely answered in this sticky.

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.