Page 1 of 1

[SOLVED]Unwanted linebreak when outputting to a textfile

Posted: Fri Feb 26, 2010 8:53 pm
by mv2112
Ok, so i wrote a program that asks you for a file name you want to create or edit, and then streams text to it. Its basicly just a simple text editor. After each line of text, there is a line break. My problem is that when i have a string with spaces, it makes each space a new line. Why is it doing this?

Code: Select all

#include "main.h"

int main(){
	char fname[20];
	string in;
	cout<<"Filename: ";
	cin>>fname;

	ofstream file;
	file.open(fname,ios::app);
	cls();

	cout<<"Enter Text and Press Enter for New Line"<<endl;
	while(file.is_open()){
		cin>>in;

//if you input "echo hello"
//the created file reads:
//echo
//hello 
//instead of: 
//echo hello

		if(in=="%exit%"){
			file.close();
			break;
		}
//this is where the string is put into the file
//then after, a line break

		file<<in<<endl;
	}
}
if your wondering what main.h is, its just a header i made so i dont have to keep entering different includes + it has some functions i made

Code: Select all

#include <conio.h>
#include <windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <time.h>

using namespace std;

void pause(){
system("pause");
}

void cls(){
system("cls");
}

void exit(){
system("exit");
}

void text_color(char color){
  
  switch(color){
    case 'a':
    system("color a");
    break;
    case 'b':
    system("color b");
    break;
    case 'c':
    system("color c");
    break;
    case 'd':
    system("color d");
    break;
    case 'e':
    system("color e");
    break;
    case 'f':
    system("color f");
    break;
  }
  cls();
}

void set_title(string title){
     string t;
     t="title "+title;
     system(t.c_str());
     return;
     }
     
void space(){
cout<<endl;
}

Re: Unwanted linebreak when outputting to a textfile

Posted: Fri Feb 26, 2010 9:21 pm
by combatant936
Change cin>>in to getline(cin,in)

Re: Unwanted linebreak when outputting to a textfile

Posted: Fri Feb 26, 2010 9:24 pm
by hurstshifter
try

Code: Select all

getline(cin, in);

instead of using cin

Re: Unwanted linebreak when outputting to a textfile

Posted: Fri Feb 26, 2010 9:39 pm
by mv2112
wow, that was simple lol :oops:
Does anyone know why cin>>in; doesnt work?

Re: Unwanted linebreak when outputting to a textfile

Posted: Fri Feb 26, 2010 9:47 pm
by hurstshifter
mv2112 wrote:wow, that was simple lol :oops:
Does anyone know why cin>>in; doesnt work?

I believe it is because it reads the spaces as null characters. A null character would signify the end of a string in traditional C strings.

Re: Unwanted linebreak when outputting to a textfile

Posted: Fri Feb 26, 2010 10:03 pm
by mv2112
hurstshifter wrote:
mv2112 wrote:wow, that was simple lol :oops:
Does anyone know why cin>>in; doesnt work?

I believe it is because it reads the spaces as null characters. A null character would signify the end of a string in traditional C strings.
That does makes sense. Thanks for the quick replies!