:mrgreen: I've just started learning and doing examples out of the book but I wanted to see if I could actually make something besides just copying stuff out of a book. So I tried to make an addition calculator.
This isnt the entire source code but just a few snippets to make sure that im getting the procedure right.

#include <iostream>
using namespace std;

int main()
{
     cout << "Plase enter 2 numbers to be added ";
     int a;
     int b;
     cin >> a;
     cin >> b;
     cout << " a + b = " << a + b << endl;

     return 0;
}

Would this work?

If this goes well I would like to expand it with if statements by asking what function to use with cout having the user enter either a for + b for - c for * and d for /. Then assign a function that if variable "number" is = "(whatever operation sign given)" then do this.

Does this seem logicaly reasonable or have i got the entire procedure wrong on hwo to do it?

Recommended Answers

All 10 Replies

When I run the program It says "Please enter the 2 numbers that will be added" but when I try to enter a number and press enter it doesnt do anything it just keeps skipping lines. Why is it not working. I declared the variables. Asked the user to enter them. And had it calculate it.

When I run the program It says "Please enter the 2 numbers that will be added" but when I try to enter a number and press enter it doesnt do anything it just keeps skipping lines. Why is it not working. I declared the variables. Asked the user to enter them. And had it calculate it.

I did tried your code..its working fine

I ran it and it works ok. In response to the prompt you enter a, then a space, then b, then a carriage return. You can also respond a, carriage return, b, carriage return. Your comments indicate that you expect it to do something after you enter a. What it's doing is waiting for b.

Good Luck

well I ran the program didnt work. I recompiled it and ran it and it still didnt work. My compiler must be screwed up im using DEC C++ that game with "Beginning C++ Game Programming" by Michael Dawson.

EDIT: changed the source code again to figure out where the problem is. So now I can enter first number and it will display what num1 =. Now when I enter the second number it just exits. Now whats going wrong?

#include <iostream>
using namespace std;

int main()
{
     cout << "Please enter 2 numbers to be added "; 
     int a;
     int b;
     cin >> a;
     cout << "\n num1: " << a << endl;
     cin >> b;
     cout << " num2: " << b << endl;
     cout << " a + b = " << a + b << endl;
     
     cout << "Press the enter key to exit";
     cin.ignore(cin.rdbuf()->in_avail() + 1);
     return 0;
}

Sorry if I just forgot something really obvious.

actually it worked ok for me

What compiler are you guys using. Im using DEV C++compiler. For me it works it displys message to type in the 2 numbers. Type in first number press enter and it displays the first number = the variable. Then it ask for second number and you put the number in and it exits for some reason. Could this have to do with memory allocation and im going over the amount of memory I set aside for the answer?

Since it seems like this is just gonna go one for ever saying "it works for me" someone just lock this thread.

at first i thought that maybe the new line created by the return might make the second cin just read that, but then i threw it in vs.net 2003 and it worked fine, so i retracted that

not sure what his problem is

>Since it seems like this is just gonna go one for ever saying "it works for me" someone just lock this thread.
Maybe you should be more specific than "it doesn't work". It sounds like you're experiencing the first problem that most Dev-C++ users ask here, where the output window closes when the program terminates, and you don't see the complete output. Try this:

#include <iostream>
#include <limits>
using namespace std;

int main()
{
     cout << "Please enter 2 numbers to be added "; 
     int a;
     int b;
     cin >> a;
     cout << "\n num1: " << a << endl;
     cin >> b;
     cout << " num2: " << b << endl;
     cout << " a + b = " << a + b << endl;
     
     cout << "Press the enter key to exit";
     cin.ignore(numeric_limits<streamsize>::max(), '\n');
     cin.get();

     return 0;
}

>cin.ignore(cin.rdbuf()->in_avail() + 1);
This isn't strictly guaranteed to do what you think it's doing, for several somewhat advanced reasons that I'm not terribly interested in explaining right now. But you can treat it like cin.sync() and avoid it in favor of the above example most of the time.

Yeah I learned that in the book I was reading for beggining C++ and have been using it ever since.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.