We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,371 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ -- The variable is being used without being initialized--FILE HANDLIN

CODE

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;

//---------------------------Function Prototypes----------------------------//
void titleheader();
void menu();
void addbook();
void display();

//--------------------------Main Function---------------------------------//


int _tmain(int argc, _TCHAR* argv[])
{
    int op;
    titleheader();
    menu();
    cout<<"\nPress option key to activate the required function: ";
    cin>>op;
    switch (op)
    {
    case 1:
        {
            system("cls");
            titleheader();
            cout<<"\n";
            addbook();  
            system ("pause");
            cin.get();
            system("cls");
            break;
        }

    case 2:
        {
            system("cls");
            titleheader();
            display();
            system ("pause");
            cin.get();
            system("cls");
            break;
        }

    default :
        {
            exit (0);
        }
    }
    getch();
}
//************************************ END of main() and Functions Bodies are given below. *********************************************\\

void titleheader()
{
    cout<<"\n ------------------------------------------------------------------------------ "<<endl;
    cout<<"                      **** Book Store Managment ****                             "<<endl;
    cout<<" ------------------------------------------------------------------------------\n"<<endl;
}
void menu()
{
    cout<<"1). Add a New Book. "<<endl;
    cout<<"2). Display all Records. "<<endl; 
}

void addbook()
{
    string bookname;
    string publisher;
    string author;
    int copies;
    int price;
    int edition;

    ofstream input("abc.txt",ios::app);
    cout<<"Enter Book name: ";
    cin>>bookname;
    cout<<"Enter Book Publisher name: ";
    cin>>publisher;
    cout<<"Enter Author/Writer name: ";
    cin>>author;
    cout<<"Enter Number of Copies : ";
    cin>>copies;
    cout<<"Enter Price: ";
    cin>>price;
    cout<<"Enter Edition: " ;
    cin>>edition;
    input<<bookname<<publisher<<author<<copies<<price<<edition;
    input.close();
}

void display()
{
    ifstream ofile("abc.txt");

    string bookname;
    string publisher;
    string author;
    int copies;
    int price;
    int edition;

    cout<<" Book Name: "<<bookname<<endl;
    cout<<" Publisher Name: "<<publisher<<endl;
    cout<<" Author Name : "<<author<<endl;
    cout<<" No. Of Copies: "<<copies<<endl;
    cout<<" Price: "<<price<<endl;
    cout<<" Book Edition: "<<edition<<endl;

    while (ofile>>bookname>>publisher>>author>>copies>>price>>edition)
    {
        cout<<bookname<<publisher<<author<<copies<<price<<edition;
    }
    ofile.close();
}

First of all I am a beginner at C++ . Whenever I use Display function an error comes up,which says "Run-Time Check Failure #3 - The variable 'copies' is being used without being initialized."....I don't Know much about file handling. I made this program with the help of a youtube tutorial:
https://www.youtube.com/watchv=p3m3qLNX9zA (https://www.youtube.com/watch?v=p3m3qLNX9zA) ..The purpose of this program is to ADD data and DISPLAY data by using file handling !! Please help me with it !!

3
Contributors
4
Replies
20 Hours
Discussion Span
5 Months Ago
Last Updated
5
Views
aslam.junaid786
Newbie Poster
7 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The compiler is right. You declare copies without initializing it, then immediately try to print its value. That's undefined behavior, and constitutes a bug.

deceptikon
Challenge Accepted
Administrator
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

Done so..Runtime Error removed..Now the inputs I am doing in file are getting saved on disk .. but when I print them on screen in display function T,they don't get printed..READING file in display function is not happening !!

aslam.junaid786
Newbie Poster
7 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The operations in addbook() (saving in file) and the operation in display() (loading from file) are not the inverse of each other, and thus, the loading will fail. Here is what I mean, say you have the following values for your variables:

string bookname = "C++ Primer";
string publisher = "Addison-Wesley Professional";
string author = "Stanley B. Lippman, Josée Lajoie, Barbara E. Moo";
int copies = 42;
int price = 45;
int edition = 5;

After you run the following code:

input << bookname << publisher << author << copies << price << edition;

The file will contain the following:

C++ PrimerAddison-Wesley ProfessionalStanley B. Lippman, Josée Lajoie, Barbara E. Moo42455

At this point, there is no way to extract the information back from the file because it is all jumbled up. You are going to need separators to mark where each field begins and where they end. One such marker is simply a new-line. If you output the data to the file in this format:

input << bookname << endl
      << publisher << endl 
      << author << endl
      << copies << endl
      << price << endl
      << edition << endl;

it will be a lot easier because the content of the file will now be:

C++ Primer
Addison-Wesley Professional
Stanley B. Lippman, Josée Lajoie, Barbara E. Moo
42
45
5

which is a lot easier to load, simply reading it line-by-line (see std::getline()).

mike_2000_17
21st Century Viking
Moderator
3,140 posts since Jul 2010
Reputation Points: 2,062
Solved Threads: 625
Skill Endorsements: 41

Sir mike_2000_17..Thankx a lot Sir its Workin !!

aslam.junaid786
Newbie Poster
7 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0847 seconds using 2.72MB