How to change a string type? - c++

I want to enter a string.
string value;
cin >> value;
And i want to take the string and change parts of a string in to integers.
eg. 00: Input 22
I want to change "00"(without ":") in to an integer. Keep "Input" a a string and make the last two character to integers.
Now i want to take the parts that i have changed and display the in a array. One col for each part of the converted string.

1 - To return a substring
2 - To Convert a String to int

Related

How to extract digit from an integer that fits a range of numbers?

For example, a user might input an ID number and creating a function that goes through that ID number and extracts and prints the first number that fits between 3 and 8?
E.g.
They input a ID "157567658" , this function would go through and extract 5 only as its the first digit to be within that range (3-8)
Expanding on a comment, an integer is not necessarily an int. For example
char five = '5';
is an integer stored in a char. Also
std::string x = "157567658";
are several single digit integers represented as a std::string.
The ingredients you need are:
Read user input into a std::string:
std::string id;
std::cin >> id;
Loop over all characters in a std::string :
for (const char& c : id) {
// c are the elements of id
}
Pick the digit you are looking for
if ('3' <= c && c < '8') { // half-open ranges are most common
// do something with c
}
You just have to put things together.

I am having problem with this question my output is not correct

Write a program to display the sizes of basic four datatypes i.e integer, double, float, character.
Input:
The first line of input contains integer T denoting the number of test cases. For each test case, the user can input any of the above data types.
Output:
For each test case, there is a single line output displaying the size of that data type.
Constraints:
1<=T<=100
Example:
Input:
4
1
#
7.98
9.985647851
Output:
4
1
4
8
I tried this
int main() {
//code
int x;
cin>>x;
std::string s;
for(int i = 0;i<x;i++){
cin>>s;
cout << sizeof(s) << endl;
}
return 0;
}
Output was
32
32
Wrong Answer
I started to work on this problem to provide the OP with some insight however I have come to conclusion that many others have within the comment section of the OPs original question: and that is that this question or problem has an indeterminate solution!
Reasoning:
If the user enters any of the following digits as a single entity for input { 0, 1, ... 9 } into the console this can be at the least interpreted as an int or a char type and at worst even a possible double, but without the '.' character being present we could eliminate that as a candidate. This problem definitely has ambiguity to it. Checking to see if it is a float or a double is easy; all one has to do is check the string to see if there is at least a '.' then it's either a double or a float then check the last character of the string to see if it is a f and if so then it is a float. Checking to see if it is a character that is a non digit is easy, however distinguishing a single character digit between a char and an int is the troublesome part!
Work around:
You could conclude that if the input string is a single character and is a non digit then it is definitely a char type.
You could conclude that if the input string is a single character and is a digit ASCII[48 - 57] then you could conclude that it is an int type. This would be considered a restraint.
You could conclude that if it isn't the above two it is at least a float or a double and it is a float if and only if the last character of the string is a f, but a . must be present for it to be either of the two. Again these would be restraints that you would put on the accepted data input.

How to convert an array of ASCII codes to int C++

First of all, i would like to read from plain text, i read hundreds of webpages about it and i just can't make it. I want to read every byte of the file and every two byte is a number what i want to store.
I want to read: 10 20.
I get: ASCII code of 1, ASCII code of 0, ASCII code of space etc. etc.
I tried several things, like stream.get, or stream.read, tried to convert with atoi but then i can't concatenate the two digits, i tried sprintf but all of them failed.
Array of ASCII codes:
char ASCII[] = "10 20";
Convert to integer variables:
std::istringstream iss(ASCII);
int x,y;
iss >> x >> y;
Done.
Here's the working sample: http://ideone.com/y8ZRGs
If you want to do this with your own code, there are only two things you need to be able to do.
First, you need to convert from the ASCII code of a digit to the number it represents. This is as simple as subtracting '0'.
Second, you need to convert from the numerical value of each digit of a two digit number to the number that represents. This is simple -- if T is the tens place and U is the units, it's 10T + U.
So, for example:
int twoDigitNumber (char tens, char units)
{
return 10 * (tens - '0') + (units - '0');
}

How can I parse double from a string?

I have the following code but it only converts strings to double. if the string contains a letter, it causes an error
while (cin >> s){
const char *c_ptr=s.c_str();
d = atof(c_ptr);
v.push_back(d);
}
I want to input a string like "a1.2 3 4b" and have the vector populated with "1.2 3 4"
What about this:
1) replace all chars in the string that aren't digits or decimal points by spaces
2) read doubles in a loop
since you got rid of the letters before parsing the doubles, they should no longer cause issues.

converting string to int

hello i have a problem i am trying to convert a string like "12314234" to int
so i will get the first number in the string.
from the example of string that is shown above i want to get '1' in int.
i tried :
string line = "12314234";
int command = line.at(0);
but it puts inside command the ascii value of 1 and not the number 1.
thanks in advance.
int command = line.at(0) - '0';
The numerical values of digit characters are required to be next to each other. So this works everywhere and always. No matter what character-set your compiler uses.
To convert a numerical character ('0' – '9') to its corresponding value, just substract the ASCII code of '0' from the result.
int command = line.at(0) - '0';
The standard function to convert an ascii to a integral value is strtol (string to long) from stdlib (#include <cstdlib>). For information, see http://en.wikipedia.org/wiki/Strtol and then the link off that page entitled Using strtol correctly and portably. With strtol you can convert one numeric character, or a sequence of numeric characters, and they can be multiple bases (dec, hex, etc).
i am trying to convert a string like "12314234" to int
Boost has a library for this:
int i = boost::lexical_cast<int>(str);
Sorry to join this party late but what is wrong with:
int value;
std::string number = "12345";
std::istringstream iss(number);
iss >> value;
If you are passing hexadecimal around (and who isn't these days) then you can do:
int value;
std::string number = "0xff";
std::istringstream iss(number);
iss >> std::hex >> value;
It's C++ and has none of this hackish subtraction of ASCii stuff.
The boost::lexical_cast<>() way is nice and clean if you are using boost. If you can't guarantee the string being passed will be a number then you should catch the thrown error.