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

hi find the error and send me the correct running code. in the same piece of code

P: 1
#include<iostream.h>
#include<conio.h>

void main()
{
char buffer[max];

clrscr();
ofstream out ("OUTPUT", ios::in|ios::out);

if(!out)
{
cerr<<"Open failed\n";
exit(1);
}
cout<<"Enter a line of data:";

while(cin.get (buffer, max) == eof())
{
put<<buffer <<endl;
cout<< "Next line: ";
}

getch();
}
Jun 20 '07 #1
Share this Question
Share on Google+
4 Replies


Meetee
Expert Mod 100+
P: 931
#include<iostream.h>
#include<conio.h>

void main()
{
char buffer[max];

clrscr();
ofstream out ("OUTPUT", ios::in|ios::out);

if(!out)
{
cerr<<"Open failed\n";
exit(1);
}
cout<<"Enter a line of data:";

while(cin.get (buffer, max) == eof())
{
put<<buffer <<endl;
cout<< "Next line: ";
}

getch();
}
Use code something like this

Expand|Select|Wrap|Line Numbers
  1. .#include<iostream.h>
  2. #include<fstream.h>
  3. //don't use conio.h in cpp file
  4.  
  5. int main() //new style programming has int as the type of main
  6. {
  7.         int max = 5; //define max
  8.         char buffer[max];
  9.  
  10. //      clrscr(); //don't use this also
  11.         ofstream out ("OUTPUT", ios::in|ios::out);
  12.  
  13.         if(!out)
  14.         {
  15.                 cerr<<"Open failed\n";
  16.                 exit(1);
  17.         }
  18.         cout<<"Enter a line of data:";
  19.  
  20.         while(cin.get (buffer, max) == eof())
  21.         {
  22.                 put<<buffer <<endl;
  23.                 cout<< "Next line: ";
  24.         }
  25.  
  26.        return 0;
  27. }
Hope this helps
Jun 20 '07 #2

archonmagnus
100+
P: 113
You can use "conio.h", but it will only work for Windows/DOS prompts. It is generally not recommended, but it can work if used in the right context.

Also, if you are attempting to right the code in ANSI/ISO C++, then you should use "#include <iostream>" rather than "#include <iostream.h>"
Jun 20 '07 #3

P: 15
This is working good:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. const MAXBUF = 256;
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10.     char buffer[MAXBUF];
  11.  
  12.     ofstream out ("OUTPUT");
  13.  
  14.     if(!out)
  15.     {
  16.     cerr<<"Open failed\n";
  17.     exit(1);
  18.     }
  19.     cout<<"Enter a line of data:";
  20.  
  21.     while(cin.getline(buffer, MAXBUF-1))
  22.     {
  23.         if (!stricmp(buffer, "q")) {
  24.             break;
  25.         }
  26.  
  27.         out<<buffer <<endl;
  28.         cout<< "Next line: ";
  29.     }
  30.  
  31.  
  32.     return 0;
  33. }
  34.  
  35.  
Cheers

#include<iostream.h>
#include<conio.h>

void main()
{
char buffer[max];

clrscr();
ofstream out ("OUTPUT", ios::in|ios::out);

if(!out)
{
cerr<<"Open failed\n";
exit(1);
}
cout<<"Enter a line of data:";

while(cin.get (buffer, max) == eof())
{
put<<buffer <<endl;
cout<< "Next line: ";
}

getch();
}
Jun 20 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Lotta errors here:
cout<<"Enter a line of data:";

while(cin.getline(buffer, MAXBUF-1))
{
if (!stricmp(buffer, "q")) {
break;
This is a _tmain. You can't use char literals or funcitons like stricmp. You need the TCHAR equivalents:

Expand|Select|Wrap|Line Numbers
  1. cout<<_T"Enter a line of data:";
  2.  
  3.     while(cin.getline(buffer, MAXBUF-1))
  4.     {
  5.         if (!_tstricmp(buffer, _T"q")) {
  6.             break;
  7.  
etc...

Otherwise, you can't switch between char and wchar_t. Even in the above code I don't know what the TCHAR equivalent is for cout. It has to toggle between cout and wcout.

If oyu can't use the TCHAR macros, you shoul sjust have a main() rather than a _tmain() so everyone knows the code is not UNICODE compliant.
Jun 20 '07 #5

Post your reply

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