|
|
Accelerated C++ exercise 4-2 -- converting int to string?
Here's the question and the code I wrote for it. The // comment
represents what I would like to do. Is there a really crude way of
doing that -- because the book hasn't provided any fancy methods of
doing it such as string streams, type casting, or anything like that
-- or would you suggest a different approach?
/*
4-3. What happens if we rewrite the previous program to allow values
up to
but not including 1000 but neglect to change the arguments to setw?
Rewrite
the program to be more robust in the face of changes that allow i to
grow
without adjusting the setw arguments.
*/
#include <iomanip>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::setw;
using std::string;
int main() {
int from = 1;
int up_to = 1000;
// int pad = length of up_to
// int pad_square = length of (up_to * up_to)
cout << setw(pad) << "Value:" << setw(pad) << "Square:" << endl;
for (; from < up_to; ++from) {
cout << setw(pad) << from << setw(pad) << from * from << endl;
}
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
fredrikhcs (6)
|
7/27/2008 2:23:55 AM |
|
On Jul 27, 6:23 pm, banangroda <fredrik...@gmail.com> wrote:
> Here's the question and the code I wrote for it. The // comment
> represents what I would like to do. Is there a really crude way of
> doing that -- because the book hasn't provided any fancy methods of
> doing it such as string streams, type casting, or anything like that
> -- or would you suggest a different approach?
>
> /*
> 4-3. What happens if we rewrite the previous program to allow values
> up to
> but not including 1000 but neglect to change the arguments to setw?
> Rewrite
> the program to be more robust in the face of changes that allow i to
> grow
> without adjusting the setw arguments.
> */
>
> #include <iomanip>
> #include <iostream>
> #include <string>
> using std::cout;
> using std::endl;
> using std::setw;
> using std::string;
> int main() {
> int from = 1;
> int up_to = 1000;
> // int pad = length of up_to
> // int pad_square = length of (up_to * up_to)
> cout << setw(pad) << "Value:" << setw(pad) << "Square:" << endl;
> for (; from < up_to; ++from) {
> cout << setw(pad) << from << setw(pad) << from * from << endl;
> }
>
> }
Not very polished, but as an indicative approach you might find this
helpful:
// length of positive int
template <int N>
struct Length_Of_Positive
{
static const int value = 1 + Length_Of_Positive<N / 10>::value;
};
template<>
struct Length_Of_Positive<0>
{
static const int value = 0;
};
#include <iostream>
using namespace std;
int main()
{
cout << Length_Of_Positive<1>::value << '\n';
cout << Length_Of_Positive<9>::value << '\n';
cout << Length_Of_Positive<10>::value << '\n';
cout << Length_Of_Positive<99>::value << '\n';
cout << Length_Of_Positive<100>::value << '\n';
cout << Length_Of_Positive<999>::value << '\n';
cout << Length_Of_Positive<1000>::value << '\n';
cout << Length_Of_Positive<9999>::value << '\n';
}
Cheers,
Tony
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Tony
|
7/28/2008 11:19:10 AM
|
|
|
1 Replies
178 Views
(page loaded in 0.071 seconds)
Similiar Articles: integer overflow in atoi - comp.lang.c... as an exercise ... lang.c integer overflow in atoi - comp.lang.c How to convert ... gcc 4.4.4 c89. What is better to convert a string to an integer value. I have tried 2 ... A Minor Problem With atoi() And Negative Numbers - comp.lang.c ...... it is reasonable exercise ... > $ cat a.c #include <stdio.h> int main(int argc, char **argv) { if (argc==2) puts(argv[1 ... Converting number to std::string ("itoa()" ) - comp ... improve strlen - comp.lang.asm.x86Here's a typical strlen() implementation in C int ... You will still need to convert it to AT&T syntax but it ... template <typename chartype> inline int string_length ... Hex to ascii - comp.lang.asm.x86... all programming can be viewed as an exercise in ... seeing a 32-bit conversion to decimal integer string ... How to Convert an ASCII String to Hex Format | eHow.com If you are ... write read string data - comp.lang.java.help... that this is only an exercise ... 10; array[1] = 20; array[2] = 30; array[3] = 40; array[4] = 50; int ... converting a character string into a variable name - comp ... Math logic - comp.lang.asm.x86... programming can be viewed as an exercise ... We can now convert the bottom 4 digits by masking away the integer part, multiplying by ... vector to real - comp.lang.vhdl String ... seconds to YYYYMMDDHHMMSS - comp.unix.programmerHi folks, i am in need to convert the ... d"] -= floor(T["Y"] * 1461 / 4) T["j"] += T["d"] T["m"] = int((5 * T["d"] + 2 ... then I think the purpose of the exercise ... FAQ -- assembly-language/x86/general/part1 - comp.lang.asm.x86 ...Please do not modify the file, such as converting it ... The library covers the following areas: string/integer ... it > in assembly would be just some kind of exercise. Converting from Y-m-d h:m:s - comp.protocols.time.ntp*/ long int yg = y + days / 365 - (days % 365 ... [tempo] Convert variable from string to number - comp.emacs ... ... How to Convert Units of Measurement - Oak Road Systems ... HP48GX Routine to Extract Prime Number Factors - comp.sys.hp48 ...... routine out of the PPC ROM book that factors an integer ... always work] If one wants to use a specific exercise ... digits" to be ... to do string assignment, string ... Integer Functions, Random Number, String Conversion, Searching and ...int atoi(char *string)-- Convert string to an integer value int atol(char *string)-- Convert ... Exercise 12534. Write a program that simulates throwing a six sided die ... String.ToCharArray Method (System)Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET ... string myString = "1234567890"; char[] myArr = myString.ToCharArray(); for (int i = 0; i < myArr.Length; i++) 7/13/2012 12:14:21 AM
|
|
|
|
|
|
|
|
|