Loops

Pages: 1... 4567
Hi SGM,

Yes that's it, though I would summarise it like this:

We are writing a design methodology with comments, then incrementally writing code until we have finished.

So we do an outline, then fill in more details, then write code.

It is an organised approach.

Now you should copy & paste the comments & code, from the code snippet that has the whole UseCalculator function, into the right place in your main(). Put the function definitions outside main as my comments indicate.

Note that I said that we won't need the IsValidFloat function.

Now you should have enough info to write the rest of the code. Also remember to check for division by zero when you do the / operator .

See how you go
Sorry I have not made any recent posts. I have decided to abandon this project for a new approach. I picked up a new book "Beginning C++ Game Programming"

Game development is my goal via C++. You have taught me a lot and I will be returning to the forum relatively soon.

Thank you for helping me. Especially The IdeasMan, who has stuck with me during this project. Even though I may be a pain! =)
SGM3,

I think it is a shame that you choose to abandon this, with my last 2 posts, we had 95% of the code.

I picked up a new book "Beginning C++ Game Programming"


Well this is fine, just remember that C++ is more complicated than C. The code completed so far is only basic C, we haven't done arrays, structs or pointers for example, and these are reasonable & important topics on their own.

C++ has classes with inheritance, overloaded functions and operators, virtual functions, constructors & destructors, access specifiers, templates , Standard Template Library. These are some of the main topics, I am not saying that these are really difficult, but they are more complex.

I don't want to put you off something you are looking forward to doing, but I think you would do well to learn a little bit more of the basics of C programming. I don't know how well your book might take you through this, if it does then that is great.

I just think it is really important to get the basics right (& understand them) before going on to more difficult things. Again I don't know how steep the learning curve is in the book you have, maybe it is ok. I have seen books before, and have I thought that they would be quite difficult, if one didn't have some basic knowledge.

This site has a bunch of beginner exercises, that gradually get harder, some of them are games. It would be good experience for you to do at least some of these.

I probably sound like a school teacher right now, however I am hoping that you will see that my advice is worthwhile.

Especially The IdeasMan, who has stuck with me during this project.


Thanks & No worries :D pleased to help

This book, as far as I can tell, is similar to other C/C++ intro books I own. But, the approach is different.

The first couple "lessons" cover output and input, operators, variable, if statements, and loops. These from what I have seen appear in almost every intro to programming. Instead of general examples :
1
2
3
4
5
6
if ( TRUE ) {
  // Execute these statements if TRUE
}
else {
  // Execute these statements if FALSE
}


The book uses practical references. Unfortunately it is hard for me to grasp general concepts because I lose the actual point. I start asking questions: when would you use this? How is it used? Why or why not would you use this. It wasn't until I began work on this project with your help, that these functions started to make sense. You pointed me in a direction that I never thought of. Which made me think of my program in an entirely different way.

I still have the source. I don't want to delete it. I will go back to it, not sure when.

Thanks again!
@ TheIdeasMan

I decided to finish work on my project. Getting overwhelmed with classes and pointers. I feel I have a better grasp on what we were trying to do. I have to admit, most of the time I wasn't really sure what was going on. It was definatly on my part for not taking the time to read and understand what we were doing.

I created the calculator in a new source file, for testing. That being said. Here is my calculator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>

void calculator();
void addition (int& funcNum1, int& funcNum2, int& funcAnswer);
void subtract (int& funcNum1, int& funcNum2, int& funcAnswer);
void multiply (int& funcNum1, int& funcNum2, int& funcAnswer);
void divide   (int& funcNum1, int& funcNum2, int& funcAnswer);


