0

How do i edit this program for j to contain "1"?

Currently it shows 49 which is the ascii value i think.

#include <iostream>
using namespace std;

main()
{
  string i = "123";
  int j = i[0];
  cout << j;
}
  • 1
    Fyi, for starters, you include [``](https://en.cppreference.com/w/cpp/string/basic_string) , which is the contracted mandate for bringing `std::string` to your C++ party. If it "works" without it, it is by chance; not design, and engineers don't like coding by chance. – WhozCraig Jan 19 '22 at 05:54
  • just subtract `'0'` from `j` – kesarling He-Him Jan 19 '22 at 06:31
  • @WhozCraig According to the comment on https://stackoverflow.com/a/16506109, `std::string` must be fully defined even if you have only included ``. However, I agree you should still include `` so you don’t accidentally calls non-member functions that are only defined in `` – Ranoiaetep Jan 19 '22 at 07:38

5 Answers5

3

You can do this as shown below:

int main()
{
  std::string i = "123";
  int j = i[0] - '0'; //this is the important statement
  std::cout << j;
}

Explanation

'0' is a character literal.

So when i wrote:

int j = i[0] - '0';

The fundamental reason why/how i[0] - '0' works is through promotion. In particular,

both i[0] and '0' will be promoted to int. And the final result that is used to initialize variable j on the left hand side will be the resultant of subtraction of those two promoted int values on the right hand side.

And the result is guaranteed by the Standard C++ to be the integer 1 since from C++ Standard (2.3 Character sets)

  1. ...In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

So there is no need to use magic number like 48 etc.

Jason
  • 36,170
  • 5
  • 26
  • 60
2

You have to subtract ASCII '0' (48) from the character digit:

#include <iostream>
using namespace std;

int main()
{
  string i = "123";
  int j = i[0] - 48;  // ASCII for '0' is 48
  // or
  // int j = i[0] - '0';
  cout << j;
}
Jason
  • 36,170
  • 5
  • 26
  • 60
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 3
    The character `'0'` is the correct character digit for *any* encoding, not only ASCII. Using the magic number `48` would be forcing ASCII while `'0'` is portable to any C++ implementation. – Some programmer dude Jan 19 '22 at 05:56
  • @Someprogrammerdude, didn't knew this. Thanks will make the edits. – kiner_shah Jan 19 '22 at 05:58
2
  1. Construct a new string from character.
  2. Convert the substring to integer. Example:
#include <iostream>
using namespace std;

main() {
  string i = "123";

  // Method 1, use constructor
  string s1(1, i[0]);
  cout << s1 << endl;

  // Method 2, use convertor
  int j = atoi(s1.c_str());
  cout << j << endl;
}
Zongru Zhan
  • 546
  • 2
  • 8
2

The solution is simple , just cast j to char . Example:

#include <iostream>
using namespace std;

main()
{
  string i = "123";
  int j = i[0];
  cout << char(j);
}
1

Change j to be a char instead of an int:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string i = "123";
  char j = i[0];
  cout << j;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770