1 Replies - 208 Views - Last Post: 03 October 2012 - 07:28 PM Rate Topic: -----

#1 Tom_Dub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-October 12

Help with converting digits or symbols into numbers

Posted 03 October 2012 - 05:32 PM

Hey all,

I'm a little confused on the proper method for converting symbols and what not into numbers.

For example, writing a program that reads from a file.

Say the file has input: $7,5,21%!87,9!8.32

I'd want the output to be on 3 separate lines as;

7521
879
832

So far this is the code I have below:

int main()
{
char ch;
ifstream inFile;
ofstream outFile;

infile.open ("Sample.txt");
if (not inFile)
{
cout<<"failure to open sample.txt"<<endl;
system("Pause");
return 1;
}
outFile.open("out.txt");
if(not outFile)
{
cout<<"Failture to open out.txt"<<endl;
system("pause");
return 1;
}

inFile.get(ch);
while(inFile)
{
while((ch!= '\n') and inFile)
    {
     outFile.put(ch);
     inFile.get(ch);
    }
if (inFile)
{
outFile.put(ch);
inFile.get(ch);
}
}
system("pause");
inFile.close(); outFile.close();
return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with converting digits or symbols into numbers

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Help with converting digits or symbols into numbers

Posted 03 October 2012 - 07:28 PM

You can check for character types cctype

If the character is a digit, print it, ignore commas and periods, otherwise print newline.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1