jeudi 27 mai 2021

Program accepting input from stdin but not from terminal

Here is my program

    #include <iostream>
using namespace std;

class Student
{
    string name;
public:
    Student(string input)
    {
        name = input;
    }
    Student()
    {
        name = "Unknown";
    }
    void get_name()
    {
        char c;
      
    while (getline(cin, name))
    { 
    for (int i = 0; i < name.length(); i++) {

        c = name.at(i);         

        if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || c==' ' || c=='\n') ) {

             name = "Unknown";
        }
     }
 }

    }
    void print_name()
    {
        cout << name << endl;
    }
};

int main()
{
        Student name;
        name.get_name();
        name.print_name();
    return 0;
}         

It is accepting input from stdin & working properly but not from the terminal in any way. I can't understand this behavior of the program.

Terminal input not working

https://i.stack.imgur.com/IyLRV.jpg

STDIN input

https://i.stack.imgur.com/Zj6fH.jpg

Aucun commentaire:

Enregistrer un commentaire