LenItsuki
LenItsuki

Reputation: 67

value is not displayed in cout

Value which is located after "v=" is not displayed in code as below. Let me know how to solve it.

while(getline(ifs,line))
    {
        vector<string> strvec=split(line,',');
        for(int l=0;l<strvec.size();l++)
        {
            cout<<"l="<<l<<"value="<<stoi(strvec.at(l))<<endl;
            unsigned char v;
            v=stoi(strvec.at(l));
            File.write((char *)&v, sizeof(char)); 
            cout<<"v="<<v<<endl;
        }           
    }

Upvotes: 0

Views: 457

Answers (1)

PapaDiHatti
PapaDiHatti

Reputation: 1921

"v=" is not displayed in the code because v is unsigned char and cout behavior for unsigned char is to display it's ASCII value and when it is not printable then you will get these type of absurd issues.so either you can declare v as unsigned int or do static_cast as shown in below example :

#include <iostream>
#include <vector>
int main()
{
  std::vector<std::string> strvec{std::string("10"),std::string("20")};
  for(int l=0;l<strvec.size();l++)
  {

        std::cout<<"l="<<l<<"value="<<stoi(strvec.at(l))<<std::endl;
        unsigned char v;
        v=stoi(strvec.at(l));
        std::cout<<"v="<<static_cast<unsigned>(v)<<std::endl;
  }
  return 0;
}

Upvotes: 2

Related Questions