look and read

This is a discussion on look and read within the C++ Programming forums, part of the General Programming Boards category; I tried to solve the program for a look and read digit. The user input a number like 488667 and ...

  1. #1
    absi
    Guest

    look and read

    I tried to solve the program for a look and read digit. The user input a number like 488667 and the result of the program should be 14282617 with no function,array, or pointer used. To solve the program I get an error which is that the counter does not cumulate.Please if somebody have any suggestions I would appreciate it.

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    
    
    	
    	int m,c,i,n;
    	int counter=0;
    	cout<<endl<<endl;
    	cout<<"    Please enter number"<<endl;
    
    	cin>>n;
    
    	int s =log10(n);
    	
    	
    
    for(i=s;i>=0;i--)
    
    	{
    	
    	
    			c=pow(10,i);
    			m=(pow(10,(s-i)));
    
    	
    		 if(((n/(c))%10)==((n/(c/10))%10))
    		{
    					counter+=1;
    				
    		 }
    			
    
    	cout<<counter<<(n/c)%10;
    
    		if(((n/(c))%10)!=((n/(c/10))%10))
    
    			cout<<"1"<<(n/c)%10;
    			
    				 	
    }			
    
    	
    	return 0;
    
    
    
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I recommend long data type instead of int. As for the algorithm, you designed it, so debug it.

    Kuphryn

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    One thing... I'd advise using <cmath> instead of <math.h>. The former is a member of the standard library, and encapsulates its members in the std namespace. Aside from that, what is your algorithm supposed to be doing?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Absi
    Guest

    Look and read

    Zach; the algorithm is that if the first number is the same as the number after it( repeated number) I should see how many times it reapeats it self and then print the number of times and the number itself if not just print out number 1 and the number itself.
    for example when I input the number 7488999 the secreen should show 17142839 i.e look and read . As for "kuphryn"
    please if you do not want to help just do not, I know it is my code but if I'm able to debug it I would not post it and I think this forum is to offer help and information exchange it.Thanks

  5. #5
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    I got bored, so I tried my hand at this quick. Unless you plan on doing some actual math with these numbers, its easier to handle it as a string.
    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::cin;
    using std::string;
    using std::endl;
                                                                                    
    int main()
    {
        string input;
                                                                                    
        cout << "Please input a number: ";
        cin >> input;
                                                                                    
        int size=input.size();
        int count=1;
        for(int x=0;x<size-1;++x)
        {
            if(input[x]==input[x+1])
                count++;
            else
            {
                cout << count << input[x];
                count=1;
            }
        }
        cout << count << input[size-1];
    }
    It should work fine I believe.

  6. #6
    Nubi
    Guest
    but if you are looking to do math with it...

    use the modulus '%'

    and with a few for loops and such you should have your answer.

  7. #7
    absi
    Guest
    Thanks guys, yeh.. I should do the math no string allowed yet. I did used the Mod but my problem was with counter and I found out the reason why it is not accumulating because it was not reading the next digit right. Thank you very much any way for the help...I appreciate it.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21