-9

数値 1009 を入力すると、これが実際のコードになりました。出力数値を 7687 にしたいのですが、これは問題ではありません。最後の cout ステートメントで、encryptnum を入力したときに 7687 を出力したいので、cout << first << second << third << four; を入力する必要はありません。

#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 に答える