• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ Challenge - If your bored :)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
Joined
Jan 17, 2004
If your looking for something to do I have just the thing for you !!

C++ conversion
prototype:
string CommaConvert(string prompt){return string};

1. write a function that takes a string
2. detect commas only and remove them
3. shift string char's into proper location after removal of comma

if you had a string of 1,552,300.85 then you would want to end up with 1552300.85
initial string size of 12 would change to size of 10



4. return string


//////////////////////// if your really awesome :clap:

prototype:
string CommaConvert(string prompt){return string};

add a bool value to function to set if error is displayed

1. detect if commas are out of order and display error

example error output for 12,8.95
1200.8,4
12,,98.51
etc ...






this is not a requirement for my project but Im trying to go the extra mile with input validation .. I will likely use it if it works and will cite your work for appropriate credit ..

also this is mainly for a function i started working on to convert a string represented double to a double double (no pun intended ) .. likely people will will re-use this ... so if your not into sharing your skills then thanks for reading :)




just has to work error free how ever you get there :D ... ill be working slowly in the backgrounds too
 
Typed here, not compiled, not tested :)
Code:
#include <cctype>
string CommaConvert(string prompt) {
 int l = prompt.length() - 1;
 string result = "";
 for (int i = 0; i < l; i++) {
  int hasDecimal = 0;
  char current = prompt[l - i];
   if (current == '.') {
    if (i != 2) {
     return "Input ain't no number.";
    } else {
     hasDecimal = 1;
     result.insert(0, current);
    }
   } else if (current == ',') {
    if (!(
        isdigit(prompt[l - i - 1])
        || isdigit(prompt[l - i + 1])
        || isdigit(prompt[l - i + 2])
        || isdigit(prompt[l - i + 3])
       )) {
     return "Input ain't no number.";
    }
   } else if (isdigit(current)) {
    result.insert(0, current);
   }
  }
 }
 return result;
}

EDIT: And I noticed it is obviously broken.
EDIT: And perhaps this version might work.
 
Last edited:
Back