-3

I found that I needed to start using getline(cin, input); for my user inputs. I figured out how to use stringstream to convert a string from a user into an int, so that I can store and use the numbers in math functions.

As an example, say you need to ask a user for a Student ID, you can easily store that as a string as it is rare that you would need to do any type of mathematical equations with it. However, if you were to ask for grades, that you would need to average out and convert to GPAs, that is another story.

I essentially would like to ask the user to input a number via getline, then convert the input into an int, but as a function so that I don't need to type the same deal out every time I need something converted.

Example:

#include<iostream>
#include<conio.h>
#include<string>
#include<sstream>

using namespace std;

class students{

    int s1, s2, s3;
    string name, id, input;

public:
    void getData(){
            cout << "Enter ID: ";
            getline(cin, id);

            cout << "Enter Name: ";
            getline(cin, name);

            while(true){
                cout << "Enter grades for Math: ";
                getline(cin, input);

                stringstream convert(input);
                if(convert >> s1)
                    break;
                cout << "Invalid Grade; Please Try Again! " << endl;
            }
            while(true){
                cout << "Enter grades for Science: ";
                getline(cin, input);

                stringstream convert(input);
                if(convert >> s2)
                    break;
                cout << "Invalid Grade; Please Try Again! " << endl;
            }
            while(true){
                cout << "Enter grades for English: ";
                getline(cin, input);

                stringstream convert(input);
                if(convert >> s3)
                    break;
                cout << "Invalid Grade; Please Try Again! " << endl;
            }
    }

    void showData(){
        cout << "\n" << id << "\t" << name << "\tMath: " << s1 << "\tScience: " << s2 << "\tEnglish: " << s3;
    }
};

int main(){
    students s[20];
    int i, numOfStudents;
    string input;

    while(true){
        cout << "\nNumber of Students? ";
        getline(cin, input);

        stringstream convert(input);
        if(convert >> numOfStudents)
            break;
        cout << "Invalid Grade; Please Try Again! " << endl;
    }

    for(i = 0; i < numOfStudents; i++){
        s[i].getData();
    }

    for(i = 0; i < numOfStudents; i++){
        s[i].showData();
    }

    _getch(); //Only there to keep the command line window open.
    return 0;
}
JRuxDev
  • 17
  • 1

4 Answers4

4

You need a function. Something like:

int getGrade(const std::string& subject)
{
   while(true){
        std::cout << "Enter grades for " << subject << ": " << std::flush;
        std::string input;
        std::getline(std::cin, input);

        std::stringstream convert(input);
        int result;
        if(convert >> result)
             return result;
        std::cout << "Invalid Grade; Please Try Again! " << std::endl;
    }
}

Usage would be something like:

s1 = getGrade("Math");
  • I see! Passing the subject as a string completely escaped me. Thank you so much! I am just kind of practicing for my own time, so this was just a little personal project that I am doing for myself to improve upon my coding. – JRuxDev Oct 24 '17 at 08:52
  • You could actually pass it as a `const char *` in this case - I just tend to default to `std::string` unless there is good reason otherwise. – Martin Bonner supports Monica Oct 24 '17 at 09:02
2

You can pass in the string by reference to const and utilize the std::stoi function:

int getGrade(const std::string& s) {
    try {
        int result = std::stoi(s);
        return result;
    }
    catch (std::invalid_argument) {
        std::cout << "Could not convert to integer.";
        return -1;
    }
}

and use it like the following:

int main() {
    int x;
    std::string s1;
    std::cout << "Enter grade: ";
    std::getline(std::cin, s1)        
    x = getGrade(s1);
}
Ron
  • 14,674
  • 4
  • 34
  • 47
0

Extract one of your loops to a different function and parametrize your "question text":

int get_input(const char* what)
{
     while(true)
     {
         cout << what;
         string input;
         getline(cin, input);

         int temp;
         stringstream convert(input);
         if(convert >> temp) return temp;

         cout << "Invalid input; Please Try Again! " << endl;
    }
}
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
0

What you want to do is extract the code that you copied into its own function like so:

...

int readGrade(const char* subject) {
    while(true) {
        cout << "Enter grade for " << subject << ": ";
        string input;
        getline(cin, input);

        stringstream convert(input);
        int n;
        if(convert >> n)
            return n;
        cout << "Invalid grade, please try again." << endl;
    }
}

class students{

    int s1, s2, s3;
    string name, id, input;

public:
    void getData(){
        cout << "Enter ID: ";
        getline(cin, id);

        cout << "Enter Name: ";
        getline(cin, name);

        s1 = readGrade("Math");
        s2 = readGrade("Science");
        s3 = readGrade("English");
    }

    void showData(){
        cout << "\n" << id << "\t" << name << "\tMath: " << s1 << "\tScience: " << s2 << "\tEnglish: " << s3;
    }
};

...
gonutz
  • 5,087
  • 3
  • 22
  • 40