|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Variable type allowing spaces?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||||
|
Security Sleuth
|
Well, I got my app to write the title to the file.
Now my problem is it doesnt allow spaces. When it asks say Ill answer an ex. of "My answer". The only thing to be wrritten to file would be "My". Im using string for the data types. Heres my code: Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long.
ofstream the_file; //variable to refer to stream to write to file
cout <<"What do you want the name of your webpage to be? NOT the title!";
cin >> filename;
the_file.open(filename);
string titlea = ("<title>");
string titleb = ("</title>");
cout <<"What do you want the title to be?";
string title;
cin >> title;
the_file <<titlea<<title<<titleb;
the_file.close();
cout << "nnnProgram by Fr0nAtZ!n";
system("PAUSE");
return 0;
}
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
Last edited by Pooping^fish : 04-03-08 at 11:36 PM. |
||||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
I provide....LEVERAGE!
|
Because you need to set the title as an array of chars (which includes spaces) like you did earlier in the program.
__________________
Watercooling Loop: MCP655 Vario -> Black Ice GTX360 -> D-Tek FuZion V1 -> MCW-60 -> MicroRes Loop Cost: $395.03
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
Security Sleuth
|
you mean char? I tried, it didnt the same thing.
Ill try agian just incanse.. Edit-Nope. Char does the same thing, my filename wont take spaces either. go figure.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||
|
Chiefly Ignorant
|
I think the cin operator>> uses all whitespace as a string terminator. You could try cin.getline() instead. Or you could mess around with the noskipws manipulator to turn off this whitespace terminating "feature." Try putting this before the filename gets input:
Code:
cin >> noskipws; ![]()
__________________
|
||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
Programmer
|
Yeah, a string or char[] should be able to store spaces just fine. The issue seems to be that your cin call is only reading until the space. As Scriptorium suggested try cin.getline() which by default will read until it hits a newline (return) character.
__________________
"He attacked everything in life with a mix of extraordinary genius and naive incompetence, and it was often difficult to tell which was which." Douglas Adams
|
|||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Security Sleuth
|
when I use cin.getline(title,21) the programs compiles, but it just skips right past the title question. It says it but acts like ive already answers and the program terminates.
I tried the noskipws's, it did the same. I tried combining them like cin >> noskipws >> titie; and I even tried cin >> noskipws; cin.getline(title,21); together. All did the same thing with skipping the title line. Shouldnt this be really simple? ahhhh.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||
|
I provide....LEVERAGE!
|
Try adding this line right after the initial cin >> filename;
Code:
cin.ignore();
__________________
Watercooling Loop: MCP655 Vario -> Black Ice GTX360 -> D-Tek FuZion V1 -> MCW-60 -> MicroRes Loop Cost: $395.03
|
|||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||||
|
Security Sleuth
|
Nope. Epic fail once again. Does the same thing.
Have I generated some inter-space C++ time paradox of the white space people? Perhaps I have.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#9 (permalink) | |||||||||||||
|
Programmer
|
allright allright I'll give it a quick look, hang on.
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
#10 (permalink) | |||||||||||||
|
Programmer
|
this seems to work:
Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char filename[51]; //variable to store file name //declares an array of characters to store the file name, 50 chars long.
ofstream the_file; //variable to refer to stream to write to file
cout <<"What do you want the name of your webpage to be? NOT the title!";
cin.getline(filename, 50);
the_file.open(filename);
string titlea = ("<title>");
string titleb = ("</title>");
cout <<"What do you want the title to be?";
char title[50];
cin.getline(title, 50);
the_file <<titlea<<title<<titleb;
the_file.close();
cout << "nnnProgram by Fr0nAtZ!n";
system("PAUSE");
return 0;
}
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|