Turbo c++ program exits on hit enter - c++

It succesfully runs without any error.
after enter first digit and hit enter key then it shows Enter second digit
The problem is here that After enter second digit and i hit enter it not showing result.
Instead of result it exit from the program window and enter to the window where i was programming.
#include <iostream.h>
#include <conio.h>
int main()
{ clrscr();
int value1, value2, sum ;
cout << "Enter First Digit : " ;
cin >> value1 ;
cout << "Enter Second Digit : " ;
cin >> value2 ;
sum = value1+value2 ;
cout << "The Sum is : " ;
cout << sum ;
return 0;
}

I think your program is outputting correctly, and then closing.
One option is to just ask for one more bit of input, but then discard it at the end:
cout << "Please Enter to quit";
int temp;
cin >> temp;
Another way would be to run your program in a command window - on Windows, you can run "cmd", navigate to the folder that contains your program, then type in the file name to run it.
Your IDE might also allow you to enable prompt-upon-finish.

Turbo C++ has an option to view to command prompt window. Click "Window" then "Output" in the menu.
Alternatively, add the line cin.get(); at the end of your program, just before the return statement.

system("pause");
Adding this code before 'return 0' will pause your program and give you time to look at the result.
If you don't like the "Press any key to continue" message it shows you can go for this:
cin.get();
However you have to include another library whenever you use it:
#include <conio.h>
Since you have used it already you don't have to include it anymore.
Warning: The second way may create you some problems in more complex programs.
I recommend you to use the 'system("pause");' if you aren't sure whether the 'cin.get();' will work or not.

Add getch(); before closing the last curly bracket. You won't exit from the output screen and you will be able to see the output.
Write like this:
#include <iostream.h>
#include <conio.h>
int main()
{ clrscr();
int value1, value2, sum ;
cout << "Enter First Digit : " ;
cin >> value1 ;
cout << "Enter Second Digit : " ;
cin >> value2 ;
sum = value1+value2 ;
cout << "The Sum is : " ;
cout << sum ;
return 0;
getch();
}

Related

The user can input many numbers as they like and will stop if they will enter zero then get the sum of all the numbers they have entered

#include <iostream>
using namespace std;
int main(){
int num=0;
int total=0;
cout<<"Enter many numbers as you like : "<<endl;
while (cin>>num){
if (num==0){
break;
cout<<"The sum is : " ;
total = total + num;
}
}
system("PAUSE");
return 0;
}
When I run this it runs and when I enter zero the program stops but it did not get the sum of the numbers entered by the user. please help me with this problem. thanks. :)
To print a number do:
cout<<"The sum is : " << total << endl;
You will want to place thebreak; statement as the last line of your conditional if(...){...} statement. When the program reaches the breakstatement, it will exit the while loop and proceed to the end of the program. Right now your loop is exited before cout has the new stream written to it and the total is updated.
Second, you want to put the value of total on the output stream:
cout << "The sum is : " << total << endl;
Currently, you are just printing out the string "The sum is :"
Also, system("PAUSE"); is a shell and OS dependent statement, and usually not seen as good coding practice. You may want to look into another way to accomplish this: http://www.dreamincode.net/forums/topic/30581-holding-the-execution-window-open/

Visual Studio command prompt closes despite that I tried to stop it with cin.get()

My following basic C++ code isn't working. When I run it in Visual Studio express, it just closes the command prompt after I enter the second number. Thanks in advance.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, answer;
cout << " Enter a number: ";
cin >> num1;
answer = num1 * num1;
cout << " the sum of the number is: " << answer << endl;
cout << "Enter a 2nd number";
cin >> num2;
answer = answer + num2;
cout << "The sum of the two numbers is: " << answer << endl;
cin.get();
return 0;
}
The problem is that there are characters left in the input buffer.
Anyway you shouldn't add "tricky" commands to keep the console open (you have to remember to remove them from the "production" code).
You can run your program Without debugger mode (CTRL + F5) and Visual Studio will keep the console application window open until you press a button (just check the settings in Project -> Properties -> Linker -> System -> Sub System -> Console (/SUBSYSTEM:CONSOLE)).
Of course, if you are debugging (F5), a breakpoint on the return 0; is the best option.
This is what is probably happening:
Last cin.get() reads the Enter key that you press after entering the second number. You need one more cin.get() so that the command prompt waits for you to press another key.

How to get two inputs from a same input (C++)

