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;
}