This is my code:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string userInput;
std::getline(std::cin, userInput);
int sum;
int result;
std::string numDigit;
bool space = true;
for (int i = 0; i < userInput.length(); i++) {
if (isdigit(userInput[i]) && space) {
numDigit += userInput[i];
space = false;
}else if(isdigit(userInput[i]) && !space) {
numDigit += userInput[i];
}else if (!isdigit(userInput[i]) && !space) {
std::stringstream(numDigit) >> result;
sum += result;
numDigit = "";
result = 0;
space = true;
}
std::cout << sum;
}
}
If i input 1 2 3 with space, it should ouput sum = 6, but instead it output many digits of number why is it like that ? (sorry I'm beginner of c++)
0and the internal representation of"0", which is (as ASCII, as usual on a lot of current systems)48?"0"is a string literal, consisting of the two characters'0'and'\0'.'0'and0, while true, isn't relevant here.