How to implement Java "Scanner" in C++?

19,862

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:

Share:
19,862
PeakGen
Author by

PeakGen

CTO

Updated on June 04, 2022

Comments

  • PeakGen
    PeakGen almost 2 years

    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!