2013-09-24 27 views
1

感謝您昨晚的幫助,我能夠讓我的程序正確計算我的輸入,但現在我無法正確格式化輸出。這是問題:無法從C++循環實現所需的輸出(cout)

我的程序應該只打印具有素數的行中的「素數」。不過打印在以往一行:

http://oi42.tinypic.com/30igbvq.jpg

我不能爲我的生命弄清楚爲什麼它是這樣做的,我所有的功能應該工作。

Stack我需要你的幫助了!

#include <iostream> 
using namespace std; 


void primecheck(int x); // proto 
void countr(int rnm, int lnm); // proto 

void prime(int x) // this function finds the prime factors of a number. 
{ 
int lastr = 2; 
int count = 0; 
while (lastr < x) 
{ 

    if (x % lastr == 0) 
    { 
     cout << x << " " << lastr << "*"; 
     x /= lastr; 
    } 
    else 
     ++lastr; 
} 
primecheck(x); // calls to check if number is prime, "Is prime" 
} 


void console(int rnum, int lnum) // this prompts the user for two numbers then stores  the answers 
{ 
cout << "please enter two numbers "; 
cin >> rnum; 
cin >> lnum; 
countr(rnum, lnum); 
} 

void countr(int rnm, int lnm) // this function loops the prime function until all the  numbers are computed 
{ 
int i = rnm; 
do{ 
    prime(i); 
    i++; 
} while (i <= lnm); 
    return; 
} 


int main() // main, calls console then pauses when finished 
{ 
int e = 0; 
int r = 0; 
    console(e, r); 
    system("PAUSE"); 
} 
void primecheck(int x) // checks to see if then number is prime. if counter is equal to 2 than number is prime. 
{ 
int counting = 0; 
for (int a = 1; a <= x; a++) 
{ 
    if (x %a == 0) 
    { 
     counting++; 
    } 
} 

if (counting == 2) 
{ 
    cout << x << " is prime " << endl; 
} 
else 
{ 
    cout << x << endl; 
} 
} 
+1

爲什麼截圖時只能複製控制檯文本? – syam

回答

2

您在prime()中使用/=運算符。這是一個賦值運算符,並且修改了的值x,使得x總是在首先調用primecheck()時爲首要。

+0

omg非常感謝你!我編輯了代碼 void prime(int x)//此函數查找數字的主要因素。 { \t int lastr = 2; \t int z = x; \t而(lastr user2808943

+0

樂意提供幫助。我希望你現在能夠暢通無阻。您可以隨時點擊該綠色複選標記。 ;-) –