Thread: Simple problem that I can't comprehend

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    15

    Simple problem that I can't comprehend

    Ok. I'm doing a test that I am sure to fail. I figured I should ask for help first.

    This is the question:
    "1. For each variable in the following C++ code, list the data type, the scope and the storage duration'

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    std::string result;
    
    int parseInt(const std::string &s)
    {
        static std::string digits[] = {"cero", "uno", "dos", "tres", "cuatro",
    				   "cinco", "seis", "siete", "ocho", "nueve"};
        static int count = 0;
        count++;
        result = "";
    
        for (unsigned int i = 0; i < s.length(); i++)
        {
    	if( isdigit(s[0]) == 0 )  //not a digit
    	    return -1;
    
    	result += digits[s.at(i) - '0'];
    	if(i < s.length() - 1)
    	    result += ",";
        }
    
        return 0;
    }
    
    int main(int argc, char *argv[]) {
        std::string n;
    
        std::cout << "Please enter an integer or \"quit\""
    	" to finish (example: 897).\n";
        std::cin >> n;
        while (n != "quit")
        {
    	if ( parseInt(n) != 0 )
    	    std::cerr << n << " is NOT a valid integer\n";
    	else
    	    std::cout << n << " == " << result << "\n";
    	
    	std::cin >> n;
        }
    
        return 0;
    }
    2. The above code has a small mistake. It does not reject input like 987.8. Can you find and fix the mistake?"


    Ok. Data types? Scope? Storage duration? If you guys could define what that means and what those look like, I'd be off to a much better start

    For part 2, I'm not 100% sure...but...could you use the assert() thing?


    Any tips would be appreciated. I'm not asking you to do it for me, so please don't accuse me of doing so.


    Thank you for your valuble help.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ummmm... so many problems with this....

    First off, I SERIOUSLY doubt we are allowed to help you on a test. Nobody here is going to help you cheat.

    Second, the question says to list the data types, scope and storage, you ask us to define all of these things and point them out in the code, then say you aren't asking us to do it for you?? Thats exactly what you just asked so hell yeah I'm going to accuse you of asking us to do it for you!

    Sorry, this is obviously an end of the semester test, if by now you don't know these simple programming concepts we aren't going to help you pretend to the teacher that you do.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    BAH xErath, what are you doing?? You are helping him cheat on a test???

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by phosphorousgobu
    Ok. Data types? Scope? Storage duration? If you guys could define what that means and what those look like, I'd be off to a much better start
    For part 2, I'm not 100% sure...but...could you use the assert() thing?

    Any tips would be appreciated. I'm not asking you to do it for me, so please don't accuse me of doing so.
    1st I read, then I post. You should try the same thing to.
    Last edited by xErath; 12-09-2004 at 12:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  3. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM