See I've been working on this problem for almost 2 weeks and we are only allowed to use things that we learned in class, so we can't use loops or even if else statements. This is a pain in the ass.
See I've been working on this problem for almost 2 weeks and we are only allowed to use things that we learned in class, so we can't use loops or even if else statements. This is a pain in the ass.
Well you could use the ?: operator, but that's really just another if/else, so would it count.
Also, you're not likely to have come across it.
Something along these lines, but without if statements, you'll end up with output likeCode:cout << (x/1000)%1000; cout << ","; cout << x%1000;
000,000,123
000,134,765
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
If you can't use conditional statements to decide when to print trailing zeros, I have to ask if you have had anything in class about formatting output to cout? (Specifically ostream member functions width() and fill().)Originally Posted by StarOrbs
If so, look at what this does, and maybe you can see how they could solve your problem:
Regards,Code:#include <iostream> using std::cout; using std::endl; int main() { int i; int x = 1234; for (i = 1; i < 10; i++) { cout.width(i); cout << x << endl; } cout << endl; cout.fill('0'); for (i = 1; i < 10; i++) { cout.width(i); cout << x << endl; } cout.width(3); cout.fill('0'); cout << endl; x = 12; cout << x << endl; return 0; }
Dave
Gee, sucks that you can't use 'if'. Well, something like this might work (though I haven't tested it):
**EDIT** Oops, didn't notice you can't use loops. Well, I'm out of ideas then
Code:int lengthOfNewString = strlen(strNumber) + strlen(strNumber) / 3; //Increase size for commas char* newString = new char[lengthOfNewString + 1]; //Leave room for '\0' newString[lengthOfNewString] = '\0'; int pos = strlen(strNumber) - 1; int nsPos = lengthOfNewString - 1; int iterations = strlen(strNumber) / 3; for(int i = 0; i < iterations; ++i) { newString[nsPos] = strNumber[pos]; newString[nsPos - 1] = strNumber[pos - 1]; newString[nsPos - 2] = strNumber[pos - 2]; newString[nsPos - 3] = ','; nsPos -= 4; pos -= 3; } int remainingDigits = strLen(strNumber) % 3; for(int i = 0; i < remainingDigits; ++i) { newString[nsPos] = strNumber[pos]; --nsPos; --pos; }
Last edited by Hunter2; 02-13-2005 at 01:59 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.