cant keep command window open/iostream code not working

statements used for keeping window open not working; however, when it

Page 1 of 1

4 Replies - 2132 Views - Last Post: 25 November 2009 - 07:33 PM Rate Topic: -----

#1 thinktank!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-November 09

cant keep command window open/iostream code not working

Post icon  Posted 25 November 2009 - 04:39 PM

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
	
  ifstream hw9_cpp;
  char m;
  

  hw9_cpp.open("data9.txt");
   if(hw9_cpp.fail())
	{
	  cout<<"The file will not open.\n";
	  exit(1);
	  }
	  
  hw9_cpp.get(m);
  while (hw9_cpp>>m)
   {
	 cout<<m<<endl;
	 hw9_cpp.get(m);
	  
	 }
  
  
  hw9_cpp.close();
 
  
 system("pause");
 
  return 0;
  
}


Is This A Good Question/Topic? 0
  • +

Replies To: cant keep command window open/iostream code not working

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: cant keep command window open/iostream code not working

Posted 25 November 2009 - 04:53 PM

System("Pause") should really by avoided.

Instead use:
std::cin.get();


Hope I helped :D

This post has been edited by iphoneorange: 25 November 2009 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 Silhouette  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 25-November 09

Re: cant keep command window open/iostream code not working

Posted 25 November 2009 - 05:01 PM

View Postthinktank!, on 25 Nov, 2009 - 03:39 PM, said:

#include<iostream>
  hw9_cpp.get(m);
  while (hw9_cpp>>m)
   {
	 cout<<m<<endl;
	 hw9_cpp.get(m);
	  
	 }
}


Typically I just use something like this, and it works fine:

ifstream inStream;
int number;

inStream.open("myFile.txt");
while (inStream >> number)
{
   //do something with number
}



It seems like you are double-getting the numbers.

This post has been edited by Silhouette: 25 November 2009 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: cant keep command window open/iostream code not working

Posted 25 November 2009 - 05:19 PM

I think hes trying to read char by char

take a look
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
	
  ifstream hw9_cpp;
  char m;
  hw9_cpp.open("data9.txt");
   if(hw9_cpp.fail())
	{
	  cout<<"The file will not open.\n";
	  exit(1);
	  }

  while (hw9_cpp.get(m))
	 cout<<m<<endl;
  
  hw9_cpp.close();

  cin.ignore();
  cin.get();

  return 0;
  
}


This post has been edited by ImaSexy: 25 November 2009 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 thinktank!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-November 09

Re: cant keep command window open/iostream code not working

Posted 25 November 2009 - 07:33 PM

View Postiphoneorange, on 25 Nov, 2009 - 03:53 PM, said:

System("Pause") should really by avoided.

Instead use:
std::cin.get();


Hope I helped :D


Thank you guys for your help. Sorry I didn't put my post in more context. Didn't think I could write in this box where the code is posted.

What this code is to do is read from a file and print to screen. The contents from the file are characters, numbers, and white spaces. The output must be neat, as if I typed it. So yes, I have to read and print out char by char.

I am using Dev C++ as my compiler. That said, I was originally having problems with the output window staying open. I found a snippet on here, I believe, and used it. It worked! The code piece was:
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  std::cin.get();


Originally I used int k; cin>>k; and it never worked. Then found 3 others:

hw9_cpp.get();
getchar(); //window will close after pressing the a key (enter key).

while(true) //normally placed at top of code.
{
}

hw9_cpp.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
hw9_cpp.get();

None of them worked for me. Another interesting thing, one did work - I believe instream.get(); getchar();, but only once. When I compiled again, the window kept disappearing again. Does anyone know why that is?

Now I need to augment my code to read all the characters...it is skipping ever other one, or every few...so the output is jibberish. Any suggestions?

Another issue I would like to address is since I am using a compiler what is the right approach/method to ensure it opens a file in another drive (ie - c:\, f:\, m:\, etc)? For instance if I am using external storage, or from the computer, etc?

thank you in advance.......
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1