"logout" word on every program - c++

When I write every C++ program, such as that one:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cout << "Tell me where to start: ";
cin >> n;
while (n>0) {
cout << n << "\n";
n = n-1;
}
cout << "FIRE!";
return 0;
}
I compile it with G++ and, when I run it, it works well, but when it finishes it displays the "logout" word after the program's last word, like this:
Tell me where to start: 10
10
9
8
7
6
5
4
3
2
1
FIRE!logout
[Process completed]
Why? And how can I remove it?

It's not from your program. It's because the terminal is opened with the sole purpose of running your program, and as such when it exits, the terminal shuts down.
If you open a shell and manually run your executable, instead of this message, you'll simply get another command prompt.

Related

Why c++ exe files closes immeditely? [duplicate]

This question already has answers here:
What is the Best Practice for Combating the Console Closing Issue?
(11 answers)
Closed last month.
I have 1 simple code in c++. That code calculate sum of 2 entered numbers. I convert that code to .exe for running windows. My problem is it will work but it doesn't show sum. After entering second number program closes immediately. I have no idea why this happen.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please enter first number \n";
cin >> a;
cout << "Please enter second number \n";
cin >> b;
int z = a + b;
cout << "Sum of these numbers are: " << z;
}
I find solution via command prompt. But I want to completely run my .exe file without command prompt
Closing the console after program finishes is a normal behaviour when you don't start it from the terminal. You could delay the closing of console by using
std::cin.get(). The function waits until recieving input from terminal, so when you press any key, the program will end and console will close

Why splitting a line separated by spaces with stream in C++ runs infinitely?

Before, when I was writing C++, I often used getch() for validation. However, now I am turning into competitive programming, I cannot use getch(); I had to use cin or getline. Thus, today, I replicated an instance of splitting a string using stringstream:
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string line;
getline(cin, line);
cout << line << endl;
stringstream reader;
reader.clear();
reader.str(line);
vector<long long int> list;
while (true) {
reader.clear();
if (reader.str().size() == 0) {
break;
}
long long int value;
reader >> value;
list.push_back(value);
}
}
Then, I ran the program, typed 1 2 3 4 5 6 7 8 9 and pressed enter. The program successfully displayed line, 1 2 3 4 5 6 7 8 9, but the program just didn't terminate. It never approached the end of the main() function. I tried to print reader.str(), but it just print 1 2 3 4 5 6 7 8 9 continually. I was confused. Why reader >> value didn't run and did not read any value? Why was the program running an infinite loop? How can I fix the issue? I appreciate any solution.
The reading operator >> does not change the underlying string. It uses an inner position of a next char to read.
int main() {
int n;
std::istringstream in; // could also use in("1 2")
in.str("1 2");
in >> n;
std::cout << "after reading the first int from \"1 2\", the int is "
<< n << ", str() = \"" << in.str() << "\"\n";
}
Outputs
after reading the first int from "1 2", the int is 1, str() = "1 2"
The loop may be terminated by replacing reader >> value; with
if (!(reader >> value))
break;
... but the program just didn't terminate.
This means that ...
while (true)
true is never false, or ...
reader.str().size() == 0
is never true.
true, in fact, is never false, and you never change the size of the reader's string object. Why would you expect this loop to break?
Please edit your question to make it more clear what you are trying to accomplish.

Why does the following program give different output in C++?

Following program prints 1 2 3 4 5 at once. That means there is no time delay in printing the output.
#include<iostream>
#include<stdio.h>
#include <thread>
using namespace std;
int main()
{
for(int i = 1; i <= 5; ++i)
{
cout << i << " ";
// Function to sleep the thread
this_thread::sleep_for(500ms);
}
return 0;
}
But this program prints
1
2
3
4
5
one by one that means I’m getting the output with 0.5 sec time delay.
#include<iostream>
#include<stdio.h>
#include <thread>
using namespace std;
int main()
{
for(int i = 1; i <= 5; ++i)
{
cout << i << "\n";
// Function to sleep thread
// for 0.5 sec
this_thread::sleep_for(500ms);
}
return 0;
}
What is literally happening in both of above programs?
Note:You can't see the difference of both the outputs in online compiler because they show the result after termination of program.
The output may be line buffered, in which case only complete lines are sent to the underlying output device.
I'm able to get the desired output, one element at a time appears with 0.5 sec of delay. Used visual studio 2015.
I suggest you to avoid online compilers.
They generally give the complete output once in a text box.
"\n" makes the output go to next line. It is newline character

Second cin is either skipped or does not work properly

I have a couple of problems, that I think are closely connected, but I couldn't get them fixed following what I previously found on the website.
My problems are related to the double use of cin in my main function. I need to read numbers from keyboard in order to either build small vectors or store single coefficients. I cannot know in advance the length of the vectors that I am going to build.
Here are the lines involved:
#include <vector>
#include <iostream>
#include <limits>
int main() {
...
double a=0;
std::vector<double> coefficients;
while (std::cin>>a) {
coefficients.push_back(a);
}
...
std::vector<double> interval;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
while(std::cin>>a) {
interval.push_back(a);
}
std::cout<<interval.size()<<std::endl;
std::cout<<*interval.cbegin()<<" "<<*(interval.cend()-1)<<std::endl;
...
}
I am using both macOS with g++ 6.3.0 and Linux with g++ 5.3.0. The flags I send to the compiler are -Wall -std=c++14 -o.
On the macOS machine the second cin is completely skipped, while on the Linux one the second reading process does not behave like it is expected to. I mean that if i give -1 1 at the second cin, the printed vector size is 0 and, obviously, the program stops because of a segmentation fault.
At each cin I enter the requested numbers in a single line, like 1 0 0 1, then press enter and then ctrl+D.
Thanks in advance to all! :)
Your call of std::cin.ignore(...) set the fail bit of the stream. This makes it impossible to enter the loop. You need to move the std::cin.clear() call right before the loop, in order to make it run. Also you have an out-of-bound read when there are no data in the second container.
#include <vector>
#include <iostream>
#include <limits>
#include <string>
int main() {
double a=0;
std::vector<double> coefficients;
while (std::cin>>a) {
coefficients.push_back(a);
}
std::cout << coefficients.size() << '\n';
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'X');
std::cin.clear();
char c;
std::cin>>c;
if(c != 'X')
{
std::cerr << "Invalid separator\n";
return 1;
}
std::vector<double> interval;
while(std::cin >> a) {
interval.push_back(a);
}
std::cout<< interval.size()<<std::endl;
if(interval.size())
std::cout<<*interval.cbegin()<<" "<<*(interval.cend()-1)<<std::endl;
return 0;
}
With the following data file,
$ cat data.txt
12 23
42
X
1 2
3 4 5
this output is generated:
$ ./a.out < data
3
5
1 5
You need to add a linefeed '\n' as a second parameter to cin.ignore() so it kills the ignore on an enter press

How can I make a second cout without destroying my first in this program?

#include "stdafx.h"
#include "iostream"
#include "thread"
#include "conio.h"
#include "windows.h"
using namespace std;
void incrm();
void charget();
void main() {
thread count(incrm);
thread getcin(charget);
count.join();
getcin.join();
cin.get();
}
void incrm() {
int j = 0; // used to increment and output
while (true) {
cout << "\r" << j; // outputs 1,2,3,4... and so on
j++;
Sleep(150);
}
cout << endl;
}
void charget() {
while (true) {
int i = getch(); // gets value of char
cout << "\r\nCHAR: " << i; // and here is the problem...!
}
}
So I wanted this program to output a number in the first line, which increments without stopping and if you hit any key it should cout the value of that key in a secound line, so i wanted it to output something like this->
45
CHAR: 97
and after you have hit a key the incrementing number should stay in the first line. If you hit several keys the second cout should be overwritten, but this doesnt seem to work for me, my output looks like this if i hit several keys->
10
12AR: 97
20AR: 96
My problem is that my first cout (the incrementing number) overwrites my second (or my second my first I don't really know) and then this countinues for every line! :(
I suggest you to use windows function called gotoxy(). You can apply this using
SetConsoleCursorPosition();
this function is availible in windows.h library.
It is possible to read more about it here:
Link