I used to make basic program of C/C++ in vs code. After 2-3 weeks. in it's terminal it is not showing variable in output window. after making program when i run code.when i hit enter key it shows in terminal--it is not an error but code is not working.
PC C:\User\Owner\Desktop\C-C++>
WHEN I HIT ENTER IT AGAIN SHOW
PS C:\User\Owner\Desktop\C-C++>
IT DOES NOT SHOW 'ENTER VALUE OF B=='
IN THE TERMINAL OUTPUT WINDOW:-
for ex-:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "ENTER VALUE OF A==";
cin >> a;
cout << "ENTER VALUE OF B==";
cin >> b;
c = a + b;
cout << "ADDITION OF VALUES" << c;
return 0;
}
IN THE TERMINAL WINDOW IT SHOWS.
ENTER VALUE OF A==5 ( WHEN I PRESS ENTER )
THEN IT SHOWS
PS C:\User\Owner\Desktop\C-C++>5
5
AGAIN
PS C:\User\Owner\Desktop\C-C++>
PLEASE ANSWER THIS PROBLEM :(
Related
I'm trying to write a simple c++ program that takes a number of miles as input and returns a conversion to kilometers. Here is my kilo.cpp:
#include <iostream>
using namespace std;
int main()
{
double miles;
double km_to_mile = 1.609;
cout << "Please enter number of miles: ";
cin >> miles;
miles *= km_to_mile;
cout << "That is " << miles << " kilometers.";
}
The program compiles, but when I run it it crashes when it tries to output the double. The last line I get in console is "That is " and I get the Windows error message "kilo.exe has stopped working."
I've tried a few other code samples and whenever I try to use cout to output a double value, the program crashes with the same error. I assume this is some problem with my compiler (mingw on Windows 8.1), but I've tried reinstalling the compiler a few times now to no avail.
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();
}
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.
I'm new to c++ I'm trying to do a simple programme that calculates the weekly pay. See source code below. When I run the programme it lets me enter the workHours but when I click enter the programme closes n does not proceed to the rest of the code. Not sure where I'm going wrong please help.
#include <iostream>
using namespace std;
int main ()
{
int workDays;
float workHours, payRate, weeklyPay;
workDays = 5;
payRate = 38.55;
cout<< "Enter the number of hours worked: ";
cin >> workHours;
cin.ignore ();
weeklyPay = workDays * workHours * payRate;
cout << "Weekly Pay = ";
cout << weeklyPay;
cout << '\n';
return 0;
}
The DevC++ doesn't pause the program after it ends you have to add this feature by yourself:
First include conio.h
#include <conio.h>
Then at the bottom of the main add getch()
getch();
What compiler you are using as i am to run this code?
I am using visual studio 10 as IDE and it is working for me.
I have posted the relevant code below. When I compile the program, it runs and reaches the point where it waits for the input. I type in an integer and press ENTER, but the code never continues. How would I go about correcting this?
int i;
cout << "Please input column to sort by: ";
cin >> i;
Well, first of all, what you posted above won't compile. Try this instead:
#include <iostream>
int main(int argc, char *argv[]) {
int i;
std::cout << "Please input column to sort by: ";
std::cin >> i;
std::cout << "You entered: " << i << "\n";
return 0;
}
Compile it with g++ -O3 thefile.cpp, assuming the file is called "thefile.cpp".
If it doesn't work then there is a serious issue going on. If it does you should be able to diagnose your issue further.
If you use visual studio 2010 try this:
#include<iostream>
using namespace std;
int main(){
int i;
cout<<"Please input column to sort by: ";
cin>>i;
cout<<"Your input the number: "<<i<<"\n\n";
system("pause");
return 0;
}