user1793565
user1793565

Reputation: 31

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.

   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');

Upvotes: 2

Views: 2961

Answers (7)

Tariq M Nasim
Tariq M Nasim

Reputation: 1278

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;
}

Upvotes: 0

Casper Beyer
Casper Beyer

Reputation: 2301

First of all, single quotes denote a character literal, double quotes a null terminated string (2.14 or so).

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.

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, e.g is it an integer or string, the act on that data. to terminate you can call exit, break from the loop or return from the function.

Upvotes: 0

spin_eight
spin_eight

Reputation: 4025

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). "when done - enter -1".
Also if(grade = 'done') - assignement is here, to compare use operator==, which is

// 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');

Upvotes: 0

Strike
Strike

Reputation: 248

If you really want to use numbers AND strings i suggest using 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

Upvotes: 0

john
john

Reputation: 87944

You have write code to enter a string. You can then test if the string equals "done". If it doesn't you then convert the string to an integer, then you can check against your grade boundaries.

Upvotes: 0

Zoran Marjanovic
Zoran Marjanovic

Reputation: 365

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. 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".

Upvotes: 0

Kiith Nabaal
Kiith Nabaal

Reputation: 466

just do a return 0? Also, you can't do grade = 'done', unless you overrode the = operator or something.

Upvotes: 1

Related Questions