sorting upper and lowercase names together

I don't understand these errors

send the name to check dupe function

could not convert '(std::string*)(& tempName)' from 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
64 | else if (checkDuplicate(tempName, rndName, nrndNames))

i want to temporary change all characters to lower case to check if name is a dupe

148 | transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower
| ^~~~~
nameSorter1.cpp:148:40: error: request for member 'end' in 'rndName', which is of pointer type 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} (maybe you meant to use '->' ?)
148 | transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower
| ^~~
nameSorter1.cpp:148:55: error: request for member 'begin' in 'rndName', which is of pointer type 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} (maybe you meant to use '->' ?)
148 | transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  string usersFile; //stores users file name
  const int MAX_RNDNAMES = 10; //list size
  int nrndNames = 0; //list capcacity
  string rndName[MAX_RNDNAMES]; //name array
  string tempName[MAX_RNDNAMES]; //name array
  int u;
  string tempVar;

  //user introduction
  introduction(objective, instructions);

  // get user file name
  getline(cin, usersFile);

  // open input file
  ifstream fin; //
  fin.open(usersFile.c_str()); //
  if (!fin.good()) throw "I/O error"; //
  
  // getnames
  for (int i = 0; i < MAX_RNDNAMES; i=i+1)
  {
    getline(fin, tempName[nrndNames]);
    if (!fin.good())
    {
    break;
    }
    else if (checkDuplicate(tempName, rndName, nrndNames))
    {
    bool result = false;
    cout << "you already entered " << tempName << " -- skipping -- ";
    i=i-1;
    }
    else if ((tempName[nrndNames].length() > 0 ))
    {
    rndName[nrndNames] = tempName[nrndNames];
    cout << rndName[nrndNames] <<endl << endl;
    nrndNames++;
    }
    else if (tempName[nrndNames].length()==0 )
    {
    i=i-1;
    }  
  }
  
  //close input file
  fin.close();
  
  // open output file
  ofstream fout; //reserve "fout" for use
  fout.open ("friends.txt"); //open specifed file
  if (!fout.good()) throw "I/O error"; //if file isnt open output error to console

  //sort
  for (int j = 0; j < nrndNames; j++) //outer loopp tranverse
  {
    for (int o = j + 1; o < nrndNames; o++)
    { 
      if (rndName[j] > rndName[o])
      {
        tempVar = rndName[j]; 
        rndName[j] = rndName[o];
        rndName[o] = tempVar;
      }
    }
  }

  //output
  for (u = 0; u < nrndNames; u++)
  {
    cout << rndName[u] <<endl;
    cout << endl;
    fout << rndName[u] <<endl;
    fout << endl;
  }

  //close output file
  fout.close();

bool checkDuplicate(string tempName, string* rndName, int n)
{
  //data
  //guess is the list of non-duplicate names
  //n is the size counter of the list
  //uG is the name being checked
  bool result = false; //false is not a duplicate, true is a duplicate guess
  int i; //loop index for traversing the list

  //check if duplicate name
  for(i=0; i < n; i = i + 1)
  {
    transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower
    if (tempName == rndName[i])
    {
      result = true;
    }//if
  }//for i
  return result;
}//checkDuplicate
could not convert '(std::string*)(& tempName)' from 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
64 | else if (checkDuplicate(tempName, rndName, nrndNames))

let me take a few words out of that for you:

could not convert from 'std::string*' to 'std::string'
---------------------------^^^^^^-------^^^^^^^^

the compiler should tell you what line that is in your code. Fix that first.
It is probably where you call checkduplicate() function, you likely call it with string,string instead of string, string* (why the heck is it a pointer anyway?! did you mean reference?)
Last edited on
nameSorter1.cpp:64:45: error: expected primary-expression before '*' token
64 | else if (checkDuplicate(tempName, string* rndName, nrndNames))
| ^
| ^
nameSorter1.cpp: In function 'bool checkDuplicate(std::string, std::string*, int)':
nameSorter1.cpp:148:23: error: request for member 'begin' in 'rndName', which is of pointer type 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} (maybe you meant to use '->' ?)
148 | transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower
| ^~~~~
nameSorter1.cpp:148:40: error: request for member 'end' in 'rndName', which is of pointer type 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} (maybe you meant to use '->' ?)
148 | transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower
| ^~~
nameSorter1.cpp:148:55: error: request for member 'begin' in 'rndName', which is of pointer type 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} (maybe you meant to use '->' ?)
148 | transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower

I just keep getting this. why is what a pointer, I'm still learning terminology
Last edited on
bool checkDuplicate(string tempName, string* rndName, int n)
^^ why is that string (the one with the *) a pointer?

* means pointer when used on variable types.
eg
int *x is an integer pointer.

you normally would use references:
bool checkDuplicate(string &tempName, string &rndName, int n)
and normally both strings would be the same type (just for consistency) -- here both are a reference.
you can use references normally: if(tmpname == rndname) for example.
pointers require extra syntax to use.

your code is actually kind of advanced for a beginner: did you write this?
Last edited on
and i got it that makes sense thank you

most of it is was written one at a time in different assignments ive had this semester, I'm just going off what I've already done in class and what I'm asking about is basically the new part that I don't know yet
i took out the pointers but i still get this "same?" error

nameSorter1.cpp:64:29: error: could not convert '(std::string*)(& tempName)' from 'std::string*' {aka 'std::__cxx11::basic_string<char>*'} to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
64 | else if (checkDuplicate(tempName, rndName, nrndNames))
| ^~~~~~~~
| |
| std::string* {aka std::__cxx11::basic_string<char>*}


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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
bool checkDuplicate(string tempName, string rndName, int n); //check if name is a duplicate name

