Hi,
I am trying to write a program that will read an integer as a string and it will add 1 to this integer. For example if it read 234, it will return 235.

My attempt is this:

``````#include <fstream>
#include <string>
using namespace std;

int n=num.size();
int i=n-1;

while(num[i]=='9'){
if (i==0){
num[i]=1;
for (int j=1;j<=n;j++){
num[j]='0';
}
}

num[i]='0';
i--;
}
if (i>=0){
num[i]=num[i]+1;
}
}

int main()
{
ifstream in("something.in");
ofstream out("something.out");

string num;
in >> num;

out << num;

return 0;
}``````

This code works good generally.
The only problem is that if the number has only 9s it outputs only 0s without 1.

Can you help me to fix this problem?

## All 9 Replies

hi mimis

``````num[i]=1;

// maybe if you replace this by

num[i]=0x31; //ascii coding for numeric 1 is hex 31 (third column, first row) or simpler num[i]='1';

// it will work``````

(didn't check rest of your code)

-- tesu Whaats all this crazy bs?

Convert the string to an int using stringstreams then add one to it.

commented: agree +28

Well, there might also be some ordinary mortals who GOT to do there profane programming homework when studying first-year computer sciences.

>>do there profane programming

Its their not there. :) And yes, we were all beginners at one time or another.

Thank you Dragon, I am still absolute beginner at English :(

-- tesu

My second attemp was that:

``````#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <string>
using namespace std;

int main()
{
ifstream in("something.in");
ofstream out("something.out");

string num;
stringstream ss;
in >> num;

int i,temp;

temp= atoi(num.c_str());
temp++;
ss << temp;
num=ss.str();

out <<num;

return 0;
}``````

But the compiler appear that error:
runaround.cpp:12: error: aggregate ‘std::stringstream ss’ has incomplete type and cannot be defined

I tried to do it with itoa but the compiler appear that itoa is not declered in this scope.

I have to include <sstream> header file. And you don't need atoi() because stringstream is a safer way to convert the string to an int.

Hi,
I looked at the task and found it interesting... So i did the code, you can get the algorithm for "9". Hope this helps

``````#include <iostream>
#include <string>
using namespace std;

int main()
{
while(1){
char c;
int i=0, factor=0,max=0;
int sub;
string  a;

cout<<"\nEnter the integer:";
cin>>a;

sub= a.size();

c = a[sub-1];
c = a;
max = a.size();
cout<<"Last Integer Found:"<<c<<"\n";

if (c=='9' && c =='9')//if the first and last element is 9
{
cout<<"\n1";

for(i=0;i<=a.size()-1;i++)
{
cout<<"0";
}
}

else if(c== '9')//if the last element is 9
{

for(i=a.size()-1;i>-1;i--)
{
if(a[i] == c)
{

factor=i;

if(factor<max)
{max = factor;}

}

else if(a[i]!=c)
{/*Do Nothing*/}

}

cout<<"\nLargest 9 found was element:"<<max<<"\n";

cout<<"Calculating using Finding NINE Method.....";

a[max-1] = (int)a[max-1] + 1;

i = 0;

for(i=max;i<=a.size();i++)
{
a[i]= '0';
}

cout<<"\nNew value is :"<<a<<"\n";
}

else if(c!='9')//if the last element is not 9
{
cout<<"9 Not detected Performing Normal Procedure\n";

sub  = 0;

sub = (int)c;

sub = sub + 1;

a[a.size()-1] = (char) sub;

cout<<"The new Integer is:"<<a<<"\n";

}

}
return 0;

}``````

Compiled it using VC++ 2008 and works perfectly.

commented: Nope -2 itoa doesn't even exist in the standard.

Use this for reference.

``````#include <iostream>
#include <string>
#include <sstream>

int main()
{
//convert string to int then add one
std::string s = "234";
std::istringstream myStream ( s );

int i;
myStream >> i;

std::cout << i + 1;

std::cin.get();
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.