如何将多个整数分配给另一个整数?

时间:2013-05-16 19:41:16

标签: c++

这是真正的代码现在如果我输入数字1009我想要输出数字是7687,现在这不是问题。在我输入encryptnum的最后一个cout语句中,我希望它输出7687,所以我不必把cout<<第一个<<第二<<第三<<第四;

#include <iostream>
using namespace std;

int main()
{
    int num;
    int first;
    int second;
    int third;
    int fourth;
    int encryptnum;

    cout << " Enter a four digit number to encrypt ";
    cin >> num;

    first = num % 100 / 10;
    second = num % 10;
    third = num % 10000 / 1000;
    fourth = num % 1000 / 100;

    first = (first + 7) % 10;
    second = (second + 7) % 10;
    third = (third + 7) % 10;
    fourth = (fourth + 7) % 10;

    encryptnum = //I want to make encryptnum print out first, second, third, and fourth

    cout << " Encrypted Number " << encryptnum;

    return 0;
}

1 个答案:

答案 0 :(得分:4)

根据您的评论(您应该将其真正纳入问题以使您的目标更加清晰),这可以帮助您:

#include <iostream>
#include <sstream>

int main()
{
    int first, second, third, fourth;
    int all;

    std::cout << " Enter four numbers ";
    std::cin >> first >> second >> third >> fourth;
    std::stringstream s;
    s << first << second << third << fourth; //insert the four numbers right after each other
    s >> all;  //read them as one number

    return 0;
}