0
how to make string (-1) into integer -1?
if i input string 100(-1)0, i want to make this string become 5 integer 1,0,0,-1,0
17 Answers
+ 1
Yoaraci
// here is a code in c++:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
string userinput;
vector <int> num;
getline(cin, userinput);
for (int index = 0; index<userinput.size(); index++) {
if (index != 0 && userinput[index - 1] == '-') {
if (isdigit(userinput[index])) {
int N = '0' - userinput[index];
num.push_back(N);
}
}
else {
if (isdigit(userinput[index])) {
int N = userinput[index] - '0';
num.push_back(N);
}
}
}
for (int i = 0; i<num.size(); i++) {
cout << num[i] << endl;
}
system("pause");
return 0;
}
+ 4
A way which works with both C and C++ : atoi
// C++
// include string and cstdlib
int a = atoi(s.c_str()); //with s of type std::string
// C
// include stdlib.h
int a = atoi(s); // with s an array of chars terminating by a null character
A C++ like solution which works with all types that overload the >> operator (int does) :
// include sstream
std::istringstream iss(s); // with s your string
int a;
iss >> a;
+ 3
Baptiste E. Prunier
Will the stringstream solution work for the sample string as mentioned in original post? "100(-1)0", somehow I doubt it will, how does the stringstream object decide what to do with the parentheses?
+ 2
+ 2
vector is same as array in c++ only difference is we do not need to specify it's size;
.push_back() is used to add an element at the end of a vector ( i.e array)
+ 2
Yoaraci, what I can see you haven't completed even half of course in c++;
It may be difficult to understand;
In case you have any doubt please don't hesitate to ask;
😊😊😊
+ 2
Ipang you will have to change it a bit, it will give you 100, -1 and 0 (3 integers), you will have to split 100 yourself
+ 1
If are you looking for javascript code:
function start() {
var userinput = document.getElementById("userInput").value;
let sign = "";
let num=[];
for (var index=0; index<userinput.length; index++) {
if (index !== 0 && userinput[index-1] === '+') {
sign="+";
} else if (index !== 0 && userinput[index-1] === '-') {
sign = '-';
}
if (!isNaN(userinput[index])) {
num.push(parseInt(sign+userinput[index]));
sign="";
}
}
document.getElementById("output").innerHTML = num;
}
same logic can be used in different programing language!!
+ 1
Yoaraci, did you understood the logic or I should give an example in c++ ?
+ 1
Yoaraci , did you get this!!!! or say I will explain!!
+ 1
Yoaraci did you really understand??
0
actually i never learn java so i dont really understand the logic😅... so, yes i hope you can give me the example in c++ 😁
0
can i ask what the function from vector<int>num and num.push_back?
0
okay thanks 😁
0
thanks hehehe... i actually learn only c in my school and i also new in coding hahaha.. but i understand a little about c++... i will ask you again if there are something that i dont understand thank youu😁😁😁
- 1
oh sorry i forgot to add it, i need c or c++ but i more prefer with c... but thanks for answering :)
- 1
thanksss 😀😀😀