Thread: Need some help on c++ assignment

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    26

    Need some help on c++ assignment

    I already completed one part of the assignment, but need help on the other part. The assignment was to create two recursive functions. One that converts strings to integers ( which i completed) and the other converts integers to strings.

    I need help on the last function.

    This is the prototype: string intToStr(int n);

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What have you tried? Can you use the ostringstream class?

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If you are not allowed to use ostringstream class,which probably don't,you may have to take a look at itoa itoa - C++ Reference

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    We are not allowed to use any functions that are similar to atoi or itoa. We also cannot use for or while.

    This is my other function that converts the string to an integer:

    Code:
    int stringToInt(string str)
    {
        static bool OK; //This variable signals if the string can be treated as int
        static int sign; //This variable signals about the sign of the number
       
       
        OK = true;
        sign = 0;
    
        int digit = 0; // The result of the function
    
        int length = str.length();
    
      
        if (length == 2) {
            if (str[0] == '-')
                sign = -1;
            else if (str[0] == '+')
                sign = +1;
        }
    
       
        if (length == 1 || length == 2 && sign != 0) {
           
            if (str[length-1] < '0' || str[length-1] > '9') {
                
                OK = false;
                return 0;
            }
          
            digit = str[length-1] - '0';
            
            if (sign == -1) digit = -digit;
        }
        // The main case:
        else {
           
            digit = stringToInt(str.substr(0, length-1));
          
            if (!OK) return 0;
            
            digit *= 10;
           
            if (str[length-1] < '0' || str[length-1] > '9') {
                OK = false;
                return 0;
            }
           
            if (sign == -1) // If the sign is minus, then do it by subtracting
                digit -= str[length-1] - '0';
            else // otherwise, do it by adding
                digit += str[length-1] - '0';
        }
        // Return the final result
        return digit;
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That seems a bit large, so probably a lot that can be improved upon there. Are you sure that you're allowed to use static variables?

    You still haven't posted your attempt at intToStr though, or asked any questions about it, so we can't help you with that yet. The good news is that it's easier than the other way around.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by iMalc View Post
    That seems a bit large, so probably a lot that can be improved upon there. Are you sure that you're allowed to use static variables?

    You still haven't posted your attempt at intToStr though, or asked any questions about it, so we can't help you with that yet. The good news is that it's easier than the other way around.
    Not sure about static variables. As for intToStr. I haven't started it yet. I would like some help with the base case of the recursive part.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The base case would be that the input is from 0 to 9, and all you need to do is return that digit as a string.
    Yes the base case really is that simple!

    That clearly leaves negative values, and values of 10 or greater, as remaining cases. Now code that up and have a go at the remaining cases then post your attempt.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Quote Originally Posted by iMalc View Post
    The base case would be that the input is from 0 to 9, and all you need to do is return that digit as a string.
    Yes the base case really is that simple!

    That clearly leaves negative values, and values of 10 or greater, as remaining cases. Now code that up and have a go at the remaining cases then post your attempt.
    So it would be like this? if(n==0) return 0?

    How would you return it as a string? Also could you also provide some advice on the main function? Would I be using the same main function as I did for stringToInt? Thank-you!

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Dylan Metz View Post
    So it would be like this? if(n==0) return 0?

    How would you return it as a string?
    That obviously only works for zero, but I said the base case would handle 0 to 9.
    I would probably use the std::string constructor that takes a count and a character, to build a string of 1 character that results from adding character '0' to the digit.
    Also could you also provide some advice on the main function? Would I be using the same main function as I did for stringToInt?
    Your main would obviously have to change because you'll be passing in an int instead of a string, and returning a string instead of an int. Not to mention you'll be calling a different function. Yes I'm really stating the obvious here, so if it doesn't answer the question, please ask a better question.

    I honestly expected that having written stringToInt, you would have no trouble writing intToString. How you managed to write the harder one and are then stuck on the easier one, I don't know.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    Here is what I have so far:
    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    //recursive function prototype
    string intToStr(int n);
    
    
    //main
    int main()
    {
    
    
    	/*/int tests[] = {
    		0, 100, -5, 10, 5, 10302
    	};
    
    
    	const int B = 6;
    	for (int i=0; i<B; i++)
    	{
    		cout << "Converting the integer "<< tests[i] <<" to string: ";
    		cout << intToStr(tests[i]) << endl;
    	}*/
    
    
    
    
    	int num;
    
    
    	cout<<"Please input a number: ";
    	cin>>num;
    	cout<<endl;
    
    
    	cout<<"Converting the integer "<<num<<" to a string: ";
    	cout<<intToStr(num)<<endl;
    
    
    	//string str;
    
    
    	system("PUASE");
    	return 0;
    }
    
    
    string intToStr(int n)
    {
    	if (
    
    
    
    
    
    
    
    
    
    
    }
    I need help with setting up the base case. I think it looks like this for int 0?

    if(?==0) return 0;

    How do you convert that to a string and return it?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    If you are not allowed to use ostringstream class,which probably don't,you may have to take a look at itoa
    If ostringstream was not allowed yet itoa was allowed, I'll facepalm and then blast the instructor since ostringstream is standard and itoa is non-standard, yet the effect of getting a library routine to do the work is the same.

    Quote Originally Posted by Dylan Metz
    I need help with setting up the base case. I think it looks like this for int 0?

    if(?==0) return 0;

    How do you convert that to a string and return it?
    It would be:
    Code:
    if (n == 0)
    {
        return string("0");
    }
    The explicit conversion to string is optional though.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    26
    How would the remaining cases look like? I sorta remember my instuctor talking about using either division or modulo(%)?

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you wrote the other function then you can certainly do a lot more than just "if (", especially after all the help we have provided already.
    It really starts to seem like you either copied the other function from someone else or you're really not trying very hard at all.
    As much as it may sound unfair suggesting this, we definitely do get people playing dumb to let others do the work for them.

    Unless you post a question about something we've said, or post an attempt with significantly more headway, I'm going to have to leave it to you to do some experimentation and learning on your own.
    Please, have a good think about it, and look stuff up on the internet when you have a general idea but cant figure out the specifics.
    Besides, you learn a lot more when not being spoon-fed.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you failing with the logic (ie, how does on accomplish such a face)? Then I'd suggest you make a flowchart to figure out the thinking.
    If you are failing with some syntax or don't know what facilities exist to achieve something, then by all means, ask away.
    I'd like to at least see that you have a way in mind on how to accomplish this behavior. Post your throughts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need some help with an assignment..please.
    By ronaldh12 in forum C Programming
    Replies: 3
    Last Post: 09-30-2011, 09:33 AM
  2. Need help with assignment
    By C_Davis in forum C Programming
    Replies: 4
    Last Post: 03-11-2010, 12:49 AM
  3. Assignment
    By willie in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 05:50 PM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. help me in Assignment
    By thaer89 in forum C Programming
    Replies: 13
    Last Post: 10-31-2007, 03:09 AM

Tags for this Thread