in reply to Five digit number display separately

#include <iostream> #include <string> using namespace std; int main () { string user_input; cout << "Please enter a five digit integer: "; cin >> user_input; for ( size_t ix = 0; ix != user_input.size(); ++ix ) { for ( size_t count = 0; count <= ix; ++count ) cout << user_input.at(ix); cout << endl; } return 0; }

Oh, you your instructor wanted Perl? This is where you'll have to learn to adapt. ;) Update: Actually Perl can adapt for you:

#!/usr/bin/env perl use strict; use warnings; use Inline CPP => <<'EOC'; #include <iostream> #include <string> void doit () { std::string user_input; std::cout << "Please enter a five digit integer: "; std::cin >> user_input; for ( size_t ix = 0; ix != user_input.size(); ++ix ) { for ( size_t count = 0; count <= ix; ++count ) std::cout << user_input.at(ix); std::cout << std::endl; } } EOC doit;

After you turn in a serious solution, try this just for fun. :)


Dave

Replies are listed 'Best First'.
Re^2: Five digit number display separately
by TimTim (Initiate) on Sep 04, 2013 at 21:03 UTC
    print "insert 5 digit number:\n"; $string = <STDIN>; for ($i=1; $i<=5; $i++) { $var = substr($string,$i-1,1) Print "$var\n"; } For this i am getting output as: 1 2 3 4 5 someone help me out i am new to perl i need output as 1 22 333 4444 55555
      You are almost there. The only thing missing is to repeat each digit. For that, you can include another loop, or use the repetition operator as adviced in my first reply to your question.

      Also, please use <code> ... </code> tags for code and data samples to improve readability.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