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>
usingnamespace 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!
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();
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.
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?