In CLion getch() is waiting for pressing of Enter button in this code
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
char c = getch();
cout << c;
}
How can I fix it?
Related
In my program it should be an option to ask a user for input, and then save input string into the file. My problem is, - when I put cin in any of it forms, inside Switch, program will stuck circling indefinitely, right after i press enter after finish typing new text. What could cause the problem?
#include <iostream>
#include <fstream>
#include <iterator>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
using namespace std;
void changePlainText()
{
ofstream nFile("plaintext.txt");
string newText;
cout << "Enter new plain text" << endl;
getline(cin, newText);
nFile << newText;
nFile.close();
}
int main()
{
int uInput = 0;
do
{
printf("2.Change content of the plain text file: \n");
cin >> uInput;
switch (uInput)
{
case 1:
break;
case 2:
changePlainText();
break;
}
} while (uInput != 5);
cout << "Closing program" << endl;
system("pause");
}
After I type something in console and press enter, the program enters never ending circle. It still stuck even if I just write simple cin >> i, in switch case.
Take a look at this question. I debugged your code and I experienced that exact behavior. The accepted answer explains quite well what's happening and also provides a solution.
I tried with boost, I changed
cin >> uInput;
to
string inputString; // so you know what inputString is
getline(cin, inputString);
uInput = boost::lexical_cast<int>(line);
and it's working fine now.
I have this bit of code
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
this_thread::sleep_for(chrono::milliseconds(5000));
string s;
cin >> s;
return 0;
}
In the time the program was sleeping I pressed some keys. This is what i see after those 5 seconds have passed and the cin is encountered.
Any ideas on why this happens and how to fix it?
This is my code!
#include "stdafx.h"
#include<iostream>
#include<conio.h>
void main()
{
clrscr();
int no;
cout<<"Enter a number";
cin>>no;
getch();
}
and I get this error here!
I think I might have to download some extra visual studio c++ related directories, but still some suggestions please
clrscr() is not a standard function. Visual Studio does not have it. However, MSDN does document how to clear the screen using system("cls"), or FillConsoleOutputCharacter() and FillConsoleOutputAttribute().
As for the cin/cout errors, you need to prefix them with the std:: namespace qualifier, eg std::cin and std::cout, or use a separate using namespace std; statement in your code below the header #include statements.
Try this:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
std::cout << "Enter a number";
std::cin >> no;
getch();
return 0;
}
Or:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
cout << "Enter a number";
cin >> no;
getch();
return 0;
}
Right now your c++ program doesn't know what clrscr(); is..
you have to define that function. To define it, see #Remy Lebeau's answer.
One quick solution instead of creating a function to clear out the screen is to simply cout a bunch of blank spaces.
so in your main you can simply put :
std::cout << string(50, '\n');
Trying to run this program, but whenever it runs is quickly opens up and closes right away without allowing me to interact with it. What am I doing wrong to make it open and close?
Below is my program, and I'm sure I'm doing something wrong.
Thanks for your help!
Driver.cpp
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
char Anagram();
}
Anagram.cpp
#include<string>
#include<algorithm>
#include<iostream>
#include "Anagram.h"
using namespace std;
char Anagram()
{
string FirstAnagram, SecondAnagram;
char keep_going;
do
{
cout << "Enter word one: ";
cin >> FirstAnagram;
cout << "Enter word two: ";
cin >> SecondAnagram;
sort(FirstAnagram.begin(), FirstAnagram.end());
sort(SecondAnagram.begin(), SecondAnagram.end());
if (FirstAnagram == SecondAnagram)
{
cout << "They are anagrams of each other.";
}
else
{
cout << "They are not anagrams of each other.";
}
cout << "\n\nTry another?";
cin >> keep_going;
}
while (keep_going == 'y');
return 0;
}
Anagram.h
char Anagram();
Try this:
#include<string>
#include<algorithm>
#include<iostream>
#include "Anagram.h" // add this
using namespace std;
int main()
{
char ch = Anagram(); // call the function like this
}
Try adding this to the end of your main function:
std::cout << "Press Enter to close application.\n";
std::cin.ignore(10000, '\n');
At the end of my loop I have:
cout<<"\n\n any key to continue or Ctrl+Z to exit.";
It allows the user to continue entering data, or to exit by pressing CtrlZ. I would like to hide the pressed key when the user decides to continue entering data.
I don't want the pressed key to appear when the user press any key to stay in the loop. How can I do that? I am using Dev-C++. the code of my function is below.
void student::read()
{
char response; ofstream OS ("student.dat", ios::app);
do
{
cout<<"Name: ";
cin>>name;
cout<<"Age: ";
cin>>age;
cout<<"GPA: ";
cin>>GPA;
//calling writefile to write into the file student.dat
student::writefile();
cout<<"\n\n any key to continue or Ctrl+Z to exit."<<endl<<endl;
cin>>response;
cin.ignore();
}
while(cin); //Ctrl+Z to exit
}
there are multiple ways to handle this
but it depends on what operating system you are using
http://opengroup.org/onlinepubs/007908799/xcurses/curses.h.html
http://en.wikipedia.org/wiki/Conio.h
Option 1:
Windows using conio.h
getch()
or for *nix using curses.h
getch()
Option 2:
In Windows, you can turn off echo for any standard input function with SetConsoleMode().
Code:
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main()
{
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
string s;
getline(cin, s);
cout << s << endl;
return 0;
}//main
or *nix syle
#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>
using namespace std;
int main()
{
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
string s;
getline(cin, s);
cout << s << endl;
return 0;
}//main