compiling error
Feb 24, 2016 at 12:25am UTC
Hi,when I attempt to run my code in unix it returns an error saying that 'data' is not a member of the vector in my toUpper function, but it runs fine when I run it else where, I would appreciate any help. Thanks.
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
void loadVector(vector<char > &v);
//Precondition: char vector passed
//Postcondition: loads a word or phrase into the vector
void displayVector(vector<char > &v);
//Precondition: char vector is passed, vector must be loaded
//with word/phrase
//Postcondition: displays the vector
void toUpper(vector<char > &v);
//Precondition: char vector is passed, vector must be loaded
//Postcondition: converts all lower case letters in the
//phrase/word into upper case
void removeNonAlphabets(vector<char > &v);
//Precondition: char vector passed, vector must be loaded
//Postcondition: removes all non-alphabets from the vector
bool isPalindrome(const vector<char > &v);
//Precondition: char vector passed, vector must be loaded
//Postcondition: determines whether or not the word/phrase is
//a palindrome
void loadVector(vector<char > &v)
{
string input;
cout << "Enter a word, phrase or sentence to see if it's a palindrome: " << endl;
getline(cin, input);
for (unsigned int i=0; i<input.size(); i++)
{
v.push_back(input[i]);
cout << endl;
}
}
void displayVector(vector<char > &v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i];
}
}
void toUpper(vector<char > &v)
{
for (unsigned int i = 0; i < v.size(); i++)
{
char temp[v.size()];
memcpy(temp,v.data(),v.size());
int j = 0;
while (temp[j])
{
temp[j] = toupper(temp[j]);
j++;
}
v[i] = temp[i];
}
}
void removeNonAlphabets(vector<char > &v)
{
unsigned int m =0;
for (unsigned int j = 0; j < v.size(); j++)
{
char temp = v[j];
if (isalpha(v[j])!=0)
{
v[m] = temp;
m++;
}
}
v.resize(m);
}
bool isPalindrome(const vector<char > &v)
{
size_t i = 0;
size_t j = v.size() - 1;
while (i < j)
{
if (v[i++] != v[j--])
{
return false ;
}
}
return true ;
}
int main()
{
vector<char > v;
loadVector(v);
cout << endl << "The original input string contains: " << endl;
displayVector(v);
cout << endl << endl;
cout << "After conversion to upper-case, input string contains: " << endl;
toUpper(v);
displayVector(v);
cout << endl << endl;
cout << "After removal of non-alphabets, input string contains: " << endl;
removeNonAlphabets(v);
displayVector(v);
cout << endl;
cout << endl;
if (isPalindrome(v) == true )
{
displayVector(v);
cout << " IS a palindrome!" << endl;
}
else
{
displayVector(v);
cout << " is NOT a palindrome!" << endl;
}
return 0;
}
Feb 24, 2016 at 8:54am UTC
Make sure your compiler is up-to date enough and that you have at least enabled C++11 features. With GCC you do this by passing -std=c++11 to the compiler.
Last edited on Feb 24, 2016 at 8:55am UTC
Feb 24, 2016 at 3:54pm UTC
Also be aware that C++ doesn't allow Variable Length Arrays. Array sizes must be compile time constants.
What exactly are you trying to do in you toUpper() function? If you're just trying to convert the whole vector to uppercase why not just use std::transform() instead of all the copying?
1 2 3 4 5 6
#include <algorithm>
...
void toUpper(vector<char > &v)
{
std::transform(v.begin(), v.end(), v.begin(), ::toupper);
}
Topic archived. No new replies allowed.