int main()
{
    calculator();

    return 0;
}
void calculator()
{
    std :: cout << "\nCalculator" << std :: endl;
    std :: cout << std :: endl;

    int num1, num2, answer;
    char operators;

    std :: cout << "What operator would you like to use? ( +, -, *, /) ";
    std :: cin >> operators;

    switch (operators)
    {
        case '+':
            addition(num1, num2, answer);
            break;
        case '-':
            subtract(num1, num2, answer);
            break;
        case '*':
            multiply(num1, num2, answer);
            break;
        case '/':
            divide(num1, num2, answer);
            break;
        default:
            std :: cout << "Invalid input." << std :: endl;
            break;
    }
    if (operators == '+' || operators == '-' || operators == '*' || operators == '/')
    {
        std :: cout << num1 << " " << operators << " " << num2 << " = " << answer << std :: endl;
    }

}
void addition (int& funcNum1, int& funcNum2, int& funcAnswer)
{
    std :: cout << "Enter first number: " << std :: endl;
    std :: cin >> funcNum1;

    std :: cout << "Enter second number: " << std :: endl;
    std :: cin >> funcNum2;

    funcAnswer = funcNum1 + funcNum2;
}
void subtract (int& funcNum1, int& funcNum2, int& funcAnswer)
{
    std :: cout << "Enter first number: " << std :: endl;
    std :: cin >> funcNum1;

    std :: cout << "Enter second number: " << std :: endl;
    std :: cin >> funcNum2;

    funcAnswer = funcNum1 - funcNum2;
}
void multiply (int& funcNum1, int& funcNum2, int& funcAnswer)
{
    std :: cout << "Enter first number: " << std :: endl;
    std :: cin >> funcNum1;

    std :: cout << "Enter second number: " << std :: endl;
    std :: cin >> funcNum2;

    funcAnswer = funcNum1 * funcNum2;
}
void divide   (int& funcNum1, int& funcNum2, int& funcAnswer)
{
    std :: cout << "Enter first number: " << std :: endl;
    std :: cin >> funcNum1;

    std :: cout << "Enter second number: " << std :: endl;
    std :: cin >> funcNum2;

    funcAnswer = funcNum1 / funcNum2;
}



I don't think it is too bad. Maybe I should make a function for this :
1
2
3
4
5
std :: cout << "Enter first number: " << std :: endl;
    std :: cin >> funcNum1;

    std :: cout << "Enter second number: " << std :: endl;
    std :: cin >> funcNum2;



Looking forward to your criticism! =D
Last edited on
Welcome back !

I see you have taken a different approach, that is OK, nothing like trying different things. The thing is, we were 95% of the way there last time.

Any way for this code, there are a few things:

The use of ints won't be any good because of the integer division, you need to use float or double to get the expected answers. The division needs to have a check for divide by zero, this is not as straight forward as it sounds, when using floats or doubles. This is my post to someone else yesterday.


EPSILON is the biggest number such that 1 + EPSILON still equals 1. There is a FLT_EPSILON for floats and DBL_EPSILON for doubles. The EPSILON's need to be scaled according to the number you have. If your number is 1000 say, then you need 1000 * DBL_EPSILON.

The thing to understand is the 'distance' between representable numbers over different ranges. If we consider a float that equals 1, the next greatest number that can represented is about 1 + 1E-8, so the 'distance' is 1E-8. If the float is 1E8, the next greater number is 1.0000001E8, so the distance is 1.0. At 1E9 the distance is 10, at 1E10 distance is 100. So when testing for equality you really need to test that your number is between the number below and the number above. That's where the EPSILON's come in. Similar for for less than and greater than.

The reason that inexact representation occurs, is because of the binary fractions that are used. A binary fraction of 1.1101 is 1 + 0.5 + 0.25 + 0.0625. USing this method it is possible to represent most of the numbers, but not all.

The other consideration is the precision of the answer that you require. Say you wish to use a prcision of 0.01, you can do this:

1
2
3
4
5
6
7
8
9
10

double MyPrecision = 0.01;
double MyDouble = 0.1;  //not exact is about 0.0999...97 to 16 sig figures 
double MyNum = 10 * MyDouble ;  // not exact 0.999...97

if (MyNum  > (1.0 - MyPrecision)  && MyNum  < 1.0 +MyPrecision )  //this is true
            cout << "MyNum equals 1.0   0.01 precision" << endl;
else 
            cout << "MyNum is not equal to 1.0   0.01 precision" << endl;


This is OK but the 0.01 suffers from the same problem as other floating point numbers, so in some situations you might still need to use DBL_EPSILON as well.

Generally floating point is a pain in the A**, but you can get used to it.

Google some more there is heaps of stuff about this topic.


The other way is to use an absolute function like this: (from iHutch105)

You could include the math header and do something like this. It's not perfect, but it'll probably suit your needs.
1
2
3
4
5
// Replace
if(added==price)

// With
if (fabs(price - added) < 0.00001)


