Need the output to back again

This is a discussion on Need the output to back again within the C++ Programming forums, part of the General Programming Boards category; I made a function that give the value of any number like the way you read it. For example you ...

  1. #1
    Alabsi
    Guest

    Need the output to back again

    I made a function that give the value of any number like the way you read it. For example you have the number "144566" the out put of the function will be printed to the secreen as follows:11241526
    and the function is as follows:

    Code:
    
    #include<iostream>
    #include <climits>
    
    
    using namespace std;
    int main (){
        unsigned long int num,temp2,
        n,count,diviser,digit,i,counter,temp1,base;
        n=1;
    
         
    	cout<<"Please enter the number :"<<endl; //getting input
        cin>>num;
    
        temp1=num;
    	count=0;
    
    	
    
        while (temp1!=0){                      //counting number of digits
           temp1=temp1/10;
           count=count+1;	     
           }
    	cout<<"The look-and-say value of the given number is :";
    
         i=1;
         temp2=0;
         counter=1;
    	 
         count=count+1;
    	 base=1;
    	 for(int r=1;r<=count;r++){ // to get the power of 10
    		 base=base*10;
    	 }
    		
    	diviser=base;
         for (i=1; i<=count;i++) {  //seperating each digit
    								//and printing output
             
    		 	
    		 diviser=(diviser/10);
    	 
             digit=num/diviser;
    
             if (i> 2){
                if(temp2==digit)
                   counter++;
                else {
                
    				cout<<counter<<temp2;
                counter=1;
    
    					}
              }
    
             temp2=digit;
             num=num%diviser;
            
           }
    
    			cout<<counter<<temp2;//this to show the last digit
    
    	 cout<<endl;
    
      return 0;
    
      }
    Now what I need is to make the output of that function goes back again so the function read it again as many times as I want.My problem is that the output of that function is printed to the screen as and is not saved into variable because I can not save it for being an output to the screen that changes with the loop. I'm not allowed to use the string liberary. If you want to understand what I mean just run the function and what I want is to make the output of the function to go back again in the same function and no recursive is allowed yet. Please if any idea or help I would appreciate it.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    Re: Need the output to back again

    Originally posted by Alabsi
    I made a function that give the value of any number like the way you read it. For example you have the number "144566" the out put of the function will be printed to the secreen as follows:11241526


    That, in no way, makes any sense, whatsoever. Could you explain that?

    Some tips (shorthand, basically)

    instead of count=count+1; do count++; It does the same thing.
    instead of temp1=temp1/10; do temp1/=10 it does the same thing too
    instead of base=base*10 do base*=10 (same thing again)
    I'll edit your code to reflect this so you can see what I mean...it's just a shorter and clearer way of writing things

    Code:
    #include<iostream>
    #include <climits>
    
    
    using namespace std;
    int main (){
        unsigned long int num,temp2,
        n,count,diviser,digit,i,counter,temp1,base;
        n=1;
        cout<<"Please enter the number :"<<endl; //getting input
        cin>>num;
    
        temp1=num;
    	count=0;
    
    	
    
        while (temp1!=0){                      //counting number of digits
           temp1/=10;
           count++;	     
           }
    
        cout<<"The look-and-say value of the given number is :";
    
         i=1;
         temp2=0;
         counter=1;
         count++;
         base=1;
         for(int r=1;r<=count;r++){ // to get the power of 10
    	 base*=10;
         }
    		
         diviser=base;
         for (i=1; i<=count;i++) {  //seperating each digit
                                                 //and printing output
             	 diviser/=10;
                         digit=num/diviser;
    
                         if (i> 2){
                                if(temp2==digit)
                                     counter++;
                                else {
                                     cout<<counter<<temp2;
                                     counter=1;
                                }
                         }
    
             temp2=digit;
             num%=diviser;
            
           }
            //no need to make two calls to cout, just stick endl on the first
           cout<<counter<<temp2<<endl;//this to show the last digit
    
      return 0;
    
      }
    As for what that's supposed to do... you lost me. Could you clarify?
    Away.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    144566

    11241526
    one '1', two '4's, one '5', two '6's

    Just parse out all the digits of the number and sort them. Then read them back the way you want.

  4. #4
    Alabsi
    Guest

    Look and say

    ygfperson got it exactly right, this function just out put the number the same way you read it, just run it and put any number and you will see what I mean like you put this number 22488 the output will look 221428, what I want now from this function is just to input the privious output as current input that the number we have just saw as output which is 221428 will be input and its output will be 2211141218 and so on as much as I want. Thanks and waiting for any suggestions and Idea and my problem is how to save every output and put it back in the function.just remember no string or recursion are allowed yet, arrays are O.K.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I don't know how you read numbers, but I read 22488 as "twenty-two thousand four hundred and eighty-eight".

    That said, put your code into a function that takes the int as a parameter and returns the converted int. You can put it into a loop which aska if you want to continue.

  6. #6
    Alabsi
    Guest

    Look and read

    Cat.. I do not want to read the number as a whole like 3314 ... how many 3s are there.. 2 right? so we say 23and how many 1s there is only one 1 so we print 11 and so on so the total number is 231114 , I hope this is clear. Now you want me to put the output back into the function and this is my problem is how to save the output into a variable so I can pass it back into the function because I do not just want to loop the same function but I want to loop the output...please if you can help with idea thanks.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>pass it back into the function
    Code:
    int foo(int input)
    {
      int NewNumber;
      
      // Do work here
      
      return NewNumber;
    }
    Is that what you mean?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Alabsi
    Guest

    Look and say

    I hope that I can put my out put into variable so I can return it back. The problem is that my output is printed to the screen one at a time without being saved in a variable and I'm trying to save it in a variable but I'm out of hope this is my main question. If I coud put the "cout<<counter<<temp2" in a variable then my problem is solved. I can put it back easy to the same function I do not have problem with that......I hope I'm clear now and thank you for the help....

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use a string or a stringstream.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Alabsi
    Guest

    Please Look and say

    Using string is not allowed yet.Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 09:33 PM
  3. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  4. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  5. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 07:46 PM

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