-3

嗨,我有 C++ 的问题

#include <iostream>  
#include <cstdlib>  

using namespace std;  

int main(int argc, char *argv[])  
{  
  float a,b,wynik;  

  cout << "quotient" << endl   
       << "..." << endl << endl   
       << "quotient 2 numbers."  
       << "\ndivisior does not equal 0"<< endl << endl;  

  cout << "a=";  
  cin >> a;  

  cout << "b=";  
  cin >> b;  


  if (b!=0)  
      cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";  
  else  
    cout <<"\ndivisior does not equal 0!\n\n";  

  system("PAUSE");    
  return 0;  
} 

当有人尝试商数除以 0 时,我必须使用 for 或 while。

4

2 回答 2

0

Your terms are wrong, see Wikipedia definition of "quotient".

#include <iostream>

int main(void)
{
    char   prompt[] =
        "\n"
        "Division, dividend / divisor = quotient:\n"
        "    Enter divisior: ";
    int divisior;
    int dividend;
    while (true)
    {
        std::cout << prompt;
        std::cin >> divisor;
        if (divisor == 0)
        {
            std::cout << "\n* Divisor is zero, try again.\n";
            continue;
        }
        std::cout << "\n    Enter dividend: ";
        std::cin >> dividend;
        std::cout << "\nResult of "
                  << dividend
                  << " / "
                  << divisor
                  << " is, using integer division, "
                  << dividend / divisor
                  << "\n";
        break;
    }
    return EXIT_SUCCESS;
}
于 2013-09-26T19:41:20.753 回答
0

b如果我理解正确,如果为零,您可能想再次要求输入

你可以有这样的东西:

do {

  cout << "quotient" << endl   
       << "..." << endl << endl   
       << "quotient 2 numbers."  
       << "\ndivisior does not equal 0"<< endl << endl;  

  cout << "a=";  
  cin >> a;  

  cout << "b=";  
  cin >> b;  

  if (b!=0)  
      cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";  
  else  
    cout <<"\ndivisior does not equal 0!\n\n";  
}while(b==0);
于 2013-09-26T18:55:20.527 回答