Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 控制台过早关闭。编辑:总和和平均值计算不正确_C++_C++11 - Fatal编程技术网

C++ 控制台过早关闭。编辑:总和和平均值计算不正确

C++ 控制台过早关闭。编辑:总和和平均值计算不正确,c++,c++11,C++,C++11,只要我写了一个无效的输入,这段代码就可以正常工作。错误消息打印出来,您只需关闭控制台并重新开始。但是,我可以让程序在控制台打开的情况下从头开始吗?此外,最大的失败和主要问题是,当输入有效输入时,控制台只是关闭。我不知道为什么,我对C++是新的,所以请容忍我。p> 编辑:既然@merlin2011好心地指出了我的超级简单的错误,这个程序做得更好了!万分感谢!但是,街上的新问题是我的计算不正确。我打印了我的总和,看看它是否正确,我想因为我有int字母=0;,它将总和计算为0。我想我需要把它改成一个参

只要我写了一个无效的输入,这段代码就可以正常工作。错误消息打印出来,您只需关闭控制台并重新开始。但是,我可以让程序在控制台打开的情况下从头开始吗?此外,最大的失败和主要问题是,当输入有效输入时,控制台只是关闭。我不知道为什么,我对C++是新的,所以请容忍我。p> 编辑:既然@merlin2011好心地指出了我的超级简单的错误,这个程序做得更好了!万分感谢!但是,街上的新问题是我的计算不正确。我打印了我的总和,看看它是否正确,我想因为我有int字母=0;,它将总和计算为0。我想我需要把它改成一个参考整数?所以int&letters=0;,但是我怎样才能让它存储它的新价值呢

#include <iostream>
using namespace std;

//Prototype to keep console from closing.
class KeepRunning {
  public:
    ~KeepRunning() {
      cin.get();}};

//Define batting values
#define H  1
#define h  1
#define O  1
#define o  1
#define W  0
#define w  0
#define S  0
#define s  0
#define P  0
#define p  0

