What is wrong with the char?

This is a simple program to write to a file, it gets input from the cin command. how can i make the char variable hold a full sentence?
-----------------------------------------------------------------------------------------------------------------
this is the fix
-----------------------------------------------------------------------------------------------------------------
I used a string to hold the sentence and then used the getline command to find the whole thing since just using cin for it will stop after the first word/blankspace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// WriteToFile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string str1;

	ofstream myfile;
	myfile.open("testfile.txt");
	cout << "what do you want to write" << "\n"; 
	getline(cin, str1);
	myfile << str1 << "\n";
	myfile.close();

	return 0;
}
Last edited on
it's this (i think):
wchar_t
why do you want to use a (ms) wide char?

from here:
http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}
Last edited on
i need to use it because I read that it can hold more info, such as a sentence which is what i need. i have fixed the cin problem by using the command wcin instead but that takes the sentence and makes it a number.
Topic archived. No new replies allowed.