hi :) and welcome to C++ 

there's some problems in your code...
1. the string temp has the length 0 and you cannot use subscript operator to 
access for example temp[3] or temp[2] or .....
first in the declaration of temp you can initialize it by x (just to adjust 
the size to the appropriate)
string temp = x;
then modify the characters using your loop or ...

2.when you wanna output it via cout... it fails because it contain characters 
which are not in the defined range... (the characters does not have a symbol 
to show...) so you must either use the loop (for) you used with %d or add '0' 
to all of them and use cout<<temp; (I advise not to change the characters to 
0,1,2,... use '0' and '1' it's not hard to work with them...! you can simply 
++ them or anything (but not %2 /2 ... -> just put an if...))

3.you use the variable len in your code...
but be aware of the last loop!!
if you prepend a "1" in the beginning of your string then you must perform the 
loop for len+1 times for printing...!
so just use cout<<temp<<endl;

and some other things that makes your code a better c++ code! :
use cout<<endl; instead of cout<<"\n";
simply use x = "1" + temp
omit x=temp and q="1" ...

good luck.


On Friday 09 February 2007 02:43, mukesh tiwari wrote:
> hi everybody
>      I have to add one in a binary string . I m using string class but i m
> not getting the correct result . i hve just  started the programing in c++.
> here is my code and i will explaining it...
> in plusOne funtion i store the length of string in len variable .  i took
> two string temp and q and initialize q with "1".
> now i coping the content string x into sring temp by subtracting the ascii
> value of char 0
>    temp[i]=x[i]-'0';//is this assignment is wrong or right
>   then after i am doing simlple arithmetic .but problem is that when i m
> printing the temp and x string using printf function it gives the correct
> result  but at the same time cout is  giving  a blank line...
> now my algorithm do is if a carry is generated in last operation(when i=0)
> then append 1 before temp ......(for example if input is 111 then adding 1
> will give 1000 ...)
> now my confusion is that for multiple precison arithmetic can we use string
> class in c++ like char array in c or not ....
> plz explain me ...
> here is some input
> 101010//this input will give correct output
> 111//wrong out ...i cant understand why ..
> plz somebody expert in c++ explain .....i hve just started coding in c++..
> #include<iostream>
> #include<string>
> #include<cstdio>
> using namespace std;
>
>     class BinaryString
>      {
>
>             public:
>              string plusOne(string x)
>                 {
>                     int len=x.length(),i,carry=1;//initially carry will be
> 1 becoz we have to add 1 in string x.
>                     string temp,q;
>                     q="1";//intialization of string
>                     for(i=0;i<len;i++)
>                         temp[i]=x[i]-'0';
>                     for(i=len-1;i>=0;i--)
>                      {
>                         temp[i]=temp[i]+carry;
>                         carry=temp[i]/2;
>                         temp[i]=temp[i]%2;
>                      }
>
>                     for(i=0;i<len;i++)
>                         printf("%d",temp[i]);
>                     cout<<"\n";
>                     //cout<<temp<<"\n";
>                     x=temp;
>                     if(carry==1)//append 1 at starting of string temp;
>                         x=q+temp;
>                     for(i=0;i<len;i++)
>                         printf("%d",x[i]);
>                     cout<<"\n";
>                     cout<<x<<"\n";
>                     return(x);
>                 }
>     };
>
>     int main()
>     {
>
>                 string x;
>                 BinaryString B;
>                 while(cin>>x)
>                 B.plusOne(x);
>                 //cout<<x<<"\n";
>
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups-beta.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to