Hi, I am new to C++, i have just started mt course. I got some problems to solve but don't know how to do it. I need someone who can solve it and explain it to me how it happened! Thanks.
Problem 1.1 You have to complete the given program to check whether a number is valid or not. Write only the process() function. The rules for checking validity are: [15 marks]
1) Input cannot contain any character other than digits or dot(.)
2) At most ONE dot(.) is allowed in the input
3) If there is a dot(.) in the input, it cannot be the last character of the input.
You need to check that each character of the "num" is a digit or dot.
You need to disallow multiple dots (easy way is to use a Boolean flag).
You need to make sure the last character is not a dot.
first just try to count the "." characters and see if there are more than one. That is a small task. I'll start you off with that.
1 2 3 4 5 6
int numDots = 0;
for (char ch : num)
{
if (ch=='.')
++numDots;
}
that loop will go through the entire text string "num" looking for .'s and incrementing a counter. If you dont yet understand for loops, you can read about them here http://www.cplusplus.com/doc/tutorial/control/
plug that in to your code and get process() to reject entries with more than one dot. When its working add another check and get that working also. keep going that way, a feature at a time.