Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Variable type allowing spaces?
New Posts  All Forums:Forum Nav:

Variable type allowing spaces?

post #1 of 10
Thread Starter 
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;
}
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
post #2 of 10
Because you need to set the title as an array of chars (which includes spaces) like you did earlier in the program.
R'lyeh
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 (4.03GHz) Gigabyte EP45-UD3P EVGA GTX285 4GB (2x2) Corsair Dominator (5-5-5-15, 2.1v) 
Hard DriveOptical DriveOSMonitor
300GB WD VelociRaptor, 320GB WD Caviar 7200 Lite-On 20X DVD+R Ubuntu 10.04 x64 / Vista Home Premium x64 Samsung 226BW 
KeyboardPowerCaseMouse
Generic P/S2 Corsair HX750W Lian-Li V2000 Logitech G3 
  hide details  
Reply
R'lyeh
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 (4.03GHz) Gigabyte EP45-UD3P EVGA GTX285 4GB (2x2) Corsair Dominator (5-5-5-15, 2.1v) 
Hard DriveOptical DriveOSMonitor
300GB WD VelociRaptor, 320GB WD Caviar 7200 Lite-On 20X DVD+R Ubuntu 10.04 x64 / Vista Home Premium x64 Samsung 226BW 
KeyboardPowerCaseMouse
Generic P/S2 Corsair HX750W Lian-Li V2000 Logitech G3 
  hide details  
Reply
post #3 of 10
Thread Starter 
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.
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
post #4 of 10
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;
Personally, I hate operator overloading. Anything that changes the grammar of a language is sure to confuse people more than necessary.
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
    
CPUGraphicsRAMHard Drive
Intel 2.4 Core i7 AMD Radeon HD 6750M 8 GB 1067 MHz DDR3 750 GB 
OS
Mac OS-X Lion 
  hide details  
Reply
post #5 of 10
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.
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
post #6 of 10
Thread Starter 
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.
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
post #7 of 10
Try adding this line right after the initial cin >> filename;

Code:
cin.ignore();
What this does is capture the "carriage return/enter key" from entering in the file name. As it stands now the enter key (aka whitespace) is becoming the "title" instead of being ignored as it should be.
R'lyeh
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 (4.03GHz) Gigabyte EP45-UD3P EVGA GTX285 4GB (2x2) Corsair Dominator (5-5-5-15, 2.1v) 
Hard DriveOptical DriveOSMonitor
300GB WD VelociRaptor, 320GB WD Caviar 7200 Lite-On 20X DVD+R Ubuntu 10.04 x64 / Vista Home Premium x64 Samsung 226BW 
KeyboardPowerCaseMouse
Generic P/S2 Corsair HX750W Lian-Li V2000 Logitech G3 
  hide details  
Reply
R'lyeh
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 (4.03GHz) Gigabyte EP45-UD3P EVGA GTX285 4GB (2x2) Corsair Dominator (5-5-5-15, 2.1v) 
Hard DriveOptical DriveOSMonitor
300GB WD VelociRaptor, 320GB WD Caviar 7200 Lite-On 20X DVD+R Ubuntu 10.04 x64 / Vista Home Premium x64 Samsung 226BW 
KeyboardPowerCaseMouse
Generic P/S2 Corsair HX750W Lian-Li V2000 Logitech G3 
  hide details  
Reply
post #8 of 10
Thread Starter 
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.
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
ragequit
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 4ghz @ 1.25v Asus Max 2 formula 8800gtx 610/1ghz OCZ LV blade 1:1 950mhz 
Hard DriveOptical DriveOSMonitor
7200.10 250gb dvd leetlinucks 24" Westy 
KeyboardPowerCaseMouse
G15 750w Toughpower Lian li pc-65 Copperhead 
Mouse Pad
Thermtaltake? 
  hide details  
Reply
post #9 of 10
allright allright I'll give it a quick look, hang on.
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
post #10 of 10
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;
}
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
It's about time!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 @3.3Ghz Gigabyte EP45-UD3P Sapphire 2900Pro Flashed to XT 4Gb Gskill 1066Mhz PK's 
Hard DriveOptical DriveOSMonitor
Seagate Barracuda 320Gb & WD Black 640Gb Lite On Vista Business and VMWare Ubuntu Acer AL2223W 22" 
KeyboardPowerCaseMouse
Ergonomic 4000 Corsair HX 620W CM 690 G5 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Variable type allowing spaces?