Title probably sounds confusing so first I'll show you my code, I made this simple program to get two input values and multiply them, and another thing, but that's not important, It works correctly:
#include <iostream>
using namespace std;
main()
{
int a,b,c,d,e;
char j = 4;
cout << "Welcome to Momentum Calculator\n\n";
cout << "------------------------------\n";
cout << "Please Enter Mass in KG (if the mass in in grams, put \"9999\" and hit enter): \n\n";
cin >> a;
if (a==9999) {
cout << "\nPlease Enter Mass in grams: \n\n";
cin >> d;
}
else {
d = 0;
}
cout << "\nPlease Enter Velocity \n\n";
cin >> e;
if (d == 0)
{
c = (a*e);
}
else {
c = (e*d)/100;
}
cout << "\nMomentum = " << c;
cin.get();
cin.ignore();
while (j == 4)
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
main();
}
}
Now as you can see, my variable is an int (integer) and my problem is If I enter an English letter (a-z) or anything that is not a number will cause it to repeat my program unlimited times at an unlimited speed. I want a string/char to see if my var "a" is a letter or anything but don't know how to. I can do it, however, I want user to input only one time in "a" and mine makes him to enter again. Please Help :)
There is a function called isalpha in ctype library, checks whether your variable is an alphabetic letter so you can do using isalpha function.
Will isdigit or isalpha from standard library help you?
P.S.
1KG contains 1000 grams, so you should divide by 1000, not by 100;
UPDATE:
Seems I understood your question...
You need cin.clear(); before cin.get() and cin.ignore().
Otherwise the these calls won't do anything, as cin is in an error state.
I think you can get a as an String, and see if it contains English letter or not, if it contains, again ask for the input ( you can do it in a while loop ). And when a correct input entered, parse it and find what is it's number.

do while loops can't have two cin statements?

I'm just following a simple c++ tutorial on do/while loops and i seem to have copied exactly what was written in the tutorial but i'm not yielding the same results. This is my code:
int main()
{
int c=0;
int i=0;
int str;
do
{
cout << "Enter a num: \n";
cin >> i;
c = c + i;
cout << "Do you wan't to enter another num? y/n: \n";
cin >> str;
} while (c < 15);
cout << "The sum of the numbers are: " << c << endl;
system("pause");
return (0);
}
Right now, after 1 iteration, the loop just runs without asking for my inputs again and only calculating the sum with my first initial input for i.
However if i remove the second pair of cout/cin statements, the program works fine..
can someone spot my error please? thank you!
After you read the string with your cin >> str;, there's still a new-line sitting in the input buffer. When you execute cin >> i; in the next iteration, it reads the newline as if you just pressed enter without entering a number, so it doesn't wait for you to enter anything.
The usual cure is to put something like cin.ignore(100, '\n'); after you read the string. The 100 is more or less arbitrary -- it just limits the number of characters it'll skip.
If you change
int str;
to
char str;
Your loop works as you seem to intend (tested in Visual Studio 2010).
Although, you should also probably check for str == 'n', since they told you that they were done.
...and only calculating the sum with my first initial input for i...
This is an expected behavior, because you are just reading the str and not using it. If you enter i >= 15 then loop must break, otherwise continues.
I think you wanted this thing
In this case total sum c will be less than 15 and continue to sum if user inputs y.
#include<iostream>
using namespace std;
int main()
{
int c=0;
int i=0;
char str;
do
{
cout << "Enter a num: \n";
cin >> i;
c = c + i;
cout << "Do you wan't to enter another num? y/n: \n";
cin >> str;
} while (c < 15 && str=='y');
cout << "The sum of the numbers are: " << c << endl;
return 0;
}

C++ Noobie - Why does moving these lines break my application?

This is my first attempt at C++, following an example to calculate a tip through a console application. The full (working code) is shown below:
// Week1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Declare variables
double totalBill = 0.0;
double liquour = 0.0;
double tipPercentage = 0.0;
double totalNoLiquour = 0.0;
double tip = 0.0;
string hadLiquour;
// Capture inputs
cout << "Did you drink any booze? (Yes or No)\t";
getline(cin, hadLiquour, '\n');
if(hadLiquour == "Yes") {
cout << "Please enter you booze bill\t";
cin >> liquour;
}
cout << "Please enter your total bill\t";
cin >> totalBill;
cout << "Enter the tip percentage (in decimal form)\t";
cin >> tipPercentage;
// Process inputs
totalNoLiquour = totalBill - liquour;
tip = totalNoLiquour * tipPercentage;
// Output
cout << "Tip: " << (char)156 << tip << endl;
system("pause");
return 0;
}
This works fine. However, I want to move:
cout << "Please enter your total bill\t";
cin >> totalBill;
to be the first line under:
// Capture inputs
But when I do the application breaks (it compiles, but just ignores the if statement and then prints both cout's at once.
Im scratching my head becuase I cant understand what's going on - but I'm assuming I'm being an idiot!
Thanks
Try this
// Capture inputs
cout << "Please enter your total bill\t";
cin >> totalBill;
cin.clear();
cin.sync();
See c++ getline() isn't waiting for input from console when called multiple times
Or, better yet don't use getline at all:
cout << "Please enter your total bill\t";
cin >> totalBill;
cout << "Did you drink any booze? (Yes or No)\t";
cin >> hadLiquour;
totalBill is a number, i.e. the program "consumes" everything from your input that is a number. Let's say you entered:
42.2[RETURN]
The 42.2 gets copied into totalBill. The [RETURN] doesn't match, and remains in the input buffer.
Now, when you call getline(), the [RETURN] is still sitting there... I am sure you can figure out the rest from there.
Cin doesn't remove the newline character from the stream or do type-checking. So using cin>>var; and following it up with another cin >> stringtype; or getline(); will receive empty inputs. It's best practice to NOT MIX the different types of input methods from cin.
[for more informations see link]
you may change your code as below :
cout << "Please enter your total bill\t";
getline(cin, hadLiquour); // i used the hadLiquour string var as a temp var
// so don't be confused
stringstream myStream(hadLiquour);
myStream >> totalBill;