I
I
igoodmood2016-04-23 17:58:42
ASCII
igoodmood, 2016-04-23 17:58:42

How to remove encoding from C++ program?

I want to copy certain elements from one array to another, but there was a problem: ASCII encoding appeared instead of the expected values.
Here is the screenshot: a56b65f64989411d8c584984cfd2e3ed.PNG
Here is the code:

#include "stdafx.h"
#include <iostream>
#define MAX 50
using namespace std;
int main()
{
  char str[50] = "12345678901";
  int i,l;
  double s1[MAX];
  i = 0;
  while(str[i] != '\0')
  {
    i++;
  }
  l = i / 2;
  for(i = 0; i < l; i++)
  {
    s1[i] = str[i];
    cout << s1[i] << endl;
  }
  system("pause");
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2016-04-23
@GavriKos

Well, everything is correct. You have three options:
1) Declaring the s1 array as a char array (or removing it altogether)
2) When copying from str to s1, convert it to a number (atof and others) (if you really need the s1 array)
3) When outputting to the console from s1 lead to char (the worst option)
It's hard to say exactly, because the purpose of the array s1, for example, is generally unclear - it can be thrown out altogether.

I
Ivan Bogachev, 2016-04-23
@sfi0zy

This is where
the implicit conversion char -> int -> double takes place. And with char -> int, of course, you get the same ascii character codes.
Perhaps you meant something like this
s1[i] = str[i] - '0';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question