Hello mcgrim8,
Sorry for the delat. I had to restart my computer and still playing catch up with what I was working on.
5- Dear Handy andy, you said above that you disagree with writing "using namespace std".
Can you please tell me why? As far as I can see, you don't use this line but you write std before all lines instead. Isn't it too much unnecessary work or is there anything important that I am missing.
|
the line
using namespace std::
is a cheep way to put off teaching what should be done early. I have come to the understanding that books and schools do not teach what a name space is until later and teach the using line to avoid this. This is not really doing you any good and they are hoping you will learn about name spaces before it becomes a problem. This does not always work.
If you get use to using
using namespace std::
some day it
WILL cause you a problem. Then you will hours trying to figure out the problem only to end up here asking what is wrong.
Another problem is by not knowing what is in the standard name space yo will write variable names or function names that are the same as in the standard name space and the compile will produce errors because it does not know which variable of function to use and when the compiler tries to put "std::" infrount of your function and the parameters do not match the one in the standard name space the compiler will produce an error.
That is a quick exploitation, but you can do a search here on "using namespace std" for more information. There have been many posts dealing with this subject. I also found this link to be helpful even if it is ahead of what you know for now its worth reading.
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
You can also take a look at this page for now
http://www.cplusplus.com /doc/tutorial/namespaces/ The whole page is worth reading.
For now what you are most likely to use is
std::cin
,
std::cout
,
std::endl
and
std::string
. These will increase as yo add newer header files to a program. Along with learning what is in the standard name space slowly you will also be learning VS's error messages and how to fix them slowly. Learning slowly and with repetition you will learn better than having to learn this all at once later.
As for your program.
First go back and take a look at the code in
http://www.cplusplus.com/forum/beginner/257188/#msg1123671 I worked this up based on what you started with. Sorry if some of the things I did to speed up testing are confusing to you.
In time you will get tired of having to enter information through a "cin" statement just to test the program and fine that defining a variable, used in a "cin" statement, with an initial value and then commenting out the "cin" statement you can skip having to enter the same information every time the program and focus more on what is not working.
Looking at your latest code. What happened?? You had a good start and then came up with something that does not work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void enter_numbers(string a, string b)
{
int m = stoi(a);
int n = stoi(b);
int max = 0;
if (a.size() >= b.size())
{
max = a.size();
}
else
{
max = b.size();
}
|
First you need to rename the function. "check_string". This describes what the function does. "enter_numbers" is misleading as you enter nothing in the function.
On lines (4) and (5) you are using "stoi" to early. If the first string is "1234" everything is fine, but if it is "123a" "stoi" will convert (123) into an "int" stopping at the first character that is not a number. And if you have "12a4" it will convert (12) into an "int" stopping at the first character that is not a number. And the last example "a123" will cause a run time error because "stoi" can not convert the letter into a number. This is the same for both lines.
Line 6. What is this for? It has no purpose and does you no good.
The if/else does not make any sense.
In the next part:
1 2 3 4 5 6 7 8
|
for (int i = 0; i < max; ++i)
{
if (!isdigit(a[i] || b[i]))
{
cout << "invalid input, please enter digits only!" << endl;
break;
}
}
|
If a has a size of (2) and b has a size of (3) your for loop has a problem.
When "i" has a value of (1) everything is fine, but when "i" has a value of (2) this will put you past the end of the first number into memory that you have no idea of what is there.
Next is the if statement. I have no idea where you may have seen this or why you even think it works because it does not. It would be nice if it did, but that is not the way it is designed. The function "isdigit" works on only character at a time. What may hept here is
if (!isdigit(a[i]) || !isdigit(b[i]))
and with two (!) nots using "&&" may be need in place of the "||". Because the function is so wrong I have not tested this part much.
Next:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
for (int j = 1; j <= 1; j++)
{
if (a.size() > 4 && b.size() <= 4)
{
printf("%d too large (max 4 digits)\n", m);
break;
}
else if (b.size() > 4 && a.size() <= 4)
{
printf("%d too large (max 4 digits)\n", n);
break;
}
else if (a.size() & b.size() > 4)
{
printf("%d and %d are too large (max 4 digits)\n", m, n);
}
else
{
cout << "the sum is " << m + n << endl;
}
}
|
The for loop goes from (1) to (1) what is the point as the code in the for loop executes only once. The for loop is not needed.
What possessed you to think that a "printf" is better than "cout"? First it is not a good practice to mix C code in a C++ program. Sometimes it is unavoidable and it does work, but here the "printf" statements do not do the output any better than a "cout" statement.
For the function to work properly it should look like:
1 2 3 4 5 6 7 8 9 10
|
bool check_string(std::string str)
{
Check that the string has a length of 4.
If not there is no reason to proceed. Return false
In a for loop Check that the four characters are digits.
If not return false
If everything meats the requirements return true else return false.
}
|
My attempt at pseudo code such as it is. The idea is to show you the order that this should be done in. Refer to the code in
http://www.cplusplus.com/forum/beginner/257188/#msg1123671 for a better idea.
Also you should be checking one string at a time.
The "main" function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main()
{
char answer;
do
{
string y, z;
cout << "enter the first number" << endl;
cin >> y;
cout << "enter the second number" << endl;
cin >> z;
enter_numbers(y, z);
cout << "try again(Y/N)?" << endl;
cin >> answer;
} while (answer == 'Y' || answer == 'y');
return 0;
}
|
I meant to mention this earlier and got ahead of my-self. Avoid using a single letter for a variable name. It is hard to keep track of when reading through the code. This will become more noticeable when your code gets larger.
A variable should be a noun that describes what it is or does.
For lines 9 and 10 and then 11 and 12 you can put these in a do/while loop (two loops) with the while condition being
(!CheckString(?))
. This will stay in the loop until you enter a correct number.
Only when you have good strings to work with you use the "stoi" function then add your numbers together.
Hope that helps,
Andy