1

My problem here is I don't know how to insert a rule wherein if a user inputted a number on the string, it will cout a warning saying it's not valid, same with if a user inputted a string/char on the grades. How? I've been trying it but the formula won't work.

int x, cstotal = 100, extotal = 150;

double scorecs, exscore, labtotala, labtotalb, total;

string mystr = "";

cout << "Compute for: " << "\n" << "1. Laboratory Grade " << "\n" << "2. Lecture Grade" << "\n" << "3. Exit" << "\n";
cout << "Please enter a number: ";
cin >> x;
switch (x) {
case 1:

    cout << "Compute for laboratory grade." << "\n";
    cout << "Enter Student Name: ";
    cin >> mystr;


    cout << "Good day, " << mystr << " . Please provide the following grades: " << "\n";
    cout << "CS Score: ";
    cin >> scorecs;

    cout << "Exam Score: ";
    cin >> exscore;


    labtotala = scorecs / cstotal * 0.6;
    labtotalb = exscore / extotal * 0.4;
    total = labtotala + labtotalb;
    cout << "Your Laboratory Grade is " << total * 100 << "\n";
    system("pause");
    break;
case 2:
    cout << "Compute for lecture grade." << "\n";
    cout << "Enter Student Name: ";
    cin >> mystr;
    cout << "Good day, " << mystr << " . Please provide the following grades: " << "\n";
    cout << "CS Score: ";
    cin >> scorecs;
    cout << "Exam Score: ";

    cin >> exscore;
    labtotala = scorecs / cstotal * 0.7;
    labtotalb = exscore / extotal * 0.3;
    total = labtotala + labtotalb;
    cout << "Your Lecture Grade is " << total * 100 << "\n";
    system("pause");
    break;
2
  • What part of the rule is giving you trouble? How to implement the detection, control flow, branching? If it's just a matter of detecting if a string is a number, you could loop through each character and call isdigit. Commented Nov 18, 2012 at 14:05
  • you need to be more specific as to what you are looking for. Post less code if possible. Commented Feb 9, 2016 at 1:55

6 Answers 6

14

cin sets a failbit when it gets input of an invalid type.

int x;
cin >> x;

if (!cin) {
    // input was not an integer
}

You can also use cin.fail() to check if the input was valid:

if (cin.fail()) {
    // input was not valid
}
Sign up to request clarification or add additional context in comments.

Comments

4

How about something like this:

std::string str;
std::cin >> str;

if (std::find_if(str.begin(), str.end(), std::isdigit) != str.end())
{
    std::cout << "No digits allowed in name\n";
}

The above code loops through the whole string, calling std::isdigit for each character. If the std::isdigit function returns true for any character, meaning it's a digit, then std::find_if returns an iterator to that place in the string where it was found. If no digits were found then the end iterator is returned. This way we can see if there was any digits in the string or not.

The C++11 standard also introduced new algorithm functions that can be used, but which basically does the above. The one that could be used instead is std::any_of:

if (std::any_of(str.begin(), str.end(), std::isdigit))
{
    std::cout << "No digits allowed in name\n";
}

3 Comments

So will this check whether or not an int is inside of the input? if so will you please explain your code?
@DylanLittle Updates my answer trying to explain what it does, and with links to references. Also added a "better" solution when using C++11 or later.
ahhh so much better. I can see what's going on now.
3
    cout << "\n Enter number : ";
    cin >> ch;
    while (!cin) {
        cout << "\n ERROR, enter a number" ;
        cin.clear();
        cin.ignore(256,'\n');
        cin >> ch;
    }

Comments

1

Use the .fail() method of the stream. Something like below:-

   cin >> aString;

  std::stringstream ss;
  ss << aString;
  int n;
  ss >> n;

  if (!ss.fail()) {
   // int;
  } else {
  // not int;
   }

Comments

1

you could use cin.fail() method! When cin fails it will be true and you could us a while loop to loop until the cin is true:

cin>>d;
while(cin.fail()) {
    cout << "Error: Enter an integer number!"<<endl;
    cin.clear();
    cin.ignore(256,'\n');
    cin >> d;
}

1 Comment

It shold be just cin.ignore() in new compiler. cin.ignore(256,'\n') doesn’t work in updated compiler
0
//this program really work in DEV c++
#include <iostream> 
using namespace std; 
int main()
{
    char input;
    cout<<"enter number or value to check"<<endl;
    cin>>input;
for(char i='a';i<='z';i++)
{
    if(input==i)
    {
        cout<<"character"<<endl;
        exit(0);
    }
}
for(int i=0;i<=1000;i++)
{
    if(input==i)
    {
        cout<<"number"<<endl;   
    }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.