Need Help Converting a string into an int

Hey I am very new to C++ and I have been doing some tutorials about it and then i decided I will try and make a calculator in c++

In the code below i want the user to input their numbers as many times as they would like. But when they press c it cancels the loop. But if plus is a int it cant process c so i changed int plus to string plus but then it won't total + plus;

I did some reseach and i found this atoi to convert the int into a string but it didnt work because it is really confusing and i dont know where to place the code.
So i would really appreciate for someone to help me figure out how to convert the int into a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int addition()
	{
		int plus;
		int total = 0;

		cout << "What is the first number you will like to add? or type c to cancel. \n";
		cin >> plus;
		while(plus != "c"){
			total = total + plus;
			cout << "what is the second number you will like to add? or type c to cancel. \n";
			cin >> plus;
		}

		cout << "The numbers you have entered add up to " << total << endl << endl << endl;

		return retry();
Use a std::ostringstream.
1
2
3
4
5
6
7
8
9
#include <string>
#include <sstream>

std::string int_to_string( int i )
{
    std::ostringstream stm ;
    stm << i ;
    return stm.str() ;
}


For a tutorial on using std::ostringstream, see http://www.artima.com/cppsource/streamstrings.html

There are many standard and portable ways to do this; see: http://www.gotw.ca/publications/mill19.htm

Last edited on
Thankyou very much. I will let you know if it works :D
 But if plus is a int it cant process c so i changed int plus to string plus but then it won't total + plus;


I am not sure what you mean by this statement any how this should solve your purpose .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int addition()
	{
		int plus;
		char ch ; 
		int total = 0;

		cout << "What is the first number you will like to add? or type c to cancel. \n";
		cin >> plus;
		 do
		{
			total = total + plus;
			cout<<"\n Do you want to continue  .. press y / Y";
			cin>>ch ; 
			if( ch == 'y' || ch == 'Y')
			cout << "what is the second number you will like to add? or type c to cancel. \n";
			cin >> plus;
		}while(ch != 'c');

		cout << "The numbers you have entered add up to " << total << endl << endl << endl;

		return 0;
}


other wise as JLBorges say ostringstream should solve your purpose
Ok i just tried bluecoder code it everything was working until I press c to get out of the loop. But it just prints out endless amounts of "what is the second number you will like to add? or type c to cancel."
while(i != "c"){

I just tried the ostringstream code but im getting an error at != cause it is incompatible
> In the code below i want the user to input their numbers as many times as they would like.
> But when they press c it cancels the loop
...
>I just tried the ostringstream code but im getting an error at != cause it is incompatible

The code to read either a "c" or an integer from stdin would be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    int number ;
    std::string str ;

    // read a string from stdin
    while( std::cout << "enter an integer or 'c' to cancel: " && std::cin >> str )
    {
        if( ( str == "c" ) || ( str == "C" ) ) // if user entered 'c'
        {
            std::cout << "cancelled\n" ;
            return 1 ; // exit program
        }
        else // try to convert the string to an int
        {
            std::istringstream stm(str) ;
            // if the string contains a number and nothing else
            if( stm >> number && stm.eof() )
            {
                std::cout << "the integer is " << number << '\n' ;
                break ;
            }
            else // inform the user and try once again
                std::cout << "error in input '" << str << "'\n" ;
        }
    }

    // use number
}

make some changes in
1
2
3
4
5
6
7
8
9
10
11
12
 do
		{
			total = total + plus;
			cout<<"\n Do you want to continue  .. press y / Y";
			
			cin>>ch ; 
			if( ch == 'y' || ch == 'Y')
			{
				cout << "what is the second number you will like to add? or type c to cancel. \n";
				cin >> plus;
			}
		}while(ch != 'c');
Hey

Do you know if there is any way of writing this in C++

1
2
3
4
5
6
if (cin = int){
       cin >> plus;
}
else if(cin = string){
       cin >> cancel;
}

you can't do that since cin i global istream object,
but you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
	union
	{
		char* cancel;
		int plus;
	};
	int menu;
	cout << "char = 1, int = 0";
	cin >> menu;
	switch(menu)
	{
	case true:
		cin >> cancel;
		break;
	case false:
		cin >> plus;
		break;
	default:
		cout << "wron input";
		break;
	}
	cin.ignore();
	return 0;
}
we have to make some modification in the code .

i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
nt _tmain(int argc, _TCHAR* argv[])
{
	union
	{
		char* cancel;
		int plus;
	} myunion;
	int menu;
	cout << "char = 1, int = 0";
	cin >> menu;
	switch(menu)
	{
	case true:
		myunion.cancel = new char[50];
		cin >> myunion.cancel;
		delete [] myunion.cancel;
		break;
	case false:
		cin >> myunion.plus;
		break;
	default:
		cout << "wron input";
		break;
	}
	cin.ignore();
	
	return 0;
}
bluecoder,
1
2
3
4
5
case true:
	myunion.cancel = new char[50];
	cin >> myunion.cancel;
	delete [] myunion.cancel;
	break;


why are you deleting just initialized char* ???

isn't that the same as deleting whole case statement?
HI codekiddy .. you are right . .. but i just wanted to give JAM 96 the idea ..
Topic archived. No new replies allowed.