This method is better because there is only one test. You could substitute DBL_EPSILON for the 0.00001.

A small thing - this code only runs once, and it finishs if there is invalid input.

Maybe I should make a function for this :


Yes, functions are good for reusing code.

Previously we had

1
2

using std::cout;


I would rather have 20 using statements at the top of the code rather than 200 std:: everywhere, but that is my preference.

With your references, have you initialsed them with another variable? That should have been a compile error, unless i am missing something.

There are no checks on the input of the numbers, last time we had scanf which did check the input.

So have a go with these suggestions, get it working, but could I persuade you to have another go with the code / design that I suggested last time?

Now for something else entirely.

I use linux, just about to install fedora core 17. Linux is great, it comes with over 2000 apps and is entirely free, very secure (no virus protection needed). I use KDevelop with Qt for GUI apps, this is just as good as if not better than Visual Studio.

Linux is also great if you are a student, I remember being "poor as a church mouse" when I was a student. All you need is a spare hard drive to install it on, and you are away! If you have a PC you can get removable hard-drive bays, so that you can chop & change as much as you want. You can still have windows with a multiboot menu. Files can be saved in MS format .

There are various distributions, fedora, ubuntoo, debian, suse are some of the common ones. You can create a separate partition and test a different linux while still being able to boot your main linux. I am having fedora as my main linux and will have a go at testing ubuntoo on a spearate partition.

Any way have a think about this, could be really worthwhile for you.


Last edited on
I know we were using another code. The origin of the calculator was someone else's work. I had copied and pasted, because I didn't know how to write my own. Thought I would take a crack at it and hear your input.

I completely forgot, using an int is limited and will not give an accurate answer. doh! =D

I started writing std :: to become more familiar with what the std has inside and how it works.

I will look back at the code we were working on before.

Linux is great, unfortunately my laptop overheats when I use it. Plus it doesn't natively support a lot of games and wine is not 100%. I have been saving up this summer to get a ubuntu "approved" laptop. They are fairly cheap. I know dell and asus have some priced between $300-$500.

The compiler I use is code blocks. I cannot remember where but was reading different opinions on compilers and visual basic was not highly suggested.

I know it doesn't loop. This code is written in its own source. I will put the playAgain function in after I copy it into the "main" source, where the playAgain function is already.

I am just passing data (reference instead of the variable) to allow me to play with the variables within the function. This started with me altering the main source.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    char returnMenu;
    std :: string funcName;

    enterName(funcName);

    while (returnMenu == 'Y');
    {
        showMenu();
    }
        std :: cout << std :: endl;
        std :: cout << "\nGoodbye, " << funcName << std :: endl;


    return 0;
}


I wanted the name inputted by the user to be included when the user exited the program. The problem was that the user entered their name within the enterName function, therefor "name" was a local variable. I had to pass the reference to int main so it could be used.

You were right, this web sight has great information.
Last edited on
Why does getline not work for the enterText function? When I run the program it ignores it. Just displays:
Enter some text you would like to display.
Would you like to use again? (Y/N)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

void enterName(std :: string& name);
void showMenu();
void menuProcess(char menuOption);
void useCalculator();
void playGuessingGame();
void enterText();
bool wantToPlayAgain();

int main()
{
    char menuOption;
    std :: string funcName;

    enterName(funcName);

    while (menuOption != 'Q')
    {
        showMenu();
        std :: cin >> menuOption;
        menuOption = toupper(menuOption);
        menuProcess(menuOption);
    }
        std :: cout << std :: endl;
        std :: cout << "Goodbye, " << funcName << std :: endl;


    return 0;
}
void enterText()
{
    std :: string text;
    bool playAgain = true; // Program replay

    std :: cout << "\t\t\t\t|Enter text|" << std :: endl;
    std :: cout << std :: endl;

    while ( playAgain == true )
    {
        std :: cout << "Enter some text you would like to display." << std :: endl;
        std :: getline ( std :: cin, text );
        std :: cout << std :: endl;
        std :: cout << text << std :: endl;
        std :: cout << std :: endl;

        playAgain = wantToPlayAgain();
    }
}


getline works fine for the enterName function.

