View previous topic :: View next topic |
Author |
Message |
Gegagabe Expert Cheater
Reputation: 0
Joined: 24 Jul 2007 Posts: 170 Location: on the computer
|
Posted: Mon Nov 16, 2009 5:17 pm Post subject: c++ help |
|
|
hey, im making a program that gives me the square root of any inputted value, i successfully made the program, but i dont know how to make it so that when the user inputs a letter, it would say "please do not enter a letter". the inputted value must be greater than 0 and positive.
oh this is done over c++
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Nov 16, 2009 6:31 pm Post subject: |
|
|
how are you getting the input ?
|
|
Back to top |
|
 |
Gegagabe Expert Cheater
Reputation: 0
Joined: 24 Jul 2007 Posts: 170 Location: on the computer
|
Posted: Mon Nov 16, 2009 7:03 pm Post subject: . |
|
|
um, the input is being inputted over cmd. Heres what i have soo far, but i cant make it to deny letters.
#include <iostream>
using namespace std;
int main()
{
double y;
double x0;
double x1;
bool looper = true;
cout<<"Enter a positive number that you would like the square root of" << endl;
cin >> y;
if(y > 0)
{
x0 = y / 4;
while(looper)
{
x1 = (x0 + y / x0) / 2;
cout << "x0: " << x0 << "\nx1: " << x1 << "\n\n";
if((x0<=x1+0.0001)&&(x0>=x1-0.0001))
{
looper = false;
}
x0 = x1;
}
cout << "\nThe square root of " << y << " is: " << x1 << "\n";
}
else
{
cout << "The number must be greater than 0"<< endl;
}
system("PAUSE");
return 0;
}
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
Back to top |
|
 |
Gegagabe Expert Cheater
Reputation: 0
Joined: 24 Jul 2007 Posts: 170 Location: on the computer
|
Posted: Mon Nov 16, 2009 7:11 pm Post subject: . |
|
|
i looked at the link, it just confused me even more.
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Mon Nov 16, 2009 8:13 pm Post subject: |
|
|
Here is an example with only positive integers. Your job is to modify it so it will accept a positive double.
Code: | #include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
int main()
{
string input;
cout << "Welcome to my super awesome program of uselessness! (-1 to exit)" << endl;
for(;;) {
cout << "Enter a positive integer: ";
getline(cin, input);
if(input != "-1") {
if(find_if(input.begin(), input.end(), not1(ptr_fun(isdigit))) == input.end()) {
cout << sqrt((float)strtoul(input.c_str(), 0, 0)) << endl;
} else {
cout << "RTFM" << endl;
}
} else {
break;
}
}
return 0;
} |
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Nov 17, 2009 4:08 am Post subject: |
|
|
What if you actually input a zero? Or if the number is out of range?
Better solution: use stringstreams. See std::istream::operator>>. Example: Code: | #include <iostream>
#include <string>
#include <cmath>
#include <sstream>
int main(int argc, char *argv[])
{
for(;;) {
std::cout << "A number: ";
std::string s("");
std::getline( std::cin, s );
std::stringstream ss(s);
int i = 0;
if( ss >> i && i >= 0 )
std::cout << "+-" << sqrt( static_cast<double>(i) );
else
std::cout << "Invalid input";
std::cout << std::endl;
}
return EXIT_SUCCESS;
} |
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Nov 17, 2009 11:21 am Post subject: |
|
|
Jani wrote: | What if you actually input a zero? |
What if? You can take the sqrt of 0. Hint: What is 0 x 0?
Jani wrote: | Or if the number is out of range? |
Then it gets truncated, and you should RTFM. The whole purpose was to show an example with integers, and his job was to do it with doubles. You could also set up a simple functor to do the checking.
Hint to OP: A functor is what you will use to parse a double.
Jani wrote: | Better solution: use stringstreams. See [snip] |
Initializing a stringstream for a simple conversion is overkill, it also doesn't account for whitespace. Starting a number with digits and then following it with nonsense also gets by.
Just because it works doesn't mean it's "the best". We could use regex pattern matching on the input to make sure it conforms, but again (for our purposes), that is overkill.
Last edited by Flyte on Tue Nov 17, 2009 11:45 am; edited 1 time in total |
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Nov 17, 2009 11:45 am Post subject: |
|
|
Athaem wrote: | Jani wrote: | What if you actually input a zero? |
What if? You can take the sqrt of 0. Hint: What is 0 x 0? | Sure you can. I was referring to using strtoul. strtoul returns 0 if you input a zero or a letter -> no way knowing if there was an error. 'kay, you can say that the OP didn't want to include zero, but still, it's a valid number and I'm talking about parsing numbers in general.
Athaem wrote: | Then it gets truncated, and you should RTFM. | Exactly and again there's no way knowing if you should display an error or the result.
Athaem wrote: | Initializing a stringstream for a simple conversion is overkill | Well, it provides a very simple way to convert a number. Sure you could write faster one yourself, but it'd take more code and knowledge than the OP probably has (no offence :P). Currently C++ standard libraries provide no better way doing this in a few lines.
Athaem wrote: | it also doesn't account for whitespace. | I kinda missed your point there. Like, let's say you have a string "123 223". The result with stringstreams would be 123. If you want to parse both of the numbers, just write a loop. Or if you mean that someone would input 123 223 as in 123223, then it's the user's problem. If you wanted to allow such numbers, just strip the whitespaces before parsing the number.
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Nov 17, 2009 11:57 am Post subject: |
|
|
Jani wrote: | Sure you can. I was referring to using strtoul. strtoul returns 0 if you input a zero or a letter -> no way knowing if there was an error. 'kay, you can say that the OP didn't want to include zero, but still, it's a valid number and I'm talking about parsing numbers in general. |
You should always sanitize user input. As you can see from my example, only digits will get by, therefore it will always return a valid number.
Jani wrote: | Exactly and again there's no way knowing if you should display an error or the result |
You could use errno if you really wanted to check for an overflow.
Jani wrote: | Well, it provides a very simple way to convert a number. Sure you could write faster one yourself, but it'd take more code and knowledge than the OP probably has (no offence ). Currently C++ standard libraries provide no better way doing this in a few lines. |
I agree with you, it is the simplest way only if you don't sanitize the input. Assuming you do that (and you always should), it's far better to use a function like strtoul.
Jani wrote: | I kinda missed your point there. Like, let's say you have a string "123 223". The result with stringstreams would be 123. If you want to parse both of the numbers, just write a loop. Or if you mean that someone would input 123 223 as in 123223, then it's the user's problem. If you wanted to allow such numbers, just strip the whitespaces before parsing the number. |
I mean that "123 345" is an invalid input, but it isn't treated as such.
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Nov 17, 2009 12:02 pm Post subject: |
|
|
Athaem wrote: | You should always sanitize user input. As you can see from my example, only digits will get by, therefore it will always return a valid number. | Yeps, if you do check the input before parsing it, then it's ok. My point is that you could use your parsing function to check the input in a case like this.
Athaem wrote: | I mean that "123 345" is an invalid input, but it isn't treated as such. | Ah, I thought it'd be better to try to parse whatever is available.
|
|
Back to top |
|
 |
|