Need help understanding what my assignment is wanting me to do

This is a discussion on Need help understanding what my assignment is wanting me to do within the C++ Programming forums, part of the General Programming Boards category; I have a c++ assignment that I need to start working on. I'm not asking for anyone to do it ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Need help understanding what my assignment is wanting me to do

    I have a c++ assignment that I need to start working on. I'm not asking for anyone to do it for me or anything. Rather, asking if someone could please explain what the assignment is asking and maybe a tip or two on how to accomplish it. Here is what the assignment says:

    "Write a c++ function named getareal() that uses exception handling to continiuosly accept an input string until a string that can be converted to a real number is entered. The function should return a double value corresponding to the string value entered by the user."

    I don't need help on how to write the function or anything, or on how to do the exception handling, I just don't know what it means by having the user countinousely enter a string until the string can be converted to a real number? And maybe a tip or 2 on how I'd go about that. Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    I guess it means that you ask the user to enter a number and check if it really is a number, so say that:

    Please enter a number:
    abdaba I'm not a number
    Please enter a VALID number:

    3a

    Please enter a VALID number:
    3.13
    Okay.
    But I could be misinterpreting... Though I don't get the name of the function... Note that it must accept a double!

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I got the program to accept integers, now I just need it to accept only one decimal and one slash. I need help making it accept a decimal or a slash

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
        
        bool getAreal(string);
        string value;
        float number;
        
        cout << "Enter a real number\n";
        getline(cin, value);
        
        if(!getAreal(value))
        cout << "The number you entered is not valid\n";
        else
        {
            number = atoi(value.c_str());
            cout << "The number you entered is " << value << endl;
            }
            
            system("pause");
            
            return 0;
            }
            
            bool getAreal(string str)
            {
                 int start = 0;
                 int i;
                 bool valid = true;
                 bool sign = false;
                 
                 if (str.length() == 0) valid = false;
                 
                 if(str.at(0) == '-' || str.at(0) == '+')
                 {
                              sign = true;
                              start = 1;
                              }
                              
                   if (str.at(1) == '.') valid == true;
                              
                 if (sign && str.length() == 1) valid = false;
                 
                 i = start;
                 while(valid && i < str.length())
                 {
                             if(!isdigit(str.at(i))) valid = false;
                             i++;
                             }
                
                 
                             
                             return valid;
                             }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    atoi should never be used for input validation – Especially not in a C++ program. The proper way to do it in C would be to use strtol, strtod etc. I'm not sure how good the formatted input operator is in C++ (if it detects numeric overflows etc like strtol), but a C++ alternative to atoi would be to simply use the >> operator from a stringstream object.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    You need to indent your code properly. This is not acceptable for any code.
    After that, e can continue discussing the assignment.
    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.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    You need to indent your code properly. This is not acceptable for any code.
    I completely agree with that. Correct indention will help you find your own mistakes ALOT easier, and in turn it will help anyone else you ask for help to see your mistakes ALOT easier.

    Also some logic notes
    Code:
    if (str.at(1) == '.') valid == true;
    why would that be needed if.

    Code:
    bool valid = true;
    Wouldn't it be safe to assume that if something was already true then you only need arguments that prove it false? Anything that proves something true that is already true could be considered a waste of time.. or a waste of arguments. Also did you really mean to state valid == true?
    = assigns value
    == compares two values
    Furthermore
    Code:
    if (str.length() == 0) valid = false;
    doesn't stop you from accessing other data points of the string such as position 1 which you check for in the code above. If you proved that the string has no length why would you continue to check positions within that length when you already proved the length doesn't exist? I'm not positive, but I think you are allowing for undefined behavior here. Aka... you already proved that length = 0 then you try to access point 1 which has no value because there is no length, so there is no garauntee that anything will work right.
    Last edited by Lesshardtofind; 09-24-2010 at 03:45 AM.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I have made a new program. Now the problem is that it always tells me the number is invalid no matter what. Can you please try and help me find the error? Thanks!
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    
    {
    	double getAreal();
    	double value;
    
    	cout << "Enter a real number\n";
    
    	value = getAreal();
    
    	cout << "The real number just entered is " << value << endl;
    
    	system ("pause");
    
    	return 0;
    }
    
    		double getAreal()
    	{
    		bool isvalidreal(string);
    		bool notAreal = true;
    		string svalue;
    
    		while(notAreal)
    		{
    			try
    			{
    				cin >> svalue;
    				if(!isvalidreal(svalue)) throw svalue;
    			}
    			catch (string e)
    			{
    				cout << "invalid real number, please re enter\n";
    				continue;
    			}
    
    			notAreal = false;
    		}
    
    		return atof (svalue.c_str());
    	}
    
    		bool isvalidreal(string str)
    		{
    			int start = 0;
    			int i = 0;
    			bool valid = true;
    			char period = '.';
    			char slash = '/';
    			
    			if (str.length() == 0) valid = false;
    
    			if(period > 1) valid = false;
    
    			if(slash > 1) valid = false;
    
    			while(valid && i < str.length())
    			{
    				if(isalpha(str.at(i))) valid = false;
    				i++;
    			}
    
    			return valid;
    		}

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    in isvalidreal(), you have the line:
    Code:
    char period = '.';
    then, before you change it's value, you have:
    Code:
    if(period > 1) valid = false;
    So, as '.' has the value 46 (ASCII value of '.'), valid is set to false here.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    I only looked at the 10 last lines. Don't you think that it will return false if you never effectively look for '/' or '.' in the string? You're basically comparing the ASCII values of those two chars to an integer...

  10. #10
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    If you wanted to search for a . or / in the string, and reject it if one is found, then I'd use something like:
    Code:
    if (strchr(str, period) != NULL) valid = false;
    But, that's for C - not sure what the equivalent qould be in C++

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Here's my most recent code, it works almost right. I Don't know the proper way to search for more than 1 period and slash. Also, when I enter a fraction, it prints only the numerator when i return the value. I'm still searching on how to go about searching for more than one period or slash but I'm not finding much. Thanks
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    
    {
    	double getAreal();
    	double value;
    
    	cout << "Enter a real number\n";
    
    	value = getAreal();
    
    	cout << "The real number just entered is " << value << endl;
    
    	system ("pause");
    
    	return 0;
    }
    
    		double getAreal()
    	{
    		bool isvalidreal(string);
    		bool notAreal = true;
    		string svalue;
    
    		while(notAreal)
    		{
    			try
    			{
    				cin >> svalue;
    				if(!isvalidreal(svalue)) throw svalue;
    			}
    			catch (string e)
    			{
    				cout << "invalid real number, please re enter\n";
    				continue;
    			}
    
    			notAreal = false;
    		}
    
    		return atof (svalue.c_str());
    	}
    
    		bool isvalidreal(string str)
    		{
    			int start = 0;
    			int i = 0;
    			bool valid = true;
    			char period = '.';
    			char slash = '/';
    			
    			if (str.length() == 0) valid = false;
    
    			
    
    			
    
    			while(valid && i < str.length())
    			{
    				if(isalpha(str.at(i))) valid = false;
    				if(str.at(i)== period) valid = true;
    
    				if(str.at(i)== slash) valid = true;
    
    				i++;
    			}
    
    			return valid;
    		}
    Last edited by nick753; 09-24-2010 at 04:49 PM.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    No one else has any input?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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