1
2
3
4
5
6
7
void enterName(std :: string& name)
{
    std :: cout << "Hello, enter your name: ";
    std :: getline ( std :: cin, name );
    std :: cout << std :: endl;
    std :: cout << "Hello, " << name << std :: endl;
}
Why does getline not work for the enterText function? When I run the program it ignores it.


Because of this line:
std :: cin >> menuOption;

What happens is when you type the menuOption in and then press enter, you're actually sending two characters to the cin buffer. The first one is caught by cin and is used to tell cin it is done. The second character is still sitting in the buffer though. I believe your next cin statement would ignore this by default? but allow you to enter another input. It doesn't work this way with the getline function, however.

When you still have that character sitting in the buffer, when you approach a getline, it reads that character as part of the input, since it's not specifically a cin call (getline can also handle filestreams i believe). There is two approaches to this:
1) replace that line with a getline function call.
2) add std::cin.ignore(80, '\n'); to ignore anything that is left over into the buffer (up to 80 characters).

Now you're probably wondering why or how the two characters are entered. I'm going to use what I know, and maybe add some things in there that may not be 100% correct, but should give you a better understanding.

On Windows systems, pressing the enter key actually sends two characters, the end line character, and the line feed character. With a standard cin, standard for a reason (you will see why shortly), it recognizes the end line character, discards it, and finishes it job. On most other systems, pressing the enter key sends only one character, the end line character. It is programmed into the OS that once there is an end line character, start a new line. This means that on all systems but Windows, only one character is sent to the cin. This is where the standard part plays a role. Since, by the standard, it accepts only one character, Windows being the odd ball, it discards that and continues your code. Since Windows is difficult, it adds that extra character into the buffer to mess things up later.

I hope that helps you understand, and if I'm wrong on this, please please tell me.
SGM3,

Well things are looking good ! I can see that you were thinking about the things I mentioned.

Another thing about linux is that because it is open source, you can download the source code for free. So you could get some source for some basic games to start with, and see how they work, change a few things - just another way to learn. There is still quite a bit to learn about C/C++ first, otherwise it will be hard to understand what's going on.

Here's some ideas about how I go about writing code:

I normally have some idea about methodology, but I check online, because someone has probably already done it - their idea might be better than mine and might involve an approach that I hadn't even thought of.

A while ago I had a vague & screwed up idea of what it meant to design classes, so I posted a question on a forum. Fortunately I received some great answers, and I learnt quite a bit. I don't normally post many questions on forums, because of the reasons below.

Doing a design like I have mentioned is good to organise my thoughts and get my methodolgy right before I write any code.

If I want to use some function or STL object, I always google it, to find out all about how it works. I notice a lot of newbies tend to tear into writing code, without any thought as to what a function does, returns etc. Mind you some times I am guilty of this too, unfortunately when I am trying to help someone and don't bother to check out a function they are using.

If there are compile problems, I can normaly sort these out myself, but I can see how it is a bit tricky to understand what it all means to start with.

If I have runtime problems, I use the debugger - normally I can fix problems with this. I don't find myself using the debugger much, possibly because I have done the design, and I am careful about naming, declaring & initialising variables & especially with pointers. These things are main reasons that people get errors.

Quite often newbies don't understand what it means to debug - I see comments like "My program fails to debug". This usually means they don't know how to use the debugger at all, or they don't realise that they should be watching the value of variables until they find out where they go wrong.

The debugger doesn't catch logical errors - an extreme case might be "Some apples plus some cars equals how many houses". A more normal example might be a failure to understand a maths concept like "calc the tangent trig function of 90 degrees". I am sure there a million other examples.

