新しい問題が発生しました。 このプログラムの目標は、ユーザーが始めたのと同じA、B、Cの値で終わることです。私のコードは、例えば984や985のように、わずか3桁の整数を除いてちょうど3桁ごとに機能します。 "AとBの新しい値"は、9と8の代わりに3と2のように9と8の倍数になります(これはすべきです)。余りの合計が奇数の場合、11を加えない限り11 *を加算します。
問題の開始位置をコメントしました。それは私の新しい試みであり、コードの最初の試みです。前もって感謝します。
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
//declare each variable
int A,B,C;
int ABC, BCA, CAB;
int X,Y,Z; //remainders stored
int P,Q,R; //sums of remainders
//welcome
cout<< "Welcome to Acelia's 3 digit reader \n"
<<"\n";
//prompt user
cout << "Enter a number between 100 and 999: ";
cin >> ABC;
//fits in range
while ((ABC < 100) || (ABC > 999)) {
cout << "Enter a valid number between 100 and 999: ";
cin >> ABC;
}
cout << "Cool, you entered " << ABC
<<".\n"
<<"\nIn the form of ABC...\n";
//strip each digit of the number
A = (ABC/100);
B = ((ABC/10) % 10);
C = (ABC % 10);
BCA = (B * 100 + C * 10 +A); //hundreds, tens, ones
CAB = (C * 100 + A * 10 +B);
cout <<"A is "<< A<< "\nB is " << B<<"\nC is " << C <<"\n"; //print individual #
//print ABC, BCA, CAB
cout <<"\nYour number in the form ABC is " << ABC
<<"\nYour number in the form BCA is " << BCA
<<"\nYour number in the form CAB is " << CAB
<< "\n\n";
//store remainder of each value when divided by 11
X = (ABC % 11); cout<< "The remainder of " << ABC <<" divided by 11 is " << X;
Y = (BCA % 11); cout<< "\nThe remainder of " << BCA <<" divided by 11 is " << Y;
Z = (CAB % 11); cout<< "\nThe remainder of " << CAB <<" divided by 11 is " << Z << "\n\n";
//sums of each remainder
P = (X + Y); cout<< "The sum of remainders from ABC and BCA is: " << P;
Q = (Y + Z); cout<< "\nThe sum of remainders from BCA and CAB is: " << Q;
R = (Z + X); cout<< "\nThe sum of remainders from CAB and ABC is: " << R <<"\n\n";
int newP=0; //it would not execute properly w/o being initialized to 0
int newQ=0;
int newR=0;
// Check sum remainder of X & Y
/*!!!!!!PROBLEM IS HERE!!!!!*/
if (P % 2 == 1){
if(P + 11 > 20) {
newP = (P-11);
}
else {
newP = (P+11);
}
}
newP = (P/2);
cout << "\nNew value of A is: " << newP;
//check sum remainder of Y & Z
/*ORIGNAL CODE */
if ((Q % 2 == 1) && ((Q+11) > 20)){
newQ = (Q-11);
newQ = (newQ/2);
cout<< ("\nNew value of B is: ") << newQ << "\n";
if ((Q % 2 == 1) && ((Q + 11) < 20)) {
newQ = (Q+11);
newQ = (newQ /2);
cout<< ("\nNew value of B is: ") << newQ << "\n";
}
}
else {
newQ = (Q/2);
cout << "\nNew value of B is: " << newQ;
}
//check sum remainder of Z + X
if ((R % 2 == 1) && ((R+11) > 20)){
newR = (R-11);
newR = (newR/2);
cout<< ("\nNew value of C is: ") << newR << "\n";
if ((R % 2 == 1) && ((R + 11) < 20)) {
newR = (R+11);
newR = (newR /2);
cout<< ("\nNew value of C is: ") << newR << "\n";
}
}
else {
newR = (R/2);
cout << "\nNew value of C is: " << newR;
}
}//end of main
一部の(指定された)入力に対して、期待される出力と実際の出力は何ですか?デバッガでコードをステップバイステップで実行してみましたが、すべてが正常に動作することを確認しましたか? –
変数を 'newP'のようにゼロに初期化するだけで問題なく動作します。私は[CPPシェル](http://cpp.sh/)でそれをテストしました –
"else"という単語が何を意味するのか少し難解です。また、常に変数を初期化する習慣を身につけてください。 – molbdnilo