2

Please have a look at the following java code

import java.util.Scanner;

public class Main
{
    static int mul=1;
    static String  convert;
    static  char[] convertChar ;
    static StringBuffer buffer = new StringBuffer("");

    public static void main(String[]args)
    {

        Scanner scan = new Scanner(System.in);

        int number=0;
        int loopValue = scan.nextInt();
       //System.out.println("print: "+loopValue);

        for(int i=0;i<loopValue;i++)
        {
            number = scan.nextInt();
           // System.out.println("print: "+number);

            for(int a=1;a<=number/2;a++)
            {
                if(number%a==0)
                {
                    //System.out.println(a);
                    mul = mul*a;
                    //System.out.println(mul);
                }
            }

           convert  = String.valueOf(mul);
           convertChar = convert.toCharArray();

           if(convertChar.length>4)
            {
               /*System.out.print(convertChar[convertChar.length-4]);
               System.out.print(convertChar[convertChar.length-3]);
               System.out.print(convertChar[convertChar.length-2]);
               System.out.print(convertChar[convertChar.length-1]);
               System.out.println();*/

               buffer.append(convertChar[convertChar.length-4]);
               buffer.append(convertChar[convertChar.length-3]);
               buffer.append(convertChar[convertChar.length-2]);
               buffer.append(convertChar[convertChar.length-1]);

                System.out.println(buffer);

            }
            else
            {
                System.out.println(mul);
            }

            //System.out.println(mul);
            mul = 1;
        }

    }
}

This code is built to compute the product of positive divisors of a given number. I have used scanner here because I don't know how many inputs will be entered. That is why I can't go something like

int a, b;

cin >> a >> b

in C++. All the inputs will be inserted by a test engine, into one single like like following

6 2 4 7 8 90 3456

How can I implement the Java "Scanner" using C++ ? Is there a header file for that? Please help!

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 1
    If you don't know how many will be entered, use a loop like you would in Java. There are some other ways, but it's still possible with almost the same code. – chris Feb 11 '13 at 21:57
  • are you reading this from a file? is that the whole content of the file? if yes, then read in a loop until the end of the file. if it's per-line, then split the line. here is a link for you http://stackoverflow.com/questions/236129/splitting-a-string-in-c – Marius Bancila Feb 11 '13 at 22:01

2 Answers2

3

You seem to be using Scanner to read one integer at a time from the standard input stream. This is easily accomplished with the extraction operator, operator>>.

Replace this code:

    Scanner scan = new Scanner(System.in);

    int number=0;
    int loopValue = scan.nextInt();
   //System.out.println("print: "+loopValue);

    for(int i=0;i<loopValue;i++)
    {
        number = scan.nextInt();
       // System.out.println("print: "+number);

With this:

    int number=0;
    int loopvalue=0;
    std::cin >> loopvalue;

    for(int i = 0; i < loopValue; i++)
    {
        std::cin >> number;

You should check the value of std::cin after the >> operations to ensure that they succeeded.

Refs:

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

If you use std::cin >> value; to read the value then you can only process the entire line once a new-line has been detected.

If you want to process each number as it is typed then you could use a function like:

int nextInt()
{
    std::stringstream s;
    while (true) 
    {
        int c = getch();
        if (c == EOF) break;
        putch(c); // remove if you don't want echo

        if ((c >= '0' && c <= '9') || (c == '-' && s.str().length() == 0)) 
            s << (char)c;
        else if (s.str().length() > 0)
            break;        
    }

    int value;
    s >> value;
    return value;
}

OK, there are probably more efficient ways to write that but it will read the input character by character until a number is encountered and will return whatever number is read when anything other than a number is encountered.

E.g. 1 2 3 4 would return 1 on the first call, 2 on the second etc.

Simon
  • 209
  • 1
  • 3