[prog] Re: more C++
wolf
wolf at wolfrising.net
Sun Dec 14 11:20:29 EST 2003
Thank you : ) Would anyone happen to be able to show this in a
simplified C++ version?
While I sort of understand some of this, it's a bit over my head for
where I am at the
moment : ) The only #include I've used so far is #include <iostream>
(altho I assume
a .h means it's a header file of some sort). And the first bit of the
book I'm still working on
doesn't show anything about sprintf or strlen(string).
Thank you to everyone for the examples and explanations, I'm getting
some good notes
to use : ) : ) : )
On Dec 14, 2003, at 2:58 AM, Jacinta Richardson wrote:
> /* This takes a number from the user and prints out each digit
> * separately. This code should work with C and C++ but there has
> * to be a better C++ way of doing it.
> */
>
> #include <iostream>
> #include <string.h>
> #include <stdio.h>
>
> int main() {
> int integer1;
> char string[100];
>
> std::cout << "Enter a number!\n";
>
> std::cin >> integer1;
>
> // Copy the number into a string (character array)
> sprintf(string, "%d", integer1);
>
> std::cout << "You entered... ";
>
> // Print out each digit in the number with a space.
> int i;
> for (i = 0; i < strlen(string); i++) {
> std::cout << string[i] << " ";
> }
>
> std::cout << std::endl;
>
> return 0;
> }
>
>
More information about the Programming
mailing list