Jump to content
  • Advertisement
Sign in to follow this  
MubasharIqbal

please correct the code

This topic is 4399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this program just write a single string e.g if i want to write "Mubashar Iqbal" without quotes , the program just write only Mubashar , how can i write whole name and search portion also not working, please help me to solve this. #include<iostream.h> #include<fstream.h> #include<conio.h> main() { char c; ofstream outfile; ifstream infile; char outputfilename[]="myfileout.txt"; char outputtext[1000]; cout<<"Please enter the sentence "; cin>>outputtext; outfile.open(outputfilename,ios::out); if(!outfile) { cout<<"Can't open input file named"<<outputfilename<<endl; exit(1); } outfile<<outputtext; outfile.close(); infile.open(outputfilename); if(!infile) { cout<<"Can't open"<<outputfilename<<endl; exit(1); } cout<<"please enter the string to be searched "; cin>>c; while((c=infile.get())!=EOF) { outfile.put(c); } getch(); }

Share this post


Link to post
Share on other sites
Advertisement
cin takes characters up to the first blank space character i believe. so you will only get the first word. i think you would have to append the second word if you were using cin. using .get() and .peek() should help you get whatever input there is character by character (including blank/white space characters) and then keep doing that until you hit '\n'.

i believe that would be one way to fix that. or, you could just use .getline(). that would work too. :)

Share this post


Link to post
Share on other sites
I would probably use cin.getline(); cin will quit after the first space.

Moving to For Beginners.

Share this post


Link to post
Share on other sites
I tried to correct your code (you have many errors), I wasn't able to decipher the meaning of the last bit of code. I suggest you to find another book/tutorial it looks like whatever you're learning from is written way before the C++ standard.

#include<iostream> // Without .h
#include<fstream> // Without .h
// Removed conio.h, it isn't even mentioned in the C++ standard
#include <ostream> // For std::endl
#include <string> // For std::string

// Add a return type (int)
int main()
{
// c not needed
//char c;

// Needs std:: prefixed because it's in namespace std, so does everything
// else in the standard header files, except macros.
std::ofstream outfile;
std::ifstream infile;
// I'll let this stay char[] for now, but think about using std::string
char outputfilename[]="myfileout.txt";
std::string outputtext; // Bug: what if the user enters more than 1000 chars? Lets instead use a variable length string
std::cout<<"Please enter the sentence "; // std::cout
// If you need a whole line, you should use getline instead
//cin>>outputtext;
std::getline( std::cin, outputtext); // If you want to end the sentence on something else than newline
// you can use a third parameter like this:
// std::getline( std::cin, outputtext,'.');
// Here the sentence will end when you type .

// You might thinkit about removing ios::out, has no purpose, also it's std::ios::out
outfile.open(outputfilename,std::ios::out);

if(!outfile)
{
// std::cout and std::endl
std::cout<<"Can't open input file named"<<outputfilename<<std::endl;

// Close files before exitting, not sure if this is strictly necessary.
if( outfile.is_open() )
outfile.close();
// Don't use exit, your objects won't get destroyed.
return 1;
}

outfile<<outputtext;
outfile.close();

infile.open(outputfilename);
if(!infile)
{
// std::cout and std::endl
std::cout<<"Can't open"<<outputfilename<<std::endl;
// Close files before exitting, not sure if this is strictly necessary.
if( infile.is_open() )
infile.close();
return 1; // return 1, not exit(1)
}
// std::cout and std::cin
std::cout<<"please enter the string to be searched ";
// You need a string, not a word, use the getline trick we used earlier.
// Also c is a char (one character) and we can't store a whole string in it.
std::string searched_string;
std::getline( std::cin, searched_string);

// Are you tring to read from infile and write to outfile? They are the same file and outfile is closed.

// A C++ way of pausing the program
std::cin.get();

//Lets return 0, just to make it clear
return 0;
}



Share this post


Link to post
Share on other sites
Quote:
Original post by supercat1
why not insert the line "using namespace std;" right after the includes?


Reason

Share this post


Link to post
Share on other sites
ah, something i didn't know. none of my books have mentioned that interestingly enough.

however, it does seem to add plenty of excessive typing to a project unless you use that decleration mentioned in the link or a way of quickly putting in the "std::" through autocomplete or other measures. do you, personally, really type "std::" or other namespace names in that manner in ever piece of c++ code you make (this is for my own edification and learning from a more experienced person)?

Share this post


Link to post
Share on other sites
Dear god, what language is that :(


// The .h versions of these headers are deprecated, and have been for several
// years now. Their contents are basically no longer guaranteed, and it's best
// to proceed as if they don't exist.
#include <iostream>
#include <fstream>
// Don't artificially pause your programs at the end.

// Represent textual data with an appropriate datatype.
#include <string>

using namespace std; // for convenience.

// main() must be declared to return int. As a special case, main() will return
// 0 if it reaches the end, so you do not need a 'return 0;' at the end.
int main() {
// The output stream is buffered, and needs to be flushed to ensure that
// text sent to the buffer is actually displayed fully on the console -
// even if you don't want a newline at the end.
cout << "Please enter the sentence: " << flush;
string outputtext;
getline(cin, outputtext); // will read the whole line.

// This name is a 'symbol', i.e. something that won't be changed or really
// operated on in a string-like way - it's just used in its entirety. So
// a char* is fine.
const char* outputfilename = "myfileout.txt";

// Make good use of constructors.
// Also note that C++ does not require you to put variable declarations at
// top of scope. Exploit this to avoid creating variables until you know
// everything you need for their constructors. This also reduces the distance
// you have to look for the declaration.
// Also, ios::out is implied for ostreams, so you don't need to bother...
ofstream outfile(outputfilename);
if (!outfile.good()) {
cout << "Can't open output file named " << outputfilename << endl;
exit(1);
}

outfile << outputtext;
outfile.close();

ifstream infile(outputfilename);
if (!infile.good()) {
cout << "Can't open " << outputfilename << endl;
exit(1);
}

// The input character wasn't actually being used for anything yet, so I
// didn't keep that part of the code around. This just reads and outputs
// the whole file, as before (although there are easier ways).
// You can't write back to the output file now, because (a) it was the
// same file and (b) you closed that ofstream. So I am writing back to the
// console.
char c;
while (infile >> c) {
cout << c;
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by supercat1
ah, something i didn't know. none of my books have mentioned that interestingly enough.

however, it does seem to add plenty of excessive typing to a project unless you use that decleration mentioned in the link or a way of quickly putting in the "std::" through autocomplete or other measures. do you, personally, really type "std::" or other namespace names in that manner in ever piece of c++ code you make (this is for my own edification and learning from a more experienced person)?


I do always type std::, this has many advantages, for instance auto-completion works better (if I type st my IDE doesn't suggest string, but if I type std::st it does). When you get used to it, it is so fast that it doesn't even matter (it's like asking if . and , takes too long to type). With some other nested or long namespaces I might used using, but then it will always only be in the scope of the current function. So far I have encountered one case where I felt the need to globally include a namespace, and that is boost::mpl::placeholders. If for instance I use boost boost::filesystem::basic_ifstream<T,A> then I tend to just "use" that single class. As long as your using directives are in the scope of a function they aren't that bad.

Share this post


Link to post
Share on other sites
what ide do you use? i'm using devcpp and i don't even know if it supports that style of autocomplete (because i have never tried that). i do know it has autocomplete bugs though, but i'm using beta5 (v4.9.9.2) in which case that may be a known error and something the development team is working on fixing.

anyways, thanks for the heads up!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!