Sign in to follow this  

C++ help

This topic is 4134 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


std::string filename;
std::cout << "Enter the file name:" << std::endl;
std::getline(std::cin,filename);

if (!std::cin) return;

std::ifstream input(filename.c_str());

Share this post


Link to post
Share on other sites
Quote:
Original post by fishfin
why is there "std::" in front of "cout" is that nesisary for the code to work?


cout is in the namespace std. You can also use cout without the namespace qualifier by writing:

using std::cout;

Avoid the oft-cited but dangerous using namespace std;, which will pollute your global namespace.

Share this post


Link to post
Share on other sites
In modern C++, all symbols in the Standard C++ Library are placed within the std namespace. This helps minimize name collisions with your own symbols (the SC++L provides a find algorithm, for instance; without a namespace, you'd have to mangle your own function name for something so common).

Share this post


Link to post
Share on other sites
Quote:
Original post by fishfin
I got a compiler eror on this line:

if (!std::cin) return;


saying:

return-statement with no value, in function return 'int'


This code was meant as an example of usage, not code to be copy-pasted. Understand how it works, and rewrite it so it fits in your own program.

Share this post


Link to post
Share on other sites
#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string filename, text;

std::cout<<"What is the filename:";
std::getline(std::cin,filename);

std::ofstream file1 (filename.c_str());
std::cin>>text;

std::cout<< text;
}

I tried this and when I ran it nothing happened after I entered the filename, I had already created a file with that name and put some text in it...

Share this post


Link to post
Share on other sites
Your code does what you wrote it to do. It opens a file, then reads a word from standard input (not from the file) and writes it back to standard input.

Perhaps you intended to write to the file?

std::cin >> text;
file << text;

Share this post


Link to post
Share on other sites
the code:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string filename, text;

std::cout<<"What is the filename:";
std::getline(std::cin,filename);

std::ofstream file1 (filename.c_str());
std::cin>>text;

std::cout<< text;
std::cin.get();
}

I want the program to have you type in a file name (a .txt file) and then print the contents of that file.

what the program is doing is it lets you enter the file name, after you press enter nothing hapens except that the blinking underscore thing goes down to the next line.

Share this post


Link to post
Share on other sites
Quote:
Original post by fishfin
std::ofstream file1 (filename.c_str());
std::cin>>text;

std::cout<< text;
std::cin.get();
}


You never do anything with "file1" after it is created. Do you expect it to magically dump all its contents into "text" by calling cin?

I recommend you read the documentation for ofstream, cin, and cout.

Share this post


Link to post
Share on other sites
well you see this line:

std::cin>>text;

that reads from stdin ... the console keyboard in your case ... NOT the file ..

your whole understanding of the iostream library is somewhat mistaken -

1. cout is stdout, which in a console app is usually for writing text to the screen. (you use this correctly)
2. cin is stdin, which is used to read input from the keyboard in a console app. . You use getline the first time, which is also ok. The middle use of cin is incorrect.
3. oftream stands for "output file stream" which will open a file for writing ... you claim you want to READ the contents of an existing file and display them on the screen. For that you need ifstream "input file stream".

change:

std::ofstream file1 (filename.c_str());
std::cin >> text;

to:

std::ifstream file1(filename.c_str());
file1 >> text;

and you should see the first line of the file.

Share this post


Link to post
Share on other sites
so you are saying that it should be like this:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string filename, text;

std::cout<<"What is the filename:";
std::getline(std::cin,filename);

std::ifstream file1(filename.c_str());
file1>>text;

std::cout<< text;
std::cin.get();
}

Share this post


Link to post
Share on other sites
or like this:


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
string filename, text;

// get a filename from user
cout << "What is the filename:";
getline(cin, filename);

// read and display file
ifstream sourceFile(filename.c_str());
while(sourceFile) // can't remember for sure if this returns false at file end
{
getline(sourceFile, text);
cout << text;
}

// wait at end of program
cin.get();
}

Share this post


Link to post
Share on other sites

This topic is 4134 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this