[SOLVED]Unwanted linebreak when outputting to a textfile

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

[SOLVED]Unwanted linebreak when outputting to a textfile

Post 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;
}
Last edited by mv2112 on Fri Feb 26, 2010 10:02 pm, edited 1 time in total.
User avatar
combatant936
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Wed Jul 29, 2009 11:11 pm
Current Project: It's a secret!!
Favorite Gaming Platforms: N64, SNES, PS2, PC
Programming Language of Choice: C++, 65816 asm
Location: Pennsylvania
Contact:

Re: Unwanted linebreak when outputting to a textfile

Post by combatant936 »

Change cin>>in to getline(cin,in)
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Unwanted linebreak when outputting to a textfile

Post by hurstshifter »

try

Code: Select all

getline(cin, in);

instead of using cin
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Unwanted linebreak when outputting to a textfile

Post by mv2112 »

wow, that was simple lol :oops:
Does anyone know why cin>>in; doesnt work?
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Unwanted linebreak when outputting to a textfile

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Unwanted linebreak when outputting to a textfile

Post 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!
Post Reply