int main ()
{
    KeepRunning kr;

    int player;                 //Assign player number
    double sum;                 //Assign variable for sum of H, h and O, o
    double sumHits;             //Assign variable for sum of only H and h
    double average;             //Assign variable for average of H and O
    char size[100];             //Allows compiler to view user input as array
    int b=0;                    //Assign variable for integer size
    int letters = 0;            //Assing value of 0 to allow compiler to count

    cout << "\t\t\tBatting Average Calculator\t\t";

    do
    {
         cout << "\n\nEnter the player's number: ";
         cin >> player;

         cout << "Enter the player's batting record: ";
         cin >> size;

         if (size[b] != 'H' && size[b] != 'h' &&
             size[b] != 'O' && size[b] != 'o' &&
             size[b] != 'W' && size[b] != 'w' &&
             size[b] != 'S' && size[b] != 's' &&
             size[b] != 'P' && size[b] != 'p' || cin.fail()) 
          {
            cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'.Please try again.\n";
            cin.clear();
            cin.ignore(); 
          }            
    } while (size[b] != 'H' && size[b] != 'h' &&
             size[b] != 'O' && size[b] != 'o' &&
             size[b] != 'W' && size[b] != 'w' &&
             size[b] != 'S' && size[b] != 's' &&
             size[b] != 'P' && size[b] != 'p' || cin.fail()); 
             return 0;  

    //Summate H, h, O, o
    sum = letters;

    //Summate 
    sumHits = H + h;

    //Calculate batting average
    average = sumHits/sum;

    cout << "\nPlayer " << player << "'s batting record: " << size << endl;
    cout << "Player " << player << "'s batting average: " << average << endl;

    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );    
}
#包括
使用名称空间std;
//原型,以防止控制台关闭。
类修剪{
公众:
~KeepRunning(){
cin.get();}};
//定义击球值
#定义h1
#定义h1
#定义O 1
#定义o 1
#定义W 0
#定义w 0
#定义s0
#定义s0
#定义p0
#定义p0
int main()
{
持续修剪kr;
int player;//分配播放器编号
double sum;//为H,H和O,O的和赋值变量
double sumHits;//仅为H和H的和分配变量
双重平均;//为H和O的平均值分配变量
字符大小[100];//允许编译器以数组形式查看用户输入
int b=0;//为整数大小分配变量
int letters=0;//赋值为0以允许编译器计数
cout播放器;
大小;
如果(大小[b]!='H'&大小[b]!='H'&&
尺寸[b]!=“O”&尺寸[b]!=“O”&&
尺寸[b]!=“W”&尺寸[b]!=“W”&&
大小[b]!=“S”&大小[b]!=“S”&&
大小[b]!='P'&大小[b]!='P'| | cin.fail()
{
无法从哪里开始

  • 正如@merlin2011所说,任何低于“return0;”行的内容都不会执行
  • 用户输入没有被真正使用(除了验证)。我不知道你想做什么,但是我想说的是,
    size
    内容应该对
    sumHits
    有一定的影响
  • 字母
    在任何地方都不会增加。它的定义很好(不要碰它!)…只需要为平均值中的每个项目增加它
  • b
    在任何地方都不会增加。我可以猜到您的输入是一个字母字符串,每个字母都有某种意义……因此您可能在b上缺少一个循环,该循环遍历字母字符串,增加
    字母
    ,并根据用户输入的不同字母向
    sumHits
    添加值
  • 顺便说一句

    修改代码:

    #include <iostream>
    #include <limits> // added because of the numeric limits at the end
    #include <cstring> // added because of strlen
    using namespace std;
    
    //Prototype to keep console from closing.
    //This is probably not needed anymore
    class KeepRunning {
      public:
        ~KeepRunning() {
          cin.get();}};
    
    
    int main ()
    {
        KeepRunning kr; // not sure if this is needed
    
        int player;                 //Assign player number
        double sumHits;             //Assign variable for sum of only H and h
        double average;             //Assign variable for average of H and O
        char size[100];             //Allows compiler to view user input as array
        int b=0;                    //Assign variable for integer size
        int letters = 0;            //Assing value of 0 to allow compiler to count
    
        // probably the above variables can be moved inside the loop.
    
        cout << "\t\t\tBatting Average Calculator\t\t";
    
        do
        {
             cout << "\n\nEnter the player's number: ";
             cin >> player;
    
             cout << "Enter the player's batting record: ";
             cin >> size;
    
             // Now size should probably be a std::string... 
             // Now we have an input, loop through it
             // initialize variables
             letters = 0;
             sumHits = 0.0;
             for(b=0;b<strlen(size);b++) {
                  // Now check each letter
                  switch(size[b]) {
                  case 'H': // fallthrough
                  case 'h':
                     sumHits+=1;
                     letters++;
                     break;
                  case 'O': // fallthrough
                  case 'o':
                     letters++;
                     break;
                  case 'S': // fallthrough
                  case 's': // fallthrough
                  case 'P': // fallthrough
                  case 'p': // fallthrough
                     // not sure what these are... so just skip
                     break;
                  default:
                     // syntax error
                     cout << "\nAcceptable batting record codes are: 'H','O','W','S','P'.Please try again.\n";
                     cin.clear();
                     cin.ignore();
                     letters=0; // reset the count so that we don't process the average
                  }
              }
              if(letters==0) {
                  // we didn't get a valid input, start the loop again
                  continue;
              }
              //Calculate batting average
              average = sumHits/letters;
    
              cout << "\nPlayer " << player << "'s batting record: " << size << endl;
              cout << "Player " << player << "'s batting average: " << average << endl;
    
              std::cout << "Press ENTER to continue...";
              std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
         }
         while(!cin.fail()); // repeat until the standard input ends.
    }
    
    #包括
    #include//由于末尾的数字限制而添加
    #包含//由于strlen而添加
    使用名称空间std;
    //原型,以防止控制台关闭。
    //这可能不再需要了
    类修剪{
    公众:
    ~KeepRunning(){
    cin.get();}};
    int main()
    {
    KeepRunning kr;//不确定是否需要这样做
    int player;//分配播放器编号
    double sumHits;//仅为H和H的和分配变量
    双重平均;//为H和O的平均值分配变量
    字符大小[100];//允许编译器以数组形式查看用户输入
    int b=0;//为整数大小分配变量
    int letters=0;//赋值为0以允许编译器计数
    //也许上述变量可以在循环内移动。
    cout播放器;
    大小;
    //现在大小应该是std::string。。。
    //现在我们有了一个输入,循环通过它
    //初始化变量
    字母=0;
    sumHits=0.0;
    
    对于(b=0;行
    return 0;
    下面的任何东西都不会执行。我真是太蠢了。哈。是的,我移动了return语句,这就是为什么我能够继续编辑问题的原因。我试图将sum设置为size,但我做不到,因为它说我试图将char类型转换为double,我尝试实现了其他东西:/。如何我可以在不干扰编译器的情况下使用大小吗?是的,输入必须是一系列已定义的字母lol。我可以使用枚举器而不是#define???吗?我对这一点非常陌生,所以我很抱歉这些“愚蠢”的问题。@她说的是,我真的不明白你们想做什么……如果每个字母都是大小[]数组意味着什么,那么你可能需要在数组中循环,检查每个字母(可能是一个开关盒?)并相应地增加sumHits和索引。至于使用什么来代替定义:无论什么对你来说更方便,请查看我发布的FAQ链接……但还有其他选项可以帮助你更好地将字母映射到其含义。我将所有定义都改为const chars,我正在尝试编写一个计算这是球员的平均击球率。所以只有H,H(命中率)和O,O(出局率)才有值.Sum应该找到这个。sumHits应该只求命中数。平均数是命中数/命中数和出局数的总和。@whatsHerFace阅读了我的评论…对不起,但我不打算再写了。对我来说,这看起来像是一个家庭作业。向您提供工作代码不会帮助您学习。编程没有什么神奇之处,只需输入对程序必须遵循的详细过程进行编码,所以只需用文字(伪代码)写下你需要做的事情,这可能会帮助你把它翻译成C++代码。