1

I'm doing an assignment that wants me to get the user to enter a students name and grade on a loop until they enter "quit". Then, display the results of each of the names and grades to the screen using a function called FormatName that uses a const string parameter. The format of the name the user inputs is assumed to be "First Last" and I have to reformat it to "Last, First" using the function.

But I keep getting error messages in my function and I can't figure out the right approach to take. I want to read the element of array [i], store it in lastfirst, then print it in reverse and print the grade[i] also.

If you can provide any guidance of what approach I should take to do this, please let me know.

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <iomanip> // only way xcode will let me use setfill and setw

using namespace std;

string FormatName(const string name1);
int main() {

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

    // loop to continue prompting user for name & grade if quit not entered
    for (i = 0; i < CAPACITY; i++) {
            cout << "Please input a name (or 'quit' to quit): ";
            getline(cin, name1[i]);

        //if quit, break
        if (name1[i].compare(quit) == 0) {
            break;
        }
        //continue loop and ask for grade if not
        cout << "Please input this person's grade: ";
        cin >> grade[i];
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    //if statement to not print chart to screen if no data entered
    if (name1[0].compare(quit) == 0) {
        cout << "\nYou did not enter any students." << endl;
        cout << "Goodbye!" << endl;
        return 1;
    }

    else {
    //display class scores to screen
    cout << "\n\nClass scores:" << endl;
    cout << setfill('-') << setw(60) << "-" << endl;

    cout << setfill(' ');
    cout << setw(50) << left << "\nName" << right << "Grade " << endl;
    cout << setfill('-') << setw(60) << "-" << endl;

    //call function with for loop to print each element 
    for (int j = 0; j < i; j++)
            FormatName(name1[i]);

    cout << '\n' << setfill('-') << setw(60) << "-" << endl;
    }
    return 0;
}

string FormatName(const string name1)
    {
    string lastfirst;
    int i;

    //set lastfirst equal to the first element of name array
    lastfirst = name1[0];

    for (string el &: lastfirst) // 2 errors saying "Expected ';' in 'for' 
                                 // statement specifier" & "Expected 
                                 // expression"
        cout << el << ',';

    //more code to make lastfirst[i] print to screen. 
    return lastfirst[i]; //error for obvious reason, I'm still working on it
    }

Edit:

I changed the function a little just to test what it prints to the screen, but it either prints out nothing at all when I return the value or it prints "q" when I cout << lastfirst. Any idea how to get it to return name1 instead of quit?

  string FormatName(const string name1)
    {
    string lastfirst;
    int i = 0;

        lastfirst = name1[i];
    //set lastfirst equal to the first element of name array
        cout << lastfirst;

    //more code to make lastfirst[i] print to screen.
    return lastfirst;
    }
  • Please provide a [mcve] – Passer By Apr 12 '18 at 01:07
  • I'm having a problem with the function at the bottom. I need to change the original name entered to "Last, First" instead of vice versa. But there's so many errors and I dont know what direction to go in to get this to print to the screen. – Holly Thorsted Apr 12 '18 at 01:12
  • You'd probably do well with a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), and not guess at an answer – Passer By Apr 12 '18 at 01:24
  • Get rid of the "&" in front of "el" in your for-loop. And use "auto" keyword instead of "string" in the same for-loop. Do compile it with "-std=c++11" flag. – Nakini Apr 12 '18 at 01:30
  • @TusharNakini I redid the function slightly, but it still wont print out the name1 function and is instead printing out "quit" one char at a time depending what I have i set to – Holly Thorsted Apr 12 '18 at 01:45
  • There is a suggestion for you -- use an IDE, such Eclipse/Qt-creator/Xcode etc. Then it becomes easier to debug. And debugging your own you will teach you more. For the bugs -- Take a look at your for loop, where you have accidentally used "i" instead of "j" while passing as an argument to the "FormatName" function. In addition, you are only accessing the first character in the "FormatName" function by doing "name1[0]". Is that something you want? If not, just use "name1" instead. – Nakini Apr 12 '18 at 03:37

0 Answers0