我正在嘗試編寫一個程序來擺脫數字的第一個和最後一個數字。對於最後一位數字,通過10次解決這個問題。我需要找到一種方法來使用%來刪除第一個數字,但是似乎我的邏輯已關閉,我的程序運行但它無法正常工作。查看邏輯中的任何錯誤?C++刪除數字的第一個和最後一個數字
#include <iostream>
using namespace std;
int main() {
int x;
int y;
cout << "Enter a number to have the first and last didgets removed" << endl;
cin >> x;
x /= 10;
y = x;
int count = 0;
while (y > 1)
{
y /= 10;
count++;
}
int newNum = x %(10^(count));
cout << newNum << endl;
cin.ignore();
cin.get();
return 0;
}
非常感謝,我現在知道了。我必須改變的唯一代碼是int newNum = pow(10,count); \t \t x%= newNum; \t \t cout << x << endl;感謝您的幫助。 – 2014-09-06 01:06:47
@XerradAnon我不會去'pow'。如果由於四捨五入,你會得到'9999.99 ...'而不是'10000'。即使這在實踐中沒有發生,這種方法仍然是可疑的和冒險的。 – AlexD 2014-09-06 01:11:04
@XerradAnon另外'while(y> 1)'看起來不正確。嘗試使用以'1'開頭的數字和其他數字進行測試。 – AlexD 2014-09-06 01:28:04