LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 8

to_string and stoi round trip Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

int main() {
    std::string s = std::to_string(12) + std::to_string(34);
    int n = std::stoi(s) + 1;
    std::cout << s << ' ' << n << ' ' << std::to_string(1.5) << ' ' << std::stoi("  -7x") << '\n';
    return 0;
}
  • A 1234 1235 1.500000 -7 Correct
  • B 46 47 1.5 -7
  • C 1234 1235 1.5 -7
  • D 1234 47 1.500000 7
Correct Answer: Option A

Joining "12" and "34" gives the text "1234", which stoi turns back into a number. to_string(double) prints six decimals. stoi skips leading spaces, reads the sign and digits, and stops at 'x'.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links