File Input help

I am trying to make a simple input file to save as a text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;
int main()
{  int m;
    cout << "This program allows you to store data in another location.";
 cout << "  ";
 cout << "Type all of what you want to save into the file";
 cout << "  ";
 system ("pause");
 ofstream myfile;
 myfile. open ("linkfile.txt", ios::in );
 myfile <<
 cin >> m;
 myfile. close ();
return 0;
}

I get the error
cpp:14: error: no match for 'operator>>' in '((std::basic_ostream<char, std::char_traits<char> >*)(&myfile))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](((const void*)(((std::basic_ios<char, std::char_traits<char> >*)(&std::cin)) + 8u)->std::basic_ios<_CharT, _Traits>::operator void* [with _CharT = char, _Traits = std::char_traits<char>]())) >> m'
Your help would be greatly appreciated!
myfile <<

And what exactly is being output to file there? You have to have something on the right that is being written to file.

I think you meant something like this:

1
2
3
4
5
6
7
8
9
cout << "Type all of what you want to save into the file";
 cout << "  ";
 system ("pause");
 ofstream myfile;
 myfile.open("linkfile.txt");
 string x;
 cin >> x;
 myfile << x;
 myfile.close();
Last edited on
Anything that the user would input would be written to the file.
and yes but now I get the error

cpp:14: error: conflicting declaration 'std::string x'

cpp:5: error: 'x' has a previous declaration as `int x'

cpp:14: error: declaration of `std::string x'

cpp:5: error: conflicts with previous declaration `int x'
That's because you have created an int called x, and a string called x. What are you using that int for? I remind you that an int object can hold a single number, which will be pretty useless if the user enters anything that isn't a number.
But wouldn't I need to have a value that I would need to initalize x for a my input?
No. You can create an object in C++ and not initialise it. What would be the point of giving x some value if you're just going to write over the top of it? The problem is not anything to do with initialising x. The problem is that you made an int called x, and then you made a string called x. You cannot have two different variables with the same name.
Alright Thanks for all the help! It compiled just fine now. But it won't allow me to put any spaces into the message that you input. Do you know how that could maybe be fixed?
Last edited on
use an if statement or a loop of some sort and just make it so it stops at a new line instead, if you know what I mean
Topic archived. No new replies allowed.