answersLogoWhite

0


Best Answer

std::string input = "";

std::getline (std::cin, input); // get input from stdin

std::stringstream ss (input); // place input in a string stream

integer num = 0;

if (ss >> num) // extract integer from string stream

{

// Success!

}

else

{

// Fail!

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write a C plus plus Program to convert a string into an integer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program to input an integer?

The safest way is to capture the input as a string and then convert the string to an integer. The reason is that all standard input (regardless of where it comes from) is done through character streams and it's safer to capture this input using a string rather than trying to perform conversions on the incoming data directly.


How do you write a Java program to convert a decimal number to an octal number?

public class Dataconversion { public static void main(String[] args) { System.out.println("Data types conversion example!"); int in = 44; System.out.println("Integer: " + in); //integer to binary String by = Integer.toBinaryString(in); System.out.println("Byte: " + by); //integer to hexadecimal String hex = Integer.toHexString(in); System.out.println("Hexa decimal: " + hex); //integer to octal String oct = Integer.toOctalString(in); System.out.println("Octal: " + oct); } }


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a program convert a string from uppercase to lower case and vice-versa without using string tolower and string toupper commands in TCL?

[ string toupper $str ] or [ string tolower $str ]


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


Write a program to subtract integer y from integer x?

x -=y;


How do you write a vbscript for converting alphanumeric to numeric?

num = InputBox("Enter a number: ","PROGRAM: Square") sumSquare = CInt(num) * CInt(num) MsgBox("The square of " & num & " = " & sumSquare) ===== *NOTE*: The function CInt() is what we use to convert a text string to become a numeric integer value.


Write a program in c to convert a given string to lower case?

Use the tolower() function. Example: char* a = 'X'; a = tolower(a); printf("%c", a);


Write a program to convert a 2 digit BCD number into hexadecimal number?

Write a program to convert a 2-digit BCD number into hexadecimal


Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in VB?

what is string


Write a program in java to display a string message using Servlets?

i dont no string for servlate


Write a program that prompts the user to input a positive integer It should then output a message indicating whether the number is aprime or not?

Output a prompt.Either:Read from standard input (std::cin) to an integer.Or:Read a line from standard input (std::getline()) to a string.Create a string stream (std::stringstream) to read the string.Read from the string stream to an integer.For each integer from 2 to half the entered integer:If the entered integer is divisible by the current integer:The number is not prime.Exit the program.The number is prime.Exit the program.