[prog] Re: more C++

Jacinta Richardson jarich at perltraining.com.au
Sun Dec 14 19:58:39 EST 2003


> Hi, I was wondering if it's possible to change the way a response 
> prints out say my number being entered by
> the user is something like 98765, instead of the response printing You 
> entered 98765 is there a way to make
> it print You entered 9 8 7 6 5? So that it distinctly shows each number 
> separately?

Yes there is, however it's a lot more complex than what you're dealing
with at the moment.  I'll give you the C way of doing it below, but I
can't really help you if there's a better C++ way of doing it because
it's been way too long for me.

>  Also, are there specific places
> you should make comments? I know my stuff doesn't do much at the moment 
> but I was wondering if say before
> the std::cout there should be a comment that says something like //this 
> program prints the number entered by
> a user. Should I already be adding comments or just wait until I get to 
> a much longer program?  Thanks  :)

Comments are a great thing and you should always add them if you feel that
it makes the code easier to understand.  I usually recommend that people
include a date, their name and a brief description of the overall purpose
of the program at the top of their scripts and then brief comments where
necessary thereafter.

Anything that is really obvious such as "this line prints out the number"
can be left out, because it just clutters up the code, but if something
isn't obvious then comments are invaluable.

> #include <iostream>
> 
> // function main begins program execution
> int main()
> {
> 
> int integer1;
> 
>     std::cout << "Enter a number!\n";
> 
>     std::cin>>integer1;
> 
>     std::cout << "You entered..."<< integer1 << std::endl;
> 
> 
> 
> 
>     return 0;   // indicate that program ended successfully
> 
> } // end function main


/* 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;
}


All the best,

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list