Writing to File Problem? [Noob Here]

Hi guys, I started learn c++ about 2 or 3 days ago. I'm getting the hang of the easy console stuff. I am running into an issue here though.

So I'm trying to create a program that allows one to read/write on to output.txt. I though I had everything set up right, but its only writing one word to the text file. Heres the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdafx.h"
#include <iostream> //Needed for User Input
#include <fstream> //needed for ofstream
#include <string> // needed for strings
#include <windows.h> //needed for Sleep
#include <cstdlib> //Needed for return EXIT_SUCCESS;

using namespace std;

int main()
{
ofstream outputfile; //allows to read and write to files
//-=ints=-//
int num1;
int begining;
//-=strings=-//
string word;
string open;
								//-=Start of program=-//
cout << "File Read/Write Program" << endl;	//Prints "File Read/Write Program "
cout << "Write: 1" << '\t' << endl;	// Prints two options, write and read. \t makes a colum inbetween
cin >> num1;	//User imput sets "num1"
outputfile.open ("output.txt");	// opens/makes output.txt file
	
if (num1 == 1)	//If num1 = 1 one, then do this, num1 is from the users input.
		{
			cout << "You May Start To Write" << '\n';	//Prints "You May Start To Write". '\n creates a new line after it has been printed.
			cin >> word;	//Fills the string "word" with the user imput from "cin"
			outputfile << word;	//Saves the "word" sting into the output.txt file opened before
		}

outputfile.close();	//closes outputdata.txt file	
cout << "Thanks For using This Read/Write Program" << endl << "Have A Great Day";
Sleep(1500);
return EXIT_SUCCESS;
}


If I type Puppies Are Cute and go to output.txt, the only thing written in the text file is Puppies.

Any help pointing me in the write direction would be awesome!
Thanks,
Bacon.
Dude use this:

cin.getline(word,100);

However 100 is the size <===

instead of

cin >> word

shortly you cannot write spaces while using cin >> word

http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
I switched it out, and it threw an error under the period for cin.getline(word,100);. Getting rid of the string string word;and making it a char word[100] solved this problem with the error. Now it just runs through the if statement with out letting any user input at all?
I', confused..
by the way there are other ways to write and read but lets work on yours...

dude seriously what happening there? loool when you remove the first cin then it works hahahhaha WTF??? I am confused.. toooo???
Last edited on
Do you know what? use this code to write 1 loool silly way...

1
2
3
4
5
6
char input[1];
cin.getline(input,2)
if(!strcmp(input,"1"))
{

}
Last edited on
That cin was there because after I got the writing portion down I was going to let the user chose between writing and reading from the file.

I did what you said works, getting rid of the first cin, and it doesn't write onto the file at all..
I am so confused -.-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
char arr[100];
ofstream out("c:\\textfile.txt" );
	if( !out )
	{
	   cout << "Couldn't open file."  << endl;
	   return 1;
	}
cin.getline(arr,50);
out << arr;
out.close();
return 0;
}
Last edited on
thanks
@ModernBacon
Ignore joneele. He doesn't know what he's talking about.

the only thing you need to change is 28:
1
2
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Note that cin >> num1; left aa end of line ('\n') in the stream what is needed to be ignored at this point (before getline)
getline(cin, word); // since you don't want the word but the whole line better rename 'word' to 'line' 


the better getline is this:

http://www.cplusplus.com/reference/string/string/getline/
@ModernBacon
Ignore joneele. He doesn't know what he's talking about

@coder777 I do know what i was talking about http://mathbits.com/MathBits/CompSci/APstrings/APgetline.htm this gonna help u ModernBacon

Just lots of vodka.. haha
Last edited on
Topic archived. No new replies allowed.