LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2013, 01:45 AM   #1
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Rep: Reputation: 17
Mysterious error with a previously working program


I did this program last night and ran it. It worked alright and gave the required output. But now I return in the morning and it isn't working!
Basically it is supposed to create a file named record.txt and populate it with the given info. then read the file and display the info back on the screen. It did that last night but I can't get it to work today. I'm using g++ and couldn't understand what went wrong. I never altered the program. Its the same one from last night so if any one can point out what should I do, I'd be great ful. Here's the code
Code:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

/**************************
**   Class Decleration   **
***************************/
class StudentInfo
{
 
    char* VUID;
    char* campusID;
    char* Studentname;
    char* Fathername;

	public:
    StudentInfo(char* vu_Id, char* campus_Id, char* student_Name, char* father_Name)
    {
        cout << "Parameterized constructor is called." << endl;
        
        setVUID(vu_Id);
        setcampusID(campus_Id);
        setStudentname(student_Name);
        setFathername(father_Name);
    }
  
    void setVUID(char* vu_Id)
    {
        VUID = new char[strlen(vu_Id) + 1];
        strcpy(VUID, vu_Id);
    }
    void setcampusID(char* campus_Id)
    {
        campusID = new char[strlen(campus_Id) + 1];
        strcpy(campusID, campus_Id);
    }
    void setStudentname(char* student_Name)
    {
        Studentname = new char[strlen(student_Name) + 1];
        strcpy(Studentname, student_Name);
    }
    void setFathername(char* father_Name)
    {
        Fathername = new char[strlen(father_Name) + 1];
        strcpy(Fathername, father_Name);
    }
    void Storefile()
    {
        cout << "The File has been saved as <record.txt>" << endl;
        
        ofstream outFile;
        char* outputFileName = "record.txt";
        outFile.open(outputFileName, ios::out);
        if(!outFile)
        {
            cout << "\n Unable to open file." << endl;

        }
        else
        {
       
            outFile << VUID;
            outFile << endl;
            outFile << campusID;
            outFile << endl;
            outFile << Studentname;
            outFile << endl;
            outFile << Fathername;

        }
    }
    void Display()
    {
         
        ifstream inputStream;
        char* fileName = "record.txt";
        inputStream.open(fileName);
        if(!inputStream)
        {
            cout << "\nCan't Open the file for read";
        }
        else
        {
         
            char line[100];
            inputStream.getline(line, 100);
            setVUID(line);
            inputStream.getline(line, 100);
            setcampusID(line);
            inputStream.getline(line, 100);
            setStudentname(line);
            inputStream.getline(line, 100);
            setFathername(line);
            cout << "The Data Stored is :" << endl;
            cout << VUID << endl;
            cout << campusID << endl;
            cout << Studentname << endl;
            cout << Fathername;
            cout << endl;
        }

    }

}

/***************************
* The Program Begains Here**
****************************/

int main()
{
  
    StudentInfo studentInfo("mc130202735", "Bqta02", "Nabeel Abbas", "Sammar abbas");
    studentInfo.Storefile();
    studentInfo.Display();
    return 0;
}
 
Old 06-27-2013, 02:20 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
after short reading
the code you posted misses a ; after the class , so it will not even compile.
you do a lot of new without ever freeing memory, that's bad
string constants are const char* and not char*

it does not work is not a problem descriptioin, what is the problem? terminates, does not compile, ...?
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysterious 403 error intramaweb Linux - Software 4 06-01-2006 05:42 PM
Mysterious Error lucky6969b Programming 7 03-20-2006 07:39 PM
No sound on XviD videos after working fine previously in KnoppMyth? davidbix Linux - General 0 11-20-2005 07:24 PM
Broke previously working WIFI with new wlan-ng drivers--how do I remove them? ming0 Linux - Software 8 05-20-2004 09:18 AM
Previously working vnc... no longer working marsonist Linux - Networking 1 01-21-2004 07:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration