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
mv2112
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:
Post
by mv2112 » Fri Feb 26, 2010 8:53 pm
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.
combatant936
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:
Post
by combatant936 » Fri Feb 26, 2010 9:21 pm
Change cin>>in to getline(cin,in)
hurstshifter
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:
Post
by hurstshifter » Fri Feb 26, 2010 9:24 pm
mv2112
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:
Post
by mv2112 » Fri Feb 26, 2010 9:39 pm
wow, that was simple lol
Does anyone know why cin>>in; doesnt work?
hurstshifter
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:
Post
by hurstshifter » Fri Feb 26, 2010 9:47 pm
mv2112 wrote: wow, that was simple lol
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.
mv2112
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:
Post
by mv2112 » Fri Feb 26, 2010 10:03 pm
hurstshifter wrote: mv2112 wrote: wow, that was simple lol
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!