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;
}
3
  • 1
    Fyi, for starters, you include <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. Commented Jan 19, 2022 at 5:54
  • just subtract '0' from j Commented Jan 19, 2022 at 6:31
  • @WhozCraig According to the comment on stackoverflow.com/a/16506109, std::string must be fully defined even if you have only included <iostream>. However, I agree you should still include <string> so you don’t accidentally calls non-member functions that are only defined in <string> Commented Jan 19, 2022 at 7:38

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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;
}

2 Comments

Method 2 is much better but the question was about getting only first digit not the entire number.
@AshutoshRaghuwanshi Both methods get the same result. I have tested them before posting.
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);
}

Comments

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;
}

2 Comments

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.
@Someprogrammerdude, didn't knew this. Thanks will make the edits.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.