Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 3 Weeks Ago   #1 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 433

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default (C++)Using strings to replace characters [fstream]

Hey guys,

I am struggling with a part of file reading where the goal is to "clean up" the code. Basically, I've gotten to the point where it reads each character and gives spaces between them. However, I need to replace the "text" part of the string with space. For instance, here is an example I'll use:

Code:
12 Hello 34 OCN 56 I need your help
I only want:

Code:
12 34 56
I'm guessing some string manipulation will be needed. I've tried to use isdigit(), but to no avail. Here's what I've got currently. As always, please try to explain the code but not show the solution to my code, as I feel guilty for A) not using your code and upset for B) having actually seen it.

All I would like is an explanation of HOW THIS CAN BE DONE...+1 rep to all who help, as usual.

Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string str;
    ifstream lane9;
    ofstream mglane9;
    const int MAX = 1000;
    char scores[MAX];
    int count = 0;
    
    lane9.open("lane9.dat");
    if(lane9.fail())
    {
                    cout << "ERROR! ";
                    exit(1);
    }

    mglane9.open("MGlane9.txt");
    if(mglane9.fail())
    {
                      cout << "ERROR OUTPUT";
    }

    while(!lane9.eof())
    {
        lane9 >> str;
 	cout << str << " ";	
        mglane9 << str << " ";	   
	 
	                    if(isdigit(count))
			    {
			  	mglane9 << str << " ";
				count++;
			    }
    }
	    
    lane9.close();
    mglane9.close();
    
    getchar();
    return 0;
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is online now   Reply With Quote
Old 3 Weeks Ago   #2 (permalink)
Intel Overclocker
 
fball922's Avatar
 
intel ati

Join Date: May 2005
Posts: 390

Rep: 11 fball922 Unknown
Unique Rep: 11
Trader Rating: 3
Default

Maybe not the best solution, but maybe keep track of the last character you encountered so that you know what should be done next... I.e. If the last character was a digit and you have a space, do something. Move space to previous character...

Hope you see where I am going with that.
__________________
Spelling mistakes in my post? I am probably typing on my iPod... Please forgive me!

Columbus, OH, area pc repair!

System: HOOOSIER!!!!
CPU
Q6600 @3.25ghz
Motherboard
Gigabyte GA-P35-DS3R
Memory
4x2GB G.Skill
Graphics Card
HD 4890 soon... X1300 for now
Hard Drive
400GB OS, 80x2 RAID-0 Apps, 640GB Storage
Sound Card
Audigy Something... Old
Power Supply
430w Thermaltake
Case
Antec 300
CPU cooling
Scythe Ninja w/ 120mm fan
GPU cooling
Stock
OS
Windows 7 Pro x64
Monitor
23" Samsung
fball922 is offline   Reply With Quote
Old 3 Weeks Ago   #3 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

You'll need to loop over all of the characters in 'str', calling isdigit(str[i]) and if it is a digit, add that character to another string (or just write the digit to the output file). Oh, and you'll need to add a space between numbers.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 07:47 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.09836 seconds with 8 queries