Garbage ? or buffer idk

+2 Mukul Chauhan · April 27, 2015
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;
 main()
{
 int i,j,k,num;
 char name[20],capital[20];
 cout<<"Enter the number of countries : ";
 cin>>num;
 cin.get();
 ofstream fout1("country.txt");
 ofstream fout2("capital.txt");
 for(i=1;i<=num;i++)
 {
  cout<<"\n-------------------------------------------\nEnter Details of country "<<i<<endl<<"-------------------------------------------"<<endl;
  cout<<"Enter country name : ";
  cin.getline(name,20);
  fout1<<name<<"\n";
  cout<<"Enter capital name : ";
  cin.getline(capital,20);
  fout2<<capital<<"\n";
  cout<<endl;
//  cin.get();
 }
 fout1.close();
 fout2.close();
 char a[20];
 ifstream fin1("country.txt");
 ifstream fin2("capital.txt");
 for(i=1;i<=num;i++)
 {
 cout<<"\n-------------------------------------------\nDetails of country"<<i<<endl<<"-------------------------------------------\n";
 cout<<"Country name : ";
 j=0;
 while(fin1!=0)
 {
fin1.get(a[j]);
j++;
if(a[j]=='\n')
{
break;
}
 }
 cout<<a<<endl;
 cout<<"Capital name : ";
 j=0;
 while(fin2!=0)
 {
fin2.get(a[j]);
j++;
if(a[j]=='\n')
{
break;
}
 }
 cout<<a;
 }
}




Program is working fine till the creation of files and properly saving the input in it.

Problem is : there is some random garbage along with the output.

/images/forum/upload/2015-04-27/f303deb7f4c60e405f5fba584fa96830.png

Post a Reply

Replies

Oldest  Newest  Rating
+1 Mukul Chauhan · April 27, 2015
@Scott program is running but look at the output i posted in image..

output is coming right but some kind of garbage is coming along with it,
Reply asap
0 Scott Walker · April 27, 2015
Just copied your code and it works fine?

Only warning is unused variable "k".

Files are saved and has stored the data.
  • 1

C++

107,267 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator