2014-03-04 59 views
-8

我做了一個「程序」,只是說歡迎!鍵入你想要添加到對方的兩個號碼: 那裏你輸入兩個數字,然後你得到答案了...... 當這樣做完成後,它說:按任意鍵繼續。 。 。 當您按下某個鍵時,程序關閉,但是當您按任何鍵時,我希望它重新啓動... 我該怎麼做?我使用Microsoft Visual Studio Express的2013年Windows桌面... 索緒爾是C++如何循環變量?

這是我的代碼:

#include <iostream> 
    #include <limits> 
    #include <cstdio> 
    using namespace std; 

    int Add(int x, int y) 
    { 


     cout << "Calculating the sum of " << x << " + " << y << "\n"; 
     return (x + y); 
    } 

    int main() 
    { 
     cout << " Welcome!\n"; 
     int a, b, c; 
     cout << "Type two numbers you want to be added to each other: "; 
     cin >> a; 
     cin >> b; 
     c = Add(a, b); 
     cout << "The answere is: " << c; 
     cout << "\nShutting down....\n\n"; 
     system("pause"); 
     return 0; 
    } 
+0

在main()中使用while循環http://www.cplusplus.com/doc/tutorial/control/ – CoryKramer

+10

您需要閱讀一本書。 – Shoe

+3

我看到人們並沒有抓住低掛的水果;) – CoryKramer

回答

0
 int main() 
    { 
     cout << " Welcome!\n"; 
     int a, b, c; 
     while (true) 
     { 
      cout << "Type two numbers you want to be added to each other: "; 
      cin >> a; 
      cin >> b; 
      c = Add(a, b); 
      cout << "The answere is: " << c; 
      cout << "\nPress a key to go again....\n\n"; 
      system("pause"); 
     }; 
     return 0; 
    } 
0

你可以做這樣的事情:

 int main() 
    { 
     cout << " Welcome!\n"; 
     int a, b, c; 

     while(true) { 
      cout << "Type two numbers you want to be added to each other: "; 
      cin >> a; 
      cin >> b; 
      c = Add(a, b); 
      cout << "The answere is: " << c; 
      cout << "\nPress any key to continue\n"; 
      system("pause"); 
     } 
     return 0; 
    } 
0

在退出程序之前使用do while while循環提及您的條件 ,以便您可以確定何時繼續以及何時退出

+0

這應該是一條評論! –

+0

@πάνταῥεῖ這與代碼專用答案有什麼不同? – Shoe

+3

@πάνταῥεῖ截至此刻,這個答案實際上比你自己的描述更具描述性。 – Shoe

0

我不知道我理解你的問題,但我認爲這就是你要找的。有一個確定程序是否會循環的布爾值。

int main() { 
    // stillRun is true while we want to keep looping the program 
    boolean stillRun = true; 

    while(stillRun) { 
     runProgram() ; // this function has all the other code in your old main() function 
     cin >> stillRun ; 
    } 
} 
3

要循環,您可以使用while

例如:

while (false) { 
    std::cout << "You will never see this output" << std::endl; 
} 

bool loop = true; 
while (loop) { 
    std::cout << "Type 'quit' to quit this loop." << std::endl; 
    std::string input; 

    // This will grab a *single word* from the input. If you want a line, look 
    // at std::getline 
    std::cin >> input; 
    if (input == "quit") { 
     loop = false; 
    } 
} 

while (true) { 
    std::cout << "This will be repeated forever" << std::endl; 
} 

還有其它兩種形式,do while

std::string input; 
do { 
    std::cout << "Type 'quit' to quit." << std::endl; 
    std::cin >> input; 
} while (input != "quit"); 

...和for(其通常用於循環在規定的事情列表):

for (size_t i = 0; i < 10; ++i) { 
    std::cout << i << " out of 10" << std::endl; 
} 

從技術上講,您可以使用任何這些循環類型紐約樣的循環,但我懷疑你想要的類型是兩個標準的無限循環(你喜歡哪一個)中的任意一個:

while (true) { 
    // stuff to repeat forever 
} 

for (;;) { 
    // stuff to repeat forever 
} 

...或do while循環類似於上面的do { ... } while (input != "quit");循環。

+0

你也可以使用'for'和'do-while'。 – Shoe

+0

@Jefffrey我只是在輸入:D –