By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

How to get garbage value when using int phonenumber in C++?

P: 1
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

const char fileName[50] = "c:\\Users\\user\\Desktop\\lab4prac\\data.txt" ;

void openfile(ofstream& outstream);
void closefile(ofstream& outstarem);

void writeInformation(ofstream& outstream, char firstName[], char lastName[], int phoneNumber);
void readInformation(char searchString[]);
void read();

bool checkInformation(char lastName[], char searchString[]);

bool checkInformation (char lastName[], char searchString[])
{
for (int i= 0; i<sizeof(searchString); i++)
{
if (lastName[i] != searchString[i])
return false;
else
continue;
}
return true;
}

void openFile(ofstream& outstream)
{
outstream.open(fileName, ios::app);
}

void closeFile(ofstream& outstream)
{
outstream.close();
}

void writeInformation(ofstream& outstream, char firstName[], char lastName[], int phoneNumber)
{
outstream<<firstName<<"\t"<<lastName<<"\t"<<phoneN umber<<endl;
}

void readInformation(char searchString[])
{
char firstName[10], lastName[10];
int phoneNumber;
bool recordFound = false;
ifstream instream;
instream.open(fileName);
instream>>firstName>>lastName>>phoneNumber;
while(!instream.eof())
{
if(checkInformation(lastName, searchString))
{
cout<<"\nFirst Name and Last Name : "<<firstName<<"\t"<<lastName<<endl;
cout<<"\nPhone Number : "<<phoneNumber<<endl;
recordFound = true;
break;
}
instream>>firstName>>lastName>>phoneNumber;
}
instream.close();
cout<<"Reading Completed ...."<<endl;

if(recordFound == false)
{
cout<<"Record not Found ... "<<endl;
}
}

void read()
{
char firstName[10], lastName[10];
int phoneNumber;

ifstream instream;
instream.open(fileName);
if(instream.fail())
{
cout<<"Invalid file or file is corrupted!!"<<endl;
exit(1);
}
else
{
int counter = 0;
instream>>firstName>>lastName>>phoneNumber;
while(!instream.eof())
{
cout<<"\nFirst Name and Last Name : "<<firstName<<"\t"<<lastName<<endl;
cout<<"\nPhone Number : "<<phoneNumber<<endl;
counter ++ ;
instream>>firstName>>lastName>>phoneNumber;
}
cout<<"Reading Completed : \n";
}
instream.close();
}

int _tmain(int argc, _TCHAR* argv[])
{
int choice;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

ofstream outstream;
openFile(outstream);

cout<<"\n\t 1: Write Date to File \n";
cout<<"\t 2: Read Data from File \n";
cout<<"\t 3: Search Data from file using Last Name \n";
cout<<"\t 4: Quit \n";
cout <<"Your Choice: ";
cin>>choice;

switch(choice)
{
case 1:
int phoneNumber;
char firstName[10], lastName[10];

cout<<"Enter the First Name : ";
cin>>firstName;
cout<<"Enter the Last Name : ";
cin>>lastName;
cout<<"Enter the Phone Number : ";
cin>>phoneNumber;
writeInformation(outstream, firstName, lastName, phoneNumber);
cout<<"Writing Completed ... "<<endl;
break ;

case 2:
read();
break;

case 3:
cout<<"Enter the Last Name to search from the file : ";
cin>>lastName;
readInformation(lastName);
break;

case 4:
cout<<"\nGOOD BYE !!!! \n";
break;
closeFile(outstream);
}

system ("PAUSE");
return 0;
}
Nov 28 '10 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,059
Is this some kind of smart program that is use full to viewer or you have a question??
Nov 29 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.