-9

现在这是真正的代码,如果我输入数字 1009,我希望输出数字是 7687,现在这不是问题所在。在我输入 encryptnum 的最后一个 cout 语句中,我希望它输出 7687 所以我不必输入 cout << first << second << third << Fourth;

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

1 回答 1

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;
}
于 2013-05-16T19:51:33.293 回答