Question

c++ how can i convert a string to double/float? write a method that takes in a...

c++
how can i convert a string to double/float? write a method that takes in a string and converts to double.

example of a string: 4.2 9 7.5 + 2

Notice that each number/operator is separated by a space.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If any corrections needed kindly let me know by comments. I will correct it

Program

#include <iostream>
#include <sstream>
using namespace std;

void convert(string str)
{
    stringstream ss;

    /* Storing the whole string into string stream */
    ss << str;

    /* Running loop till the end of the stream */
    string temp;
    double found;
    while (!ss.eof()) {

        /* extracting word by word from stream */
        ss >> temp;

        /* Checking the given word is double or not */
        if (stringstream(temp) >> found)
            cout << found << " ";

        /* To save from space at the end of string */
        temp = "";
    }
}

// Driver code
int main()
{
    string str = " 4.2 9 7.5 + 2";
    convert(str);
    return 0;
}

Output

4.2 9 7.5 2

Add a comment
Know the answer?
Add Answer to:
c++ how can i convert a string to double/float? write a method that takes in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • Write a cell method that takes a single double as an argument and returns an int....

    Write a cell method that takes a single double as an argument and returns an int. The int should be the next highest whole integer (eg 3.14 would return 4). Use math not built-in Java methods mins) Write a method that takes a string as an argument. It your string is more than 10 letters or less than 1 letter return "Bad number". Otherwise, return the telephone number equivalent of the input. Your return can be of type String Eg...

  • 25. The ____________ method can be used to convert a string to a double. Group of...

    25. The ____________ method can be used to convert a string to a double. Group of answer choices c. ToString.double d. double.ToString b. double.Parse a. Parse.double 28. A constant variable's value can only be changed by other statements inside the class. Group of answer choices True False 30. When you assign a double value to a decimal variable, the double value is implicitly converted to a decimal with no loss of data. Group of answer choices True False

  • Write a ceil method that takes a single double as an argument and returns an int....

    Write a ceil method that takes a single double as an argument and returns an int. The int should be the next highest whole integer (eg 3.14 would return 4). Use math, not built-in Java methods (est. 20 mins) Write a method that takes a string as an argument. If your string is more than 10 letters or less than 1 letter return "Bad number." Otherwise, return the telephone number equivalent of the input. Your return can be of type...

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • RUBY! \Write a method called reverse_each_word that takes in a string argument of a sentence and...

    RUBY! \Write a method called reverse_each_word that takes in a string argument of a sentence and returns that same sentence with each word reversed in place. First solve it using .each Then utilize the same method using .collect to see the difference. For example: reverse_each_word("Hello there, and how are you?") #=> "olleH ,ereht dna woh era ?uoy" Hint: You can't use an enumerator on a string, so how can we turn our string into an array? Hint: How can we...

  • Functions can return a string, not just an int or a float. Write a function called...

    Functions can return a string, not just an int or a float. Write a function called everOrOdd which takes an integer parameter and returns either "Even" or "Odd" based on the value passed. In main, prompt the user for a number, called num, then pass num to evenOrOdd and display the returned value on the screen. Keep doing this until the user enters a zero. Use the following run as an example: Enter a number: 11 Odd Enter a number:...

  • I am trying to write a java method that takes two string parameters from a user...

    I am trying to write a java method that takes two string parameters from a user and then returns the first input after removing the letters that appear in the second input. For example: if input1 = fortune and input2 = tune then the method returns "for" seems simple but I am not understanding exactly how to tackle this thanks!!

  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT