toupper string within array struct

I'm trying to convert a string of characters within an array structure but I keep getting errors whenever I try to compile the program. The code below isn't exactly what I have but it's basically the part I'm having problems with. It says "no matching function for call to 'toupper(std:string&)' "
Not sure if I'm converting the names to upper case correctly.

Within the "file.txt"
1
2
Bryan-Smith 12 2.1
Austin-Ryan 13 3.1


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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
const int N=2;

struct DATA{
    string Name;
    int Age;
    float Gpa;
};

void CopyRecords(fstream&file, DATA x[]){
        for (int i=0;i<2;i++){
            file >> x[i].Name >> x[i].Age >> x[i].Gpa;
        }
    file.close();
}

void DisplayRecord(DATA x[]){
    cout << "Name\t    Age    \t    GPA    \n"
         << "------------------------------------------" << endl;

    for (int i=0;i<2;i++){
        x[i].Name=toupper(x[i].Name);
        cout << x[i].Name << endl;
    }

}

int main(){
    DATA p[N];
    fstream inFile;

    //Opens text file
    inFile.open("file.txt",ios::in);

    //Reads data from "file.txt" into array p
    CopyRecords(inFile,p);

    //Displays all records, names in uppercase
    DisplayRecord(p);

    return 0;
}
Last edited on
Hey there, toupper is a single char function- so you'd have to use it on each individual character in the .Name
So I would have to create a while loop to read each char within the string then convert them, right?
Last edited on
You've got it!
Okay, I'm trying every method I can think of but I still can't understand what to do. Do I convert the string to char? I tried using the transform algorithm on the x[].Name but also getting an error.
Last edited on
You'll want to iterate through each char in the string like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <functional>
#include <string>

using namespace std;

int main(){

    auto upstring = [](const string& in)->string{
        string ret=in;
        for(int a = 0; a < ret.size(); a++){
                ret[a]=toupper(ret[a]);
        }
        return ret;
    };

    std::cout << upstring("Hello this was previously mostly lower case.");

}
Topic archived. No new replies allowed.