If I still have problems, then I ask a question on the forum. I am not saying that I am a genius coder, (that is so far from the truth it's not funny !), but I learn more if I figure it out myself. Some times it can take a long time to come up with a solution.

Any way that is today's news - so far.
Volatile Pulse,

I had moved cin.ignore around to correct the issue because I still had problems when I inserted the cin.ignore after the menuOption. I had to use trial and error because the program would either skip the enter text as it did before, run once with no problems until you replayed (the function looped) then it would not output the entered text, it would skip the enter text as before only when you replayed (loop), or you would have to press the enter key after you hit enter to display your text.

I also tried to replace cin >> with getline for the menuOption but had compiler errors. I may have to do something within the menuProcess function for that to work.

All in all, I got it to work. This is the only change from the original post.
It clears the buffer before use and if you replay.

1
2
3
4
5
6
7
8
9
10
11
while ( playAgain == true )
    {

        std :: cin.ignore(1000, '\n'); // The only ignore I put in.
        std :: cout << "Enter some text you would like to display.\n" << std :: endl;
        std :: getline ( std :: cin, text );
        std :: cout << std :: endl;
        std :: cout << text << std :: endl;

        playAgain = wantToPlayAgain();
    }


I am not sure why I had these problems once I cleared the buffer from menuOption. I was reading into getline and it should be a better way to get user input. Problems may be there because I am using two different methods for collecting input.
Last edited on
I am not sure why I had these problems once I cleared the buffer from menuOption. I was reading into getline and it should be a better way to get user input. Problems may be there because I am using two different methods for collecting input.

It's because your entire code wasn't pasted so I had assumed that the only cin statement that you had left was this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    char menuOption;
    std :: string funcName;

    enterName(funcName);

    while (menuOption != 'Q')
    {
        showMenu();
        std :: cin >> menuOption;
        menuOption = toupper(menuOption);
        menuProcess(menuOption);
    }

The reason the ignore needs to go after the cin and not after the get line is because the cin doesn't capture both return characters, but the ignore function will capture them, and up to 80 characters before them, per our example, and essentially remove them from the buffer. I believe this is why you had to put the ignore where you did. Try putting it after the line I underlined above. make sure it is after every cin statement or you might miss just one character in the buffer.
I did put an ignore below the cin you have underlined. When I went into the enterText function I could enter and display text once. But, when I replayed (activated the while loop) I could no longer enter text. It continued to skip the getline. From what I understand is that a function is like a box. Nothing can access it unless you set up arguments or call the function into play. While I have other functions that contain cin they shouldn't effect the enterText function. Especially when I run the program fresh and go directly to the enterText function.
The input buffer isn't contained in code, it's handled by the system. The reason you had issues even after putting it where I underlined is because you have other cin statements in your program. Just try putting an ignore statement after all of them and see if that works for you.
Last edited on
I will put ignore statements where the cins are. The program does what I want it to do with the cin here:


I am curious as to why I would have problems putting an ignore statement where you underlined. Considering I only pass through that underlined cin, which the buffer should have been cleared from the ignore statement.

A question about output. I know this is basic stuff and this forum has great resources. I am just curious since knowing about the buffer and how information lingers without an ignore statement. Would it be wise to use getline statements instead of cin?
Last edited on
TheIdeasMan,

Is there any major difference in c++ code for games on linux compared to windows?

The code should be relativity the same, maybe different ports?

With the open source of linux, where may I find game code? Google? Can I download a game for linux and open up the source?

I think that would be great to view functional game code to establish good file management and code structure.
It really depends. cin is very simple and easy to use and a lot of people have gotten away from using functions for input since that was the C way to do it. Now, however, getline is needed for strings, without learning another way to do it, that is. An easy way to remember to do it is how you originally did it, but put the cin.ignore before the getline. cin also has member functions like get and getline that work the same way as getline does. Do a little research and find out which way works best for you.

As for your most recent post, as long as you stick to standard libs, which I do believe are all header files that don't require a .h in the < >, it will work on other OS's. The biggest exception to this is the system() function in cstdlib. If you're looking to download game source code from another operating system, your best bet is to look for portable code. Simply put, the only thing you need to do is compile it and it'll work on windows. This doesn't include possibly having to download new libraries that might be required to compile the games though. If you're looking for windows games, it might just be better to google open source window games.
ok - I will look into open source windows game. I do enjoy using Linux over windows, but games made for windows gain more traction.

I am a long way from programming a functional game using a GUI but just keeping things in mind.

I was looking into I/O in c++ and came across a way to input code that I have never seen in any tutorial. Is this a more "professional" way to write while statements and clear the buffer?

1
2
3
4
5
6
7
while ((std :: cout << "Enter first number: " << std :: endl)
            && (std :: cin >> entNum1) || (entNum1 < 1))
    {
        std :: cout << "Cannot be calculated." << std :: endl;
        std :: cin.clear();
        std :: cin.ignore(std :: numeric_limits < std :: streamsize > :: max(), '\n');
    }


Maybe just a style?
It's all about preference and error proofing your code. I personally don't like putting cout/cin statements in the conditional part of the while, but I do believe that cin returns a non positive number if the value entered isn't of the data type of the variable (read: If you enter a string instead of an int like it wants, the return value of std::cin >> entNum1 is either 0 (false) or a negative number), so the loop will continue to run until you satisfy all conditions.

The std::cout statement should always be true (does anyone know of a time when it's not?) so as long as you enter an int (which is what I believe entNum1 is) and that int is 1 or greater, the loop will be ignored and you'll move on to the next segment of code. If any of those conditions aren't met, the code then tells you it can't be calculated, clears (I can't remember precisely what is cleared), then ignores characters until it meets an endline character.

I'll have to look into the actual use of those two lines to be able to explain it in full detail, but I believe this is already a little over your head anyways. If not, more power to you.
I am having a difficult time checking user input for my calculator. I am using doubles for the numbers. I thought I would just check user input in the enter number function. I may be going about it all wrong. The code works fine if numbers are inputted, but when I enter a letter (to test) the program skips through the loops an ends. I will post the whole code ( of the calculator)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void useCalculator()
{
    std :: cout << "\t\t\t\t|Calculator|" << std :: endl;
    std :: cout << std :: endl;

    double num1 = 0.0, num2 = 0.0, answer = 0.0;
    char operators;
    bool playAgain = true; // Program replay

    while ( playAgain == true )
    {
    std :: cout << "What operator would you like to use? ( +, -, *, /) ";
    std :: cin >> operators;
    std :: cin.clear();
    std :: cin.ignore(std :: numeric_limits < std :: streamsize > :: max(), '\n');
    std :: cout << std :: endl;

    switch (operators)
    {
        case '+':
            addition(num1, num2, answer);
            break;
        case '-':
            subtract(num1, num2, answer);
            break;
        case '*':
            multiply(num1, num2, answer);
            break;
        case '/':
            divide(num1, num2, answer);
            break;
        default:
            std :: cout << "Invalid input." << std :: endl;
            std :: cout << std :: endl;
            continue;
    }
        if (operators == '+' || operators == '-' || operators == '*' || operators == '/')
        {
            std :: cout << num1 << " " << operators << " " << num2 << " = " << answer << std :: endl;
            std :: cin.clear();
            std :: cin.ignore(std :: numeric_limits < std :: streamsize > :: max(), '\n');

        }
            playAgain = wantToPlayAgain();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
bool wantToPlayAgain()
{
    char Response = 'N';

    std :: cout << "\nWould you like to use again? (Y/N)  ";
    std :: cin >> Response ;
    Response = toupper(Response );
    std :: cout << std :: endl;

    switch (Response )
    {
        case 'Y':
        return true;
        break;
        default:
        return false;
        break;
    }
}
void addition (double& funcNum1, double& funcNum2, double& funcAnswer)
{
    enterNum (funcNum1, funcNum2);

    funcAnswer = funcNum1 + funcNum2;
}
void subtract (double& funcNum1, double& funcNum2, double& funcAnswer)
{
    enterNum (funcNum1, funcNum2);

    funcAnswer = funcNum1 - funcNum2;
}
void multiply (double& funcNum1, double& funcNum2, double& funcAnswer)
{
    enterNum (funcNum1, funcNum2);

    funcAnswer = funcNum1 * funcNum2;
}
void divide   (double& funcNum1, double& funcNum2, double& funcAnswer)
{
    enterNum (funcNum1, funcNum2);

    funcAnswer = funcNum1 / funcNum2;
}


This is the function I am having a problem with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void enterNum (double& entNum1, double& entNum2)
{
    while ((std :: cout << "Enter first number: " << std :: endl)
            && (std :: cin >> entNum1) && (entNum1 == 0))
    {
        std :: cout << "Cannot be calculated." << std :: endl;
        std :: cin.clear();
        std :: cin.ignore(std :: numeric_limits < std :: streamsize > :: max(), '\n');
    }

    while ((std :: cout << "Enter second number: " << std :: endl)
            && (std :: cin >> entNum2) && (entNum2 == 0))
    {
        std :: cout << "Cannot be calculated." << std :: endl;
        std :: cin.clear();
        std :: cin.ignore(std :: numeric_limits < std :: streamsize > :: max(), '\n');
    }
}
Pages: 1... 4567