Loop not compiling correctly

My loop is not working
Last edited on
First: You code does not compile. Please correct the while loop.
Second: Do not mix c-style printf and c++-style cout (either one or the other).

So what are you trying to do? One name and 10(9?) GPA values?

However on line 43 the [r] is misplaced. It should be after g not name.
I am trying to get ten names, and ten GPA's then print the name beside the GPA respectively in two columns, while making sure the name is capitalized and lowercased correctly.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <iomanip>
#include <locale>
#include <vector>
#include <algorithm>
#include <limits>

constexpr auto SIZE = 10;

std::string& toUpperWord(std::string& s);

struct Student
{
    std::string m_name;
    double m_GPA;

    Student(std::string& name, const double& GPA) : m_GPA(GPA)
    {
        m_name = toUpperWord(name);
    }
};
std::ostream& operator << (std::ostream& os, const Student& s)
{
    os << std::left << std::setw(45) << s.m_name;
    os << std::setw(10) << std::fixed << std::setprecision(2) << s.m_GPA << '\n';

    return os;
}
int main()
{
    std::vector<Student> students;
    int i{};
    while (i < SIZE)
    {
        std::cout << "Enter student name \n";
        std::string name;
        getline(std::cin, name);
        std::cout << "Enter student GPA \n";
        double GPA;
        std::cin >> GPA;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        Student tmp(name, GPA);
        students.push_back(std::move(tmp));
        tmp.m_name = " ";
        tmp.m_GPA = 0.0;
        i++;
    }
    for (auto& elem : students)
    {
        std::cout << elem ;
    }
}
std::string& toUpperWord(std::string& s)
{
    std::locale loc;
    size_t count = s.size();
    if (count > 0)
    {
        s[0] = toupper(s[0], loc);
    }
    for(size_t i = 1; i < count; i++)
    {
        if (isspace(s[i - 1], loc))
            s[i] = toupper(s[i], loc);
        else
            s[i] = tolower(s[i]);
    }
    return s;
}
Topic archived. No new replies allowed.