//main program
int main()
{
  //Data
  string objective = "sort names."; //program objective
  string instructions = "Input file name for sorting "; //user instructions
  string usersFile; //stores users file name
  const int MAX_RNDNAMES = 10; //list size
  int nrndNames = 0; //list capcacity
  string rndName[MAX_RNDNAMES]; //name array
  string tempName[MAX_RNDNAMES]; //name array
  int u;
  string tempVar;

  //user introduction
  introduction(objective, instructions);

  // get user file name
  getline(cin, usersFile);

  // open input file
  ifstream fin; //
  fin.open(usersFile.c_str()); //
  if (!fin.good()) throw "I/O error"; //
  
  // getnames
  for (int i = 0; i < MAX_RNDNAMES; i=i+1)
  {
    getline(fin, tempName[nrndNames]);
    if (!fin.good())
    {
    break;
    }
    else if (checkDuplicate(tempName, rndName, nrndNames))
    {
    bool result = false;
    cout << "you already entered " << tempName << " -- skipping -- ";
    i=i-1;
    }
    else if ((tempName[nrndNames].length() > 0 ))
    {
    rndName[nrndNames] = tempName[nrndNames];
    cout << rndName[nrndNames] <<endl << endl;
    nrndNames++;
    }
    else if (tempName[nrndNames].length()==0 )
    {
    i=i-1;
    }  
  }
  
  //close input file
  fin.close();
  
  // open output file
  ofstream fout; //reserve "fout" for use
  fout.open ("friends.txt"); //open specifed file
  if (!fout.good()) throw "I/O error"; //if file isnt open output error to console

  //sort
  for (int j = 0; j < nrndNames; j++) //outer loopp tranverse
  {
    for (int o = j + 1; o < nrndNames; o++)
    { 
      if (rndName[j] > rndName[o])
      {
        tempVar = rndName[j]; 
        rndName[j] = rndName[o];
        rndName[o] = tempVar;
      }
    }
  }

  //output
  for (u = 0; u < nrndNames; u++)
  {
    cout << rndName[u] <<endl;
    cout << endl;
    fout << rndName[u] <<endl;
    fout << endl;
  }

  //close output file
  fout.close();

}//main


//check if user name is a duplicate names
bool checkDuplicate(string tempName tempName, string rndName, int n)
{
  //data
  //guess is the list of non-duplicate names
  //n is the size counter of the list
  //uG is the name being checked
  bool result = false; //false is not a duplicate, true is a duplicate guess
  int i; //loop index for traversing the list

  //check if duplicate name
  for(i=0; i < n; i = i + 1)
  {
    transform(rndName.begin(), rndName.end(), rndName.begin(), toLower()); //change from uppercase to lower
    if (tempName == rndName[i])
    {
      result = true;
    }//if
  }//for i
  return result;
}//checkDuplicate
Last edited on
I should have looked deeper into the code, instead of trying to explain the error message.
Lets start over, from the beginning:

bool checkDuplicate(string tempName tempName, string *rndName, int n)
//^^ because rndname is an array, it needs to be passed as a pointer, that is OK.
//you can also pass it with []

so you had it right, but could not explain it. Put the * back on the header and function, they were fine.

!! else if (checkDuplicate(tempName, rndName, nrndNames))
This is not fine it should be
else if (checkDuplicate(tempName[nrndNames ], rndName, nrndNames))
//^^^^ pass it one name to check, the one you just got, and the list of what you already have to check against, which is a pointer/array (array names are pointers, so rndName IS converted into a pointer for you).

you should maybe use a vector, instead of an array, so you don't have to manually track the size of nrandnames etc.

why is tempname an array at all?
string tempName tempName //this is wrong, one name per type.

see if you can get it now?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
nameSorter1.cpp:29:38: error: expected identifier before '*' token
   29 | bool checkDuplicate(string tempName, *rndName, int n); //check if name is a duplicate name
      |                                      ^
nameSorter1.cpp: In function 'int main()':
nameSorter1.cpp:64:50: error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'int*'
   64 |     else if (checkDuplicate(tempName[nrndNames], *rndName, nrndNames))
      |                                                  ^~~~~~~~
      |                                                  |
      |                                                  std::string {aka std::__cxx11::basic_string<char>}
nameSorter1.cpp:29:39: note:   initializing argument 2 of 'bool checkDuplicate(std::string, int*, int)'
   29 | bool checkDuplicate(string tempName, *rndName, int n); //check if name is a duplicate name
      |                                      ~^~~~~~~
the error just moved down the line, I think you're on to something with tempname not needing to be an array
Last edited on
else if (checkDuplicate(tempName[nrndNames], *rndName, nrndNames))
should be
else if (checkDuplicate(tempName[nrndNames], rndName, nrndNames))

you are going to struggle with this until you stop trying to code and take 1/2 an hour or so to review pointers & how they relate to arrays.

Like this:
void foo( int * ip, int size) //array, passed in as if a pointer
{
for(i = 0; i < size; i++) cout << ip[i];
}

int x[10] = {0};
foo(x,10); //just x. no *, nothing weird.

x is the array's name.
it is also convertible to the address of the first location in x, eg x is the same as &(x[0]) ... this is just a handy shorthand in the syntax so you don't have to type &(x[0]) all the time, the compiler makes the connection and does that for you.

int * p = x; //this also works. the compiler again converts the x into a workable pointer.
p[3] = 5; //this is identical to x[3] = 5; they refer to the same memory locations!
Last edited on
THANK YOU, im reviewing rn
Last edited on
Topic archived. No new replies allowed.