no conversion from 'const char *' to 'int' - char

Can someone help me with this issue I'm having? I'm getting the 'no conversion from 'const char *' to 'int' on my first 'if' statement line. Can someone tell me what i'm doing wrong?
char Vtype,
Vehicle;
int HI,
HO,
MI,
MO,
Ctime,
Catime,
Btime,
Basub,
Ttime,
Tasub;
double Ctotal,
Batot,
Bbtot,
Btotal,
Tatot,
Tbtot,
Ttotal;
const double C_Rate = 1.25,
B1_Rate = 2.00,
B2_Rate = 2.50,
T1_Rate = 3.75,
T2_Rate = 4.50;
cout << "TYPE OF VEHICLE: ";
cin >> Vtype;
if (Vtype == "C" || Vtyp == "B" || Vtype == "T")
{
cout << "HOURS IN: ";
cin >> HI;
if (HI < 0 || HI > 23)
{
cout << "Hours cannot be less than 0 or greater than 23!\n";
cout << "Please enter a valid hour.\n";
cin >> HI;
}
Thank you,
T

If Vtype is a char, compare it to characters:
if (Vtype == 'C' || Vtype == 'B' || Vtype == 'T') {
}
The compiler is accusing you of comparing const char * to int because as far as C++ is concerned, a character is an integer.
Or, if you need to compare it to strings, declare it as std::string.

Related

casting double to signed vs unsigned integer

It seem that the MSVC Compiler treats signed and unsigned overflow differnetly. When casting a double value that exceeds the maximum integer value, the result is the smallest possible integer value (always the same). When casting to unsigned int, the cast produces an overflow as expected (maximum unsigned int value + 1 produces 0, maximum unsigned int + 2 produces 1, ...)
Can someone explain the behaviour of the compiler, or is it a bug?
Tested compilers MSVC 10 and 14
#define BOOST_TEST_MODULE Tests
#include <boost/test/unit_test.hpp>
#include <climits>
#include <iostream>
BOOST_AUTO_TEST_CASE(test_overflow_signed) {
double d_int_max_1 = INT_MAX + 1.; //2147483647 + 1
double d_int_max_2 = INT_MAX + 2.; //2147483647 + 2
BOOST_CHECK((int)(2147483648.) != (int)(2147483649.)); //succeeds (overflows to -2147483648 and -2147483647)
BOOST_CHECK((int)(d_int_max_1) != (int)(d_int_max_2)); //fails (both values overflow to -2147483648)
std::cout << "(int)(2147483648.) == " << (int)(2147483648.) << std::endl; //-2147483648
std::cout << "(int)(2147483649.) == " << (int)(2147483649.) << std::endl; //-2147483647
std::cout << "(int)(d_int_max_1) == " << (int)(d_int_max_1) << std::endl; //-2147483648
std::cout << "(int)(d_int_max_2) == " << (int)(d_int_max_2) << std::endl; //-2147483648
}
BOOST_AUTO_TEST_CASE(test_overflow_unsigned) {
double d_int_max_1 = UINT_MAX + 1.;//4294967295 + 1
double d_int_max_2 = UINT_MAX + 2.;//4294967295 + 2
//BOOST_CHECK((unsigned int)(4294967296.) != (unsigned int)(4294967297.)); //compiler fails (!= truncation of constant value)
BOOST_CHECK((unsigned int)(d_int_max_1) != (unsigned int)(d_int_max_2)); //succeeds (overflows to 0 and 1)
std::cout << "(unsigned int)(d_int_max_1) == " << (unsigned int)(d_int_max_1) << std::endl; //0
std::cout << "(unsigned int)(d_int_max_2) == " << (unsigned int)(d_int_max_2) << std::endl; //1
}
[conv.fpint]/1 A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be
represented in the destination type.
Emphasis mine. Since the behavior is undefined, any outcome whatsoever is correct.

Why does a vector value stored in an integer give different results?

I have very recently started using vectors in C++.The purpose of the program I am trying to write is to determine the first missing positive number. Given an input array A : [ -9, 12, -1, 0, 1 ] the expected output is 2. I coded it -
int Solution::firstMissingPositive(vector<int> &A)
{
std::sort(A.begin(),A.end()); //sorting vector
bool Negative=false;
int flag;
int i=0;
for(i=0;i<A.size();++i)
{
if(A[i]<=0)
{
Negative=true;
std::cout<<"\nwe found a -ve number or 0";
}
else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
{
Negative=false;
std::cout<<"\ncomparing # ="<<i<<" which = "<<A[i];
std::cout<<"\ncomparing # ="<<i+1<<" which = "<<A[i+1];
flag=A[i]+1; //The faulty statement
std::cout<<"\n\n missing number(A[i]+1) # ="<<A[i]+1;
std::cout<<"\n\n missing number(flag) # ="<<flag;
break;
}
}
//do something more
}
With this output -
-9 -1 0 1 12
we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing # =3 which = 1
comparing # =4 which = 12
missing number(A[i]+1) # =2
missing number(flag) # =20
I found this interesting because, to me, it looks like I cannot use an integer to store the value of a vector.
Trying to debug it I found changing the flag assignment to flag = A[i]+2 makes the resultant print 30.
I've read other questions on SO where it suggests using vector.at(i) instead of the [] operator as a better practice. Changing this does not reflect any change for my code.
Changing flag to vector<int> gives me a dirty error which I'm not sure about.
Isn't A[i]+1 syntactically equivalent to an integer value? And if it is, why can I not store it?
Reducing your code to an MCVE quickly demonstrates that the problem is with your output formatting, putting the "\n" at the beginning of the statement is an uncommon practice for precisely this reason. You're not seeing 20, what you're seeing is the "2" from flag followed by a zero printed elsewhere.
#include <algorithm>
#include <iostream>
#include <vector>
void solve(std::vector<int>& A)
{
std::sort(A.begin(),A.end()); //sorting vector
bool Negative=false;
int flag;
int i=0;
for(i=0;i<A.size();++i)
{
if(A[i]<=0)
{
Negative=true;
std::cout<<"we found a -ve number or 0\n";
}
else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
{
Negative=false;
std::cout<<"comparing # ="<<i<<" which = "<<A[i]<<'\n';
std::cout<<"comparing # ="<<i+1<<" which = "<<A[i+1]<<'\n';
flag=A[i]+1; //The faulty statement
std::cout<<"missing number(A[i]+1) # ="<<A[i]+1<<'\n';
std::cout<<"missing number(flag) # ="<<flag<<'\n';
break;
}
}
//do something more
}
int main() {
std::vector<int> A { -9, 12, -1, 0, 1 };
solve(A);
std::cout << "and we're done\n";
}
Output (see http://ideone.com/zb9fNX)
we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing # =3 which = 1
comparing # =4 which = 12
missing number(A[i]+1) # =2
missing number(flag) # =2
and we're done
I should also point out that your test for "A[i+1]" will lead to an out of bounds array access if it ever tries to read the last element in the array. You should change
for(i=0;i<A.size();++i)
to
for(i=0;i<A.size() - 1;++i)
or more conventially,
for(i=1;i<A.size();++i)
and use "A[i-1]" instead of "A[i+1]" to fetch the previous value.
For example:
#include <algorithm>
#include <iostream>
#include <vector>
void solve(std::vector<int>& A)
{
std::sort(A.begin(),A.end()); //sorting vector
int expected = 0;
int missing = 0;
for(int i = 1; i < A.size(); ++i)
{
if (A[i] <= 0) {
std::cout << "we found " << A[i] << "\n";
continue;
}
if (A[i-1] <= 0) {
expected = 1;
} else {
expected = A[i-1] + 1;
}
if (A[i] == expected)
continue;
std::cout << "A[" << i-1 << "] = " << A[i-1] << '\n';
std::cout << "expecting " << expected << '\n';
std::cout << "A[" << i << "] = " << A[i] << '\n';
missing = expected;
std::cout << "missing number: " << expected << '\n';
break;
}
//do something more
}
int main() {
std::vector<int> A { -9, 12, -1, 0, 1 };
solve(A);
std::cout << "and we're done\n";
}
http://ideone.com/AIw4oU

Using constexpr wrong from the c++11 standard or have I caught some compiler bug?

I'm playing with the constexpr keyword and coded the following simple program:
#include <iostream>
using namespace std;
template<typename T>
constexpr bool is_equal(T const* array1, T const* array2, size_t len)
{
return array1 == array2 || (len ? array1[len - 1] == array2[len - 1] && is_equal<T>(array1, array2, len - 1) : true);
}
template<typename T, size_t N1, size_t N2>
constexpr bool is_equal(T const (&array1)[N1], T const (&array2)[N2])
{
return N1 == N2 && is_equal<T>(array1, array2, N1);
}
template<typename T, size_t N>
constexpr size_t arraylength(T const (&array)[N])
{
return N;
}
constexpr size_t stringlength(char const* str, size_t len=0)
{
return str ? (*str ? stringlength(str + 1, len + 1) : len) : 0;
}
constexpr size_t min(size_t one, size_t another)
{
return one < another ? one : another;
}
constexpr size_t min_length(char const* str1, char const* str2)
{
return min(stringlength(str1), stringlength(str2));
}
template<typename T, size_t N1, size_t N2>
constexpr size_t min_length(T const (&array1)[N1], T const (&array2)[N2])
{
return min(N1, N2);
}
template<bool cond=false>
struct to_num
{
enum {value=0};
};
template<>
struct to_num<true>
{
enum {value=42};
};
template<size_t init>
struct two_times
{
enum {value=init*2};
};
static constexpr char x[]{"One string"};
static constexpr char y[]{"One string"};
static constexpr int a[]{1,2,3,4};
static constexpr int b[]{1,2,3,4};
static constexpr int const* const c = &a[0];
static constexpr int const* const d = &b[0];
int main()
{
cout << "The two literals are equal: " << to_num< is_equal("One string", "One string") >::value << endl; // COMPILES AND WORKS IN GCC BUT NOT IN CLANG
cout << "The two variables x & y are equal: " << to_num< is_equal(x, y) >::value << endl;
cout << "Pointers a & c are equal: " << to_num< a == c >::value << endl;
cout << "Pointers b & c are equal: " << to_num< b == c >::value << endl;
cout << "Pointers c & d are equal: " << to_num< c == d >::value << endl;
cout << "The contents of c & d is the same: " << to_num< is_equal(c, d, arraylength(a)) >::value << endl;
cout << "Pointers a & b are equal: " << to_num< a == b >::value << endl;
cout << "The contents of a & b is the same: " << to_num< is_equal(a, b) >::value << endl;
cout << "String x contains " << two_times< stringlength(x) >::value / 2 << " characters" << endl; // COMPILES AND WORKS IN CLANG BUT NOT IN GCC
cout << "String literal contains " << two_times< stringlength("literal") >::value / 2 << " characters" << endl; // COMPILES AND WORKS IN CLANG BUT NOT IN GCC
cout << "Array literal contains " << two_times< arraylength("literal") >::value / 2 << " values" << endl;
return 0;
}
As the comments remark, some code compiles and works ok with g++ but not with clang++ and other compiles and work ok with clang++ but not with g++
The gcc error is:
comaprison.cpp: In function ‘int main()’:
comaprison.cpp:97:62: in constexpr expansion of ‘stringlength(((const char*)(& x)), 0ul)’
comaprison.cpp:29:55: in constexpr expansion of ‘stringlength((str + 1u), (len + 1ul))’
comaprison.cpp:97:64: error: ‘((((const char*)(& x)) + 1u) != 0u)’ is not a constant expression
cout << "String x contains " << two_times< stringlength(x) >::value / 2 << " characters" << endl;
^
comaprison.cpp:97:64: note: in template argument for type ‘long unsigned int’
comaprison.cpp:98:76: in constexpr expansion of ‘stringlength(((const char*)"literal"), 0ul)’
comaprison.cpp:29:55: in constexpr expansion of ‘stringlength((str + 1u), (len + 1ul))’
comaprison.cpp:98:78: error: ‘((((const char*)"literal") + 1u) != 0u)’ is not a constant expression
cout << "String literal contains " << two_times< stringlength("literal") >::value / 2 << " characters" << endl; // COMPILES AND WORKS IN CLANG BUT NOT IN GCC
^
comaprison.cpp:98:78: note: in template argument for type ‘long unsigned int’
and the clang++ one is:
comaprison.cpp:89:55: error: non-type template argument is not a constant expression
cout << "The two literals are equal: " << to_num< is_equal("One string", "One string") >::value << ...
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
comaprison.cpp:8:19: note: subexpression not valid in a constant expression
return array1 == array2 || (len ? array1[len - 1] == array2[len - 1] && is_equal<T>(array1, array2...
^
comaprison.cpp:14:24: note: in call to 'is_equal(&"One string"[0], &"One string"[0], 11)'
return N1 == N2 && is_equal<T>(array1, array2, N1);
^
comaprison.cpp:89:55: note: in call to 'is_equal("One string", "One string")'
cout << "The two literals are equal: " << to_num< is_equal("One string", "One string") >::value << ...
^
1 error generated.
g++ is "gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)",
clang++ is "Ubuntu clang version 3.4-1ubuntu1 (trunk) (based on LLVM 3.4)"
Both in a x86_64 linux.
The command lines are:
clang++ -std=c++11 -o comaprison comaprison.cpp
g++ -std=c++11 -o comaprison comaprison.cpp
So, am I doing anything outside the c++11 standard with this code or there is something wrong in both compilers?.
Note that If I remove the g++ problematic lines but leave the clang++ problematic one, the code compiles and works with g++ and if I remove the clang++ problematic line and leave the g++ problematic ones, the code compiles and works ok with clang++
Note also that I'm using the two templated structs to force the compiler to resolve the functions at compile time.
Thank you for your time and experience.
Update:
GCC chokes on your function stringlength. Try this:
constexpr size_t stringlength(char const* str, size_t i=0)
{
return str ? (str[i] ? 1 + stringlength(str, i+1) : 0) : 0;
}
To GCC 4.6.3 this seems dubious:
static constexpr int const* c = &a[0];
static constexpr int const* d = &b[0];
Try this:
static constexpr int* c = &a[0];
static constexpr int* d = &b[0];
Old answer:
You code seems to be correct and with GCC 4.6.3 it also compiles.
int main()
{
const char* a = "hallo";
const char* b = "qallo";
std::cout << is_equal(a,b,5) << std::endl;
constexpr const char* c = "hallo";
std::cout << is_equal(a,c,5) << std::endl;
}
Be careful, that the strings you are giving to your functions are constants.
See here what is allowed with constexpr.
Your arraylength example that you say doesn't work with GCC, works just fine with GCC 4.7 and 4.8, and except for an unrelated error about constexpr const, also with GCC 4.6.
As for your stringlength example: this is a limitation of GCC. I know I've seen a bug report about it already. You can work around it by rewriting your stringlength function:
constexpr size_t stringlength(char const* str, size_t len=0)
{
return str ? (str[len] ? stringlength(str, len + 1) : len) : 0;
}
but as far as I can tell, what you had already was perfectly valid.

Sentinel not working?

I'm not sure why this isn't working, but this is my error message:
Error] no match for 'operator!=' (operand types are 'std::string {aka std::basic_string}' and 'const int')
EDIT: The above issue has been resolved. But, the current issues are redundant ***, and the lack of vowel removal in a sentence rather than just the first word of a sentence.
#include <iostream>
#include <string>
using namespace std;
void removeVowel(string&); // Removes vowels from input string.
string withVowel; // Will be used to read user input.
int main ()
{
const string SENTINEL = "0"; // Sentinel value.
// Request input string unless SENTINEL is entered.
cout << "Enter a word or series of words. " << '\n';
cout << "Or, enter " << SENTINEL << " to quit. " << endl;
cin >> withVowel;
// In case of SENTINEL:
while (withVowel == SENTINEL)
{
cout << "***" << endl;
}
// Run loop.
removeVowel(withVowel);
// Display the string without vowels.
cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;
return 0;
}
void removeVowel(string& withVowel)
{
int i = 0;
int length = int(withVowel.length());
while (i < length)
{
if (withVowel.at(i) == 'a' ||
withVowel.at(i) == 'A' ||
withVowel.at(i) == 'e' ||
withVowel.at(i) == 'E' ||
withVowel.at(i) == 'i' ||
withVowel.at(i) == 'I' ||
withVowel.at(i) == 'o' ||
withVowel.at(i) == 'O' ||
withVowel.at(i) == 'u' ||
withVowel.at(i) == 'U')
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
else i++;
}
// Display the string without vowels.
cout << removeVowel << endl;
}
Based on your other question, and the error message, I assume withVowel is a std::string. The error message is pretty much telling you what the problem is: you can't compare a std::string with an int.
Since you only need SENTINEL for printing and comparison, just declare it as a std::string as well:
const std::string SENTINEL = "0";
You can't compare const int with strings.
Use ctrl+z and enter to stop the input.
string word;
cout << "ctrl+z and Enter to exit\n";
while (cin >> word){
cout << word << ' ';
// other processing
}
And for your case:
cout << "Enter a word or series of words.\n";
cout << "Ctrl+z and Enter to exit\n";
while (cin >> withVowel){
removeVowel(withVowel);
cout << "The word(s) entered reflecting only consonants :" << withVowel << endl;
}

Remove Vowels from more than first word

Program compiles, runs, and works.
Big issue:
It only works for the first word of a sentence.
EXAMPLE:
"Welcome to the jungle" results in "wlcm" rather than "wlcm t th jngl".
Small Issue:
There's a "1" appearing between input and output when it runs. How can I get rid of that? I think it's from this, but I'm not positive:
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
WHOLE CODE:
#include <iostream>
#include <string>
using namespace std;
void removeVowel(string&); // Removes vowels from input string.
string withVowel; // Will be used to read user input.
int main ()
{
const string SENTINEL = "0"; // Sentinel value.
// Request input string unless SENTINEL is entered.
cout << "Enter a word or series of words." << '\n';
cout << "Or, enter " << SENTINEL << " to quit." << '\n' << endl;
cin >> withVowel;
// In case of SENTINEL:
while (withVowel == SENTINEL)
{
cout << '\n' << "***" << endl;
return 0;
}
// Run loop.
removeVowel(withVowel);
// Display the string without vowels.
cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;
return 0;
}
void removeVowel(string& withVowel)
{
int i = 0;
int length = int(withVowel.length());
while (i < length)
{
if (withVowel.at(i) == 'a' ||
withVowel.at(i) == 'A' ||
withVowel.at(i) == 'e' ||
withVowel.at(i) == 'E' ||
withVowel.at(i) == 'i' ||
withVowel.at(i) == 'I' ||
withVowel.at(i) == 'o' ||
withVowel.at(i) == 'O' ||
withVowel.at(i) == 'u' ||
withVowel.at(i) == 'U')
{
withVowel.erase(i, 1);
length = int(withVowel.length());
}
else i++;
}
// Display the string without vowels.
cout << removeVowel << endl;
}
Use getline(cin, withVowel); instead of cin >> withVowel;
Also replace while with if in main().
And don't forget to upvote and accept answers=)
The problem with only getting the first word is being you're using cin >> withVowel;, which will stop reading input as soon as it encounters white space. Try using std::getine(cin, withVowel); instead.
If at all possible, I would avoid manipulating the string in place, and just copy things to the output if they're not vowels.
std::remove_copy_if(withVowel.begin(), withVowel.end(),
[](char c) { return c == 'a' || c == 'A' ||
c == 'e' || c == 'E' ||
c == 'i' ...;});

Resources