0

I have an assignment that requires me to write a program that prompts the user to enter the name of a student and their grade and keeps looping until they enter "quit".

But I can't figure out how to get the user input for the array to get the entire line (which is a first & last name so I can't just do cin >> name1[i] since theres white space) but when I use cin.getline or just getline and compile it, I get an error message saying No member function matching getline.

Also when I compile it without getline, its just a continuous loop and doesnt let me input any info for name or grade. I'm new to arrays and cstring so please try to dumb down where I'm messing up. Thank you.

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespace std;

int main() {

    const int CAPACITY = 50;
    string name1[CAPACITY];
    string grade[CAPACITY];
    char quit[]= "quit";
    int i;

    //for loop to get names and grades from user until quit is entered
    for (i = 0; i < CAPACITY; i++) {
        while (name1[i] != quit)
            cout << "Please input a name (or 'quit' to quit): ";
            getline(cin, name1[i]);

    //break if name1[i] = quit 
    if (name1[i].compare(quit) == 0) {
        break;
    }

    //continue loop if quit not entered and get the grade from that person
    cout << "Please input this person's grade: ";
    cin >> grade[i];
    }

    return 0;

}
  • https://stackoverflow.com/questions/18786575/using-getline-in-c – Manvir Apr 11 '18 at 20:11
  • `name1` is not an array of strings, it's an array of `char`, which is just one string. – Barmar Apr 11 '18 at 20:12
  • You can't use `==` to compare C strings, you have to use `strcmp()`. – Barmar Apr 11 '18 at 20:13
  • It seems like you need to learn the basics of how C strings work. If you need an array of 50 strings, it should be `char name1[50][MAXNAMESIZE];` – Barmar Apr 11 '18 at 20:15
  • @Barmar yes that was my mistake I just fixed it. I just reread the directions and it says to set the max with CAPACITY but it doesnt tell me what that capacity should be equal to. Im working on the strcmp function now – Holly Thorsted Apr 11 '18 at 20:20
  • Don't edit the question with these questions, it makes the answers invalid. – Barmar Apr 11 '18 at 20:29
  • Why did you change `char name1` to `string name`? Aren't you supposed to be using C strings, not `std::string`? – Barmar Apr 11 '18 at 21:35

2 Answers2

1

Declare name1 variable as std::string, Then just use std::cin:

std::string name1;
std::cin >> name1;

But if you really need to get entire line you always can do:

std::string line;
std::getline(std::cin, line);

And then use the line.

If your assignment really requires that you use cstrings you can:

char line[50];
std::cin.get(line, 50);
Rama
  • 3,222
  • 2
  • 11
  • 26
  • It sounds like his assignment requires him to use C strings, not `std::string`. – Barmar Apr 11 '18 at 20:13
  • It's an array of 50 names, not just one line. – Barmar Apr 11 '18 at 20:31
  • 1
    @Barmar, yes but since it is a task assignment, I only give the necessary tools to solve the problem. The rest consists of the particular exercise, what must be done by him. – Rama Apr 11 '18 at 20:34
1

Several issues:

  • For an array of C strings, you need char name1[50][MAXNAMESIZE];. You just declared a single string.
  • When reading into a C string, cin.getline() requires a length parameter to specify the maximum number of characters to input, so it doesn't overflow the buffer.
  • You don't need separate loops for the names and grades. Get the grade for each student immediately after getting their name.
  • To compare C strings you have to use strcmp(), not ==.
  • When you mix >> and getline(), you need to call cin.ignore() after >> to skip past the newline. See cin and getline skipping input

Code:

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespace std;

#define MAXNAMESIZE 100

int main() {

    char name1[50][MAXNAMESIZE];
    int grade[50];

    for (int i = 0; i < 50; i++) {

        cout << "Please input a name (or 'quit' to quit): ";
        cin.getline(name1[i], sizeof name1[i]);

        if (strcmp(name1[i], "quit") == 0) {
            break;
        }
        cout << "Please input this person's grade: ";
        cin >> grade[i];
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I used what you said but I still get an error saying "No matching member function for call to 'getline'" and when I compile its a never ending loop telling me to please enter a name and doesnt say anything about the grade – Holly Thorsted Apr 11 '18 at 21:31
  • I mistakenly copied your edited version of the question that changed `char` to `string`. It's fixed now. – Barmar Apr 11 '18 at 21:34
  • You were right the first time though, my professor wants us to create an array of string objects to represent the name. But still how come when it compiles its only a nonstop loop telling me to input the name but doesnt ever give me the option to enter it? – Holly Thorsted Apr 11 '18 at 21:39
  • Sorry just figured out what I was doing wrong, I still left in my while loop – Holly Thorsted Apr 11 '18 at 21:40