简体   繁体   English

如何使用用户输入停止程序的特定部分?

[英]How do I stop a certain part of a program using the user's input?

I'm making a program that tallies grades. 我正在编写一个计分的程序。 I want to know how to stop a certain part of a program based on the user's input. 我想知道如何根据用户输入停止程序的某个部分。 Below is the program I am working on. 下面是我正在处理的程序。 How do I stop the program if the user enters "done"? 如果用户输入“完成”,如何停止程序? Also, I don't necessarily have to use "done" to exit the program. 另外,我不一定必须使用“完成”来退出程序。 I was initially going to use -1, but I ran into a problem where I had to make a robust program where values < 0 and > 100 are not accepted. 我最初打算使用-1,但是遇到一个问题,我不得不制作一个健壮的程序,其中值<0和> 100不被接受。

   int grade;
   a=b=c=d=f=0;


   do
   {
      cout << "Enter a grade or enter done to stop. ";


      if (grade >= 90 && grade <= 100)
          {a++;}
      if (grade >= 80 && grade < 90)
          {b++;}
      if (grade >= 70 && grade < 80)
          {c++;}
      if (grade >= 60 && grade < 70)
          {d++;}
      if (grade >= 0 && grade < 60)
          {f++;}



   } while (grade != 'done');

just do a return 0? 只是返回0? Also, you can't do grade = 'done', unless you overrode the = operator or something. 另外,除非您改写了=运算符或其他内容,否则您不能进行grade ='done'。

The most simple way to ask user to enter integer value , which is out of range of values you expected him to enter (ex. -1). 要求用户输入整数值的最简单方法,该值超出了您希望用户输入的 范围 (例如-1)。 "when done - enter -1". “完成时-输入-1”。
Also if (grade = 'done') - assignement is here, to compare use operator== , which is 同样如果(grade = 'done') - 分配在这里,要比较use operator == ,这是

// if(grade == 'done') // if(grade =='done')

or you can use the following approach: 或者您可以使用以下方法:

do
   {
      cout << "Enter a grade or enter done to stop. ";
      // first try to get string from user (which he/she enters when done)
      string str;
      cin >> str;
      if (str == "done") // if done was entered - exit from the loop
         break;
      // else clear fail bit in stream object and read int value
      cin.clear();
      cin >> grade;

      if (grade >= 90 && grade <= 100)
          {//a++;}
      if (grade >= 80 && grade < 90)
          {//b++;}
      if (grade >= 70 && grade < 80)
          {//c++;}
      if (grade >= 60 && grade < 70)
          {//d++;}
      if (grade >= 0 && grade < 60)
          {//f++;}

   } while (grade != 'done');

Firstly, "grade" is type int, so you can not convert int to *char, if you want that "grade" is int than you can exit with -1. 首先,“ grade”是int类型,因此,如果您希望“ grade”是int,则不能将int转换为* char,而可以使用-1退出。 Or if you want to exit with "done", than grade can be char[5], and you can use atoi() to convert string to integer to check, and strcmp() to compere "grade" with "done". 或者,如果要退出“ done”,则等级可以是char [5],并且可以使用atoi()将字符串转换为整数以进行检查,并使用strcmp()来将“ grade”转换为“ done”。

You have write code to enter a string. 您已编写代码以输入字符串。 You can then test if the string equals "done". 然后,您可以测试字符串是否等于“ done”。 If it doesn't you then convert the string to an integer, then you can check against your grade boundaries. 如果不是,则将字符串转换为整数,然后可以对照成绩边界进行检查。

If you really want to use numbers AND strings i suggest using stringstreams. 如果您真的想使用数字和字符串,我建议使用stringstreams。 Here is an example: 这是一个例子:

string Text = "456";//string containing the number
int Result;//number which will contain the result

stringstream convert(Text); // stringstream used for the conversion initialized with the  contents of Text

if ( !(convert >> Result) )//give the value to Result using the characters in the string
    Result = 0;//if that fails set Result to 0
//Result now equal to 456 

Source link 源链接

First of all, single quotes denote a character literal, double quotes a null terminated string (2.14 or so). 首先,单引号表示字符文字,双引号表示以null终止的字符串(大约2.14)。

if(grade = 'done') will always be true, compiler should have thrown a warning at you that you aren't doing what you think you are doing, you're assigning it then checking if it is true, which it will always be. if(grade = 'done')始终为true,编译器应该向您发出警告,告知您您没有按照自己的意愿进行操作,请对其进行分配,然后检查它是否为true,它将始终是。

And you're trying to assign an integer to a string. 并且您正在尝试为字符串分配一个整数。

This seems like homework so i won't write it, but you need to take in a string from stdin, then parse it, eg is it an integer or string, the act on that data. 这似乎是作业,所以我不会编写它,但是您需要从stdin接收一个字符串,然后解析它,例如,它是整数还是字符串,作用于该数据。 to terminate you can call exit , break from the loop or return from the function. 要终止,您可以调用exitexit循环或从函数返回。

You can take the inputs as string, if the first character is not a digit then break the loop, covert the string to integer otherwise: 您可以将输入作为字符串,如果第一个字符不是数字,则中断循环,否则将字符串转换为整数:

#include<iostream>
#include<string>
#include<cctype>
#include<cstdlib>


using namespace std;

int main() {
    int grade;
    string input;
    int a, b, c, d, f;
    a=b=c=d=f=0;


    do
    {
        cout << "Enter a grade or enter done to stop: ";
        cin >> input;
        cout << endl;
        if(!isdigit(input[0])) {
            break;
        } else {
            grade = atoi(input.c_str());
        }

        if (grade >= 90 && grade <= 100)
          {a++;}
        if (grade >= 80 && grade < 90)
          {b++;}
        if (grade >= 70 && grade < 80)
          {c++;}
        if (grade >= 60 && grade < 70)
          {d++;}
        if (grade >= 0 && grade < 60)
          {f++;}


    } while (1==1);

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;
    cout << "d = " << d << endl;
    cout << "f = " << f << endl;

    getchar();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM