I want a, to be read until user presses Ctrl+Z. But it does nothing. My guess is that it's visual studios fault, because when I press Ctrl+Z it shows ^Z in console. What should I do? here is my code :
#include <iostream>
using namespace std;
int main()
{
char a[50], b[20];
int n;
while (1)
{
if (NULL == fgets(a, 50, stdin))
break;
cin >> b;
cin >> n;
}
return 0;
}
End of file in an interactive terminal does not depend on the language nor on the OS but on... the terminal and the configuration of the command interpretor.
If you use a true console on Windows running the good old cmd.exe, then Ctrl Z will be seen as an end of file. If you use an IDE, anything is possible and you should try also Ctrl D because some IDE prefere to provide a Unix compatible environment. After all Ctrl Z was the end of file on CP/M (ancestor of MS/DOS) in the 70'...
I tried Enter then Ctrl Z then Enter then next ... and it worked
Related
I wrote c++ code in the codeblocks IDE. I use EOF but don't no which keyword i will use to exit form my code.
Here is my code:
int main() {
int a, b;
while ( scanf("%d%d",&a,&b)!=EOF) {
printf ( "%d\n", a^b);
}
}
The "scanf" return value is EOF for an error, or if the end-of-file character or the end-of-string character is found in the first attempt to read a character.
You can use "Ctrl + Z" in windows or "ctrl +D" for linux.
If you are using windows CTRL + C is the code for EOF() as Enter/Return key would add a newline only
I just start to using Clion as my IDE and I'm struggling with EOF inputed from keyboard.
If I execute this simple c++ code
#include <iostream>
int main(){
int sum = 0, value = 0;
//read until EOF, calculating a running total of all values read
while(std::cin >> value){
sum += value;
}
std::cout << "Sum is " << sum << std::endl;
return 0;
}
In the Run window of Clion, after I input from keyboard
1 2 cmd+d I get this
1 2 ^D
Process finished with exit code 0
In MacOS the EOF(with keyboard) is Cmd+D.
In CLion is in settable in:
File -> Preferences -> KeyMap -> search EOF and set Cmd+D.
However, I suggest you to use in your code a Terminating Character such as zero that in this case doesn't change the result.
Example of execution in CLion
This is some known issue with CLion on Windows. Disabling the run.processes.with.pty in Registry (open via Find Action) usually helps.
In some environments the EOF emulation is performed using Ctrl+Z combination not Ctrl+D
To avoid such problems - create file with contents you want to enter "1 2"
and run your program from terminal redirecting the standard input to read from file. In this case the EOF event will always happen at the end of the input.
$./test < myinputfile.txt
Ctrl+Z used to work well when I wanted to terminate the input in a C++ program on a Windows PC. Now I was writing code on a Mac and I used ctrl+D to simulate the EOF signal, but it behaved weirdly. The input did stop but the program seemed not to continue. The program seemed to have stopped all at once when I pressed ctrl+D. I was writing a simple union find set if that helps.
#include <iostream>
#include "UnionFindSet.h"
using namespace std;
int main() {
int n;
int x,y;
std::cin>> n;
UnionFindSet UFSet(n);
while(std::cin>> x >>y)
{
UFSet.makeUnion(x,y);
}
std::cout<< UFSet.getConnectedPartNumber()<<std::endl;
return 0;
}
On a Mac CLion maps Ctrl-D to Command-D
Command-D sends an EOF.
This is configurable under Preferences --> Keymap
I wrote a simple C++ code. When I click "Run" on Eclipse, first a Windows command prompt appears with my program. After executing it, Eclipse console starts to work. Is there a way to deactivate command prompt and just use Eclipse console?
#include <iostream>
int main(){
int x = 0,y = 0;
std::cout <<"type2 numbers and I will make a magic trick."<< std::endl;
std::cin >> x >> y;
std::cout<<"Ta daa "<<x<<" and "<<y<<" equals to "<<x+y<<std::endl;
return 0;
}
When I erase std:cin part, command prompt appears and dissappears less than a second so I guess everytime I run my code, this command prompt will appear.
I wrote program in C , but after compiling and running it the output console does not stay on after I enter anything. My program requires to enter distance and time.
My code is here:
#include <stdio.h>
int spd(int x , int y);
int main() {
int x,y;
printf("enter the distance first then time in their SI units :\n");
scanf("%d",&x);
scanf("%d",&y);
printf("the speed required is ",spd(x,y));
getch();
return 0;
}
int spd(int x , int y) {
return x/y;
}
It seems that the getch feeds on your input-buffer. When you enter your value in scanf(), the '\n' remains.
Try to put flush(stdin) after your scanf() or for now put getch () twice.
If you want to run a console program, you should run it from a console. You have written a console program -- run it from a console.
Also, you need to either flush standard out or write a newline before the getch call. Otherwise, you're waiting for a keypress before you've actually written anything.
Try including this header file : -
#include conio.h