| | |
Trying to create
Thread Solved
![]() |
: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.
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?
This isnt the entire source code but just a few snippets to make sure that im getting the procedure right.
C++ Syntax (Toggle Plain Text)
#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?
•
•
•
•
Originally Posted by Niklas
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.
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?
Sorry if I just forgot something really obvious.
C++ Syntax (Toggle Plain Text)
#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.
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.
Since it seems like this is just gonna go one for ever saying "it works for me" someone just lock this thread.
>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:
>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.
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:
C++ Syntax (Toggle Plain Text)
#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; }
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- Create a Password Reset Disk (Windows tips 'n' tweaks)
- Create a Desktop Shortcut to Lock Your Computer (Windows tips 'n' tweaks)
- so in order to create an aim bot... (*nix Software)
- Create an Access Database using Java (Java)
- Create Your Own Restore Points (Windows tips 'n' tweaks)
- Create a Keyboard Shortcut to Open a Folder (Windows tips 'n' tweaks)
- Create a Personal Screen Saver (Windows tips 'n' tweaks)
- Create Internet Explorer CD?? (Web Browsers)
- Create a Desktop Shortcut that Locks a Windows XP-Based Workstation (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: Program keeps crashing...
- Next Thread: Multidimesional Arrays in C
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






