how to assign a string to integer..

i have a number into string and i want to assign it to an integer variable...plz help me soon
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	char ch[20];
	cout<<"Enter number";
	cin>>ch;
	int n;
	n=(int)ch;
	cout<<"\n"<<n;
	getch();
}

the n is not display the number that i entered into ch...
1
2
3
#include <cstdlib>

    int n = atoi(ch);


http://www.cplusplus.com/reference/cstdlib/atoi/
Last edited on
thanks...
similarly i want to assign an integer number to a string plz tell this...
That's possible using std::stringstream: http://www.cplusplus.com/reference/sstream/stringstream/

1
2
3
4
5
6
7
8
#include <sstream>

int num = 5;
std::stringstream ss;
ss << num;

std::string word = ss.str();
std::cout << word << std::endl; // Will output 5 
Topic archived. No new replies allowed.