C++ more then one max values

Pages: 12
getline can take in whitespace, so use getline. Hope that helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>
#include <string>

int main()
{
    using namespace std;   
    
    string line;
    getline(cin, line);
    
    if (line == " ")
    {
       cout << "You entered a space\n";
    }
}


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
// Example program
#include <iostream>
#include <string>
#include <cctype>

/// is character alphabetic OR numeric
bool isAlphanumeric(char c)
{
    return std::isalpha(c) || std::isdigit(c);
}


int main()
{
    using namespace std;   
    
    string line;
    getline(cin, line);
    
    if (line.length() != 1)
    {
        cout << "Incorrect format\n";
    }
    else if (std::isspace(line[0]))
    {
       cout << "You entered (white)space\n";
    }
    else if (isAlphanumeric(line[0]))
    {
        cout << "You entered an alphanumeric character\n";   
    }
}
Last edited on
@Ganado
Thank you, it's very helpful!

Hmm, there is an error.
In my code, when I replace char alpha; cin >> alpha, with string alpha; getline (cin, alpha), an error occurs (?)

f = count(vs.at(i).begin(), vs.at(i).end(), alpha);

Now I need a c_str() to convert string to char and strcpy, right?
Last edited on
an error occurs
What error occurs?
Show the actual code, or not just snippets. I can't see what's wrong just from that. I'm still not exactly sure what you want to have happen.

getline works with strings, so if you want to do a character, then just do line[0] like I did above. This gets a char. Or did you mean char*? Two different things.
Well, something is wrong, don't know what. What I want? I want to enter alphanumeric as well as space.

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
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
int r;
cout << "Number of rows: ";
cin >> r;

vector <string> vs;

string sText;

cin.ignore(1000, '\n');
for(int i = 0; i < r; i++)
{
    cout << "Input text: "
    getline(cin, sText);
    vs.push_back(sText);
}
/* ///////////////////////////////////////////////////////////////old version
char alpha;
cout << "Input any alphanumeric: "; cin >> alpha;
*/
//////////////////////////////////////////////////////////////////// new version doesn't work
string alpha; 
getline (cin, alpha);
/////////////////////////////////////////////////////////////////////////////////////////////////
int f = 0;
int q = 0;
vector <int> fr;

for(int i = 0; i < r; i++)
{
    f = count(vs.at(i).begin(), vs.at(i).end(), alpha);
    if (f > 0) {q = f;}
    fr.push_back(f);
}

//*************************************

if (q > 0)  //if any alpha
{
    cout << "Max occ. in row(s):\n";

    int how_many = *max_element(fr.begin(), fr.end()); 	

    for (size_t i = 0; i < fr.size(); i++)
    {
        if (fr.at(i) == how_many)
            cout << i << ' ';	//shows the row
    }
}
else
    cout << "Nop. No such character.";

return 0;
}
Last edited on
Your confusion is understandable, C++ template compiler errors are infamously atrocious.

If you search the compiler error output for "required from", it gives you a hint as to where the issue is.
error: no match for 'operator==' (operand types are 'char' and 'const std::basic_string<char>')

Somewhere you're trying to compare a char to a string (basic_string<char>).

f = count(vs.at(i).begin(), vs.at(i).end(), alpha);

vs is a vector<string>.
vs.at(i) is a string.
You're iterating from the beginning of one string to the end of it, which is iterating over characters, but alpha is a string and not a character.

If you require alpha to be 1 character long, do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    string alpha; 
    getline (cin, alpha);
    if (alpha.length() != 1)
    {
        std::cout << "Bad input. Enter again, etc.\n";   
    }
    //
    int f = 0;
    int q = 0;
    vector <int> fr;
    
    for(int i = 0; i < r; i++)
    {
        f = count(vs.at(i).begin(), vs.at(i).end(), alpha[0]);
        if (f > 0) {q = f;}
        fr.push_back(f);
    }

etc. It should compile now, although I don't know if the logic is correct yet.

Also, it helps to use meaningful variable names. I have no idea what f, q, fr means.
Last edited on
f = count(vs.at(i).begin(), vs.at(i).end(), alpha[0]);

alpha[0] is the solution

Thank you!
Last edited on
Topic archived. No new replies allowed.
Pages: 12