如何将多个整数赋值给另一个整数

How do I assign mutiple integers to another integer?

本文关键字:整数 赋值 另一个      更新时间:2023-10-16

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

#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;
}

根据你的评论(你应该把评论和问题结合起来,使你的目标更明确),这应该对你有帮助:

#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;
}