我怎么能让我的输入没有。如1010而非1“\ n”0“\ n”1“\ n”0“\ n”

时间:2015-03-09 15:42:44

标签: c++

#include <iostream>

using namespace std;

int main(){

   int array_size, array[100];

   cout << "Enter length of number you want to input ";
   cin >> array_size;

   cout << "Enter the inputs" << endl;

   for (int i = 0; i < array_size; i++){
            cin >> array[i];

            if (array[i] == 0){
               array[i] = 1;                          
            }else{
               array[i] = 0;      
            }
       } 

       cout << "output" << endl;
       for (int i = 0; i < array_size; i++){
                cout << array[i];
           }
           cout << endl;
           system("PAUSE");
           return 0;
   }

1 个答案:

答案 0 :(得分:1)

读取字符串的输入并解析该字符串中的数字。

std::string input;
std::cin >> input;

for (int i = 0; i < array_size; i++){
    if (input.at(i) == '0') {
        array[i] = 1;
    } else {
        array[i] = 0;
    }
}