我在将字符串转换为整数数组时遇到问题

时间:2016-09-18 00:00:36

标签: c++ arrays string type-conversion

以下是我的代码:

(假设字符串小于10位)

public class Server
{
   public static void main(String args[])
   {
   try
   {
       ServerSocket ss = new ServerSocket(80);
       Socket sock = ss.accept();
       DataInputStream recive = new DataInputStream(sock.getInputStream());
       DataOutputStream sendOut = new DataOutputStream(sock.getOutputStream());
       BufferedReader readOut = new BufferedReader(new InputStreamReader(System.in));

       String in = "";
       String out = "";
       Scanner scan = new Scanner(System.in);
       System.out.println("Talk with client(y/n): ");
       String start = scan.nextLine();
       while(start == "y")
       {
       in = recive.readUTF();
       System.out.println(in);
       out = readOut.readLine();
       sendOut.writeUTF(out);
       sendOut.flush();
      }
    }
    catch(IOException e)
    {System.out.println("[!]CANNOT ESTABLISH CONNECTION[!]");}

  }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可能想要这样做:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s;
    cin >> s;

    int array[10];

    for(int i=s.length(); i>=0; i--)
    {
        array[i]=s[i]-'0';
    }

    for(int b=0; b<s.length();b++)
    {
        cout << array[b];
    }


    //better way

    int num =stoi (s,nullptr,0); // You can convert the entire string to an int like this.
    cout << endl << num << endl;

}