Codelite build presenting only the last row of output - c++

Using CodeLite c++14, mingw-32. I'm fairly new and learning from a course to program in the languge, but i've encounterd a problem in the last couple of days i couldn't figure out. For some reason, the command line prompt shows only the last row of my output code. this happens in all types of code that i've done so far.
Something that might be connected is that sometimes on the first building & executing of code (after a clean) i'm getting an error:
mingw32-make.exe: *** [All] Error 2
It's just an inconvinice though, a second build is solving this problem for some reason. But it might be connected some how.
Any way here's an example for a code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
char vowels[] {'a','e','i','o','u'};
cout << "\nThe first vowel is " << vowels[0] << endl;
cout << "\nThe last vowel is " << vowels[4] << endl;
return 0;
}
The output that shows is only "The last vowel is u", it goes to basiclly any code i create.
Thanks!

So I found an answer, and this probably won't help alot of people because its pretty absurd. apperantly i had a mining virus on the computer which also made it so at startup only a CMD window would appear and I had to type 'explorer.exe' manually.
So mingw basiclly tried to tell me something was messing with my CMD every time i opened it. What a way to find out.
A fix for those who face the same (found on reddit #spiralspectra):
1.a) Open Run (windows key + r) and enter "%appdata%" without quotes and press enter.
1.b) Go to the Microsoft folder and delete the SoundMixer folder contained within (This is the mining software).
1.c) Open Run and enter "regedit.exe" without quotes.
1.d) Find the registry entry mentioned as([HKEY_CURRENT_USER\Software\Microsoft\Command Processor]) and remove it. This entry attempts to run the miner whenever a CMD is started.
2.a) Go to the winlogon registry entry in the same regedit software ([HKEY_CURRENT_USER\Software\Microsoft\WindowsNT\CurrentVersion\Winlogon])
2.b) Check the sub-key named "shell" and change its value to "explorer.exe" without the quotes. (This means when you log onto windows it will no longer attempt to run the miner and instead start up normally.)

Related

GDB Skips While Loop Condition When Used With File Input

Alright Stack Overflow, I am running into a decently persistent problem in my C++ code. I'm sure this is one of those dumb mistake moments, but I have tried everything and cannot seem to squish this bug.
I have a bit of code here, and it's behavior is very odd. I have a main function that opens a file containing text I want to read in. I was taught in programming fundamentals class at my university that I could use getline() as a condition for the while loop, which is nice since it automatically terminates when it reaches the end of the file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream input_mem_traces("gcc.txt");
string trace_to_parse = "";
while(getline(input_mem_traces, trace_to_parse))
{
cout << trace_to_parse << endl;
}
}
When I compile and run it, it works just fine. It reads out every single line of the file I pass it, and returns with no problems.
However, when I try to use gdb, and set a breakpoint at the line
cout << trace_to_parse << endl;
it didn't hit the breakpoint. Curious as to why that was, I broke above the loop, and tried single stepping through the code. When I got to the while loop, and tried to step, it simply skipped to the line after it, which happened to be the end of the program.
This behavior occurs both using the VSCode GUI front end for gdb, as well as straight gdb from the command line. I am running this on Windows using Ubuntu under WSL2, and VSCode as my IDE with the Remote - WSL extension enabled.
Turns out there was some weirdness going on with the working directory with GDB. For some reason, my working directory was changing under the VSCode GUI, thus the file was not able to be opened, and the while loop condition performed as expected for that circumstance by not entering the loop. Upon the recommendation by Retired Ninja in the comments, I used an absolute path in the fstream object constructor, and that solved the issue.

White space on the end of output string not printing with the string, but rather with the next printed line after it

I tried to print a line that asks for input from the user, get the input, then print again some line. The problem is that the white space at the end of the first printed line is printed not at the end of the line, but rather at the beginning of second printed line, after i get the input.
I'm completely new to C++ so I couldn't really try much, but i tried printing the code without the part that prompts the input from the user, and it prints the space just fine, but when i add std::cin << input; the space get's sent to the beginning of the second line.
My code:
int input;
std::cout << "Enter your favorite number between 1 and 100: ";
std::cin >> input;
std::cout << "Amazing... That's my favorite number too... wow..." << std::endl;
I want the output to be
Enter your favorite number between 1 and 100: //some input
Amazing... That's my favorite number too... wow...
(note the space before //some input)
Instead i get
Enter your favorite number between 1 and 100://some input
Amazing... That's my favorite number too... wow...
(note the space before Amazing)
Edit: I'm using Clion if it could be connected. Also, I tried to run the executable on powershell and it worked as expected, without the problem, so this has something to do with the Clion terminal. Also, i'm using windows 10 as my OS.
Second Edit: add my findings on my answer.
This seems to be a problem with buffered input of Clion. See this issue: https://youtrack.jetbrains.com/issue/CPP-7437
When you are using CLion, You can try disabling PTY (Help | Find Action > type "Registry" > open Registry > find and disable the run.processes.with.pty option)
CLion moving space into new line
Are you sure about that? I tried both in online shell and on local machine and it works as expected.
After checking I found out that this occurs only on the Clion Run terminal, so this has something to do with it exclusively. I'm currently trying to mess around with the settings. I will post a solution and an explanation here if I find it.
Edit: as mentioned in one comment, it could be the issue mentioned here https://youtrack.jetbrains.com/issue/CPP-7437.
In any case it is a Clion related problem exclusively, and not a C++ problem.
This problem is only applicable for the second line, if you leave that line empty then the problem will be fixed. Don't know about C++ but for C before the print statement you have to add: printf("\n");

sublime text doesn't proceed my code after cin. what should I do?

all
I'm pretty new to programming and I'm currently teaching myself C++ with sublime text editor. I'm having a problem where the code does not proceed after I input something through cin. For example,
#include <iostream>
using namespace std;
int main() {
string password = "password";
cout << "Enter your password: " << flush;
string input;
cin >> input;
if(input == password) {
cout << "Password accepted." << endl;
} else {
cout << "Access denied." << endl;
}
return 0;
}
After I put my input, it doesn't cout anything such as "password accepted" or "access denied". Does anyone know how to solve this problem and make it work? Thanks.
As mentioned your code does work as expected.
The issue is when you build a c++ program in sublime text it will compile that file and then run. What you see is sublime text piping the output from your program back to a window within sublime text.
Sublime does not have the ability to send input back to your program. Hence why your program "does not proceed after I input something through cin". There are some plugins available on the linux version of sublime that give access to a full terminal emulator, I have not tried using one of these but they do exist.
What I would recommend is that you learn how to use the gnu tool chain using the command line.
This is an old post but since I faced the same problem and this was the first page to pop up when I searched for it, I'll post the solution I think best for other newbros (Does this term work outside of Eve online? Who knows...).
Do remember I am new as well and what I am saying is probably the wrong approach and bad for you, but it will get you started....
Option 1: Use Sublime text to edit and run the file on terminal as everyone suggests. After the first few times it gets extremely repetitive and annoying. As long as you know how to do it, you should be fine. NOT RECOMMENDED.
Option 2: Use VS Code or other IDEs. The problem with this one is that sublime text is just faster to start which is primarily why I like subl. In all honesty VS code is also fast and responsive and I think switching to it is not at all a bad idea.
Option 3: If you still wanna continue using Sublime text. Set it up for something they call competitive programming. Skip the 'bits/stdc++.h' part and do everything else. Follow this link: https://www.geeksforgeeks.org/setting-up-sublime-text-for-cpp-competitive-programming-environment/
In the rare event this webpage stops existing in the future:
Step 1: Have mingw/mingw64 compiler installed and added to path. (You will find plenty of resources that will help you with that).
Step 2:
(i) Open Sublime Text editor and then go to Tools > Build System > New Build System.
(ii) Paste the following code in the file and save it.
{
"cmd": ["g++.exe", "-std=c++17", "${file}",
"-o", "${file_base_name}.exe",
"&&", "${file_base_name}.exe<inputf.in>outputf.out"],
"shell":true,
"working_dir":"$file_path",
"selector":"source.cpp"
}
(iii) Name the file as “CP.sublime-build“ (or anything really, just name something you know so that you can find it from the Tools > Build system list. Also make sure to keep the '.sublime-build' part.)
Step 3: Create these three new files and save them in the same folder. (Keep it somewhere where you store your codes or something like that).
file.cpp: The file for writing the code.
inputf.in: The file where we will be giving the input.
outputf.out: The file where the output will be displayed.
//Also don't change the names of these files since this is part of the code we had earlier.
Step 4: Setting up Window layout:
-Select View > Layout > Columns : 3. This will create three columns in the workspace. Move the three files into three columns.
-Select View > Groups > Max Columns : 2.
-Select the build system we just created from Tools > Build System > CP (or whatever you named it).
This will create a live environment for you write and run/test code at the same time in subl. o7
One way to do this in Submlime is to create a new Build System.
Goto Tools > Build System > New Build System..
Paste something like below into the open file and save it.
{
"cmd": ["bash", "-c", "/usr/bin/g++ '${file}' -o '${file_path}/${file_base_name}' && open -a Terminal.app '${file_path}/${file_base_name}'"]
}
*This is for Mac OSX, you will have the change it for your environment.
Change Build System to saved name
When you build next time, there will be a new Terminal App window waiting for the cin input.

C++ , won't display last line of code

Shouldn't this work? I mean, the code is merely a test and is meant so that the dialogue goes this way : What is your name? name here, Hello name here, and yet it does not show the last line of Hello after I type in my name and click enter it just dissapears. Here is the code.
#include <iostream>
#include <string>
int main (void)
{
using std::cin;
using std::cout;
using std::string;
string name = "";
cout << "What is your name, pls?\n";
cin >> name;
cout << "\nHello " << name.c_str() << "\n";
return 0;
}
My guess is that you are running from the debugger, or double clicking the executable. In either of those cases, when the program ends, the console will close. So, the program produced output, but you just could not see it before the console closed.
Run the program from a pre-existing console so that the console remains after your program ends. Or, just whilst debugging, arrange that your program does not terminate immediately after emitting its final output. A simple way to do that is to place a break point at the end of the program.
It probably showed it right before it disappeared. If you're going to write console programs, and if you're going to send output to a console, you should run them from a console so the output has some place to go.
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Make sure you put a breakpoint before main goes out of scope. I guess your console disappears under VS?
Also, you don't need to extract the char* in the last cout statement:
cout << "\nHello " << name << endl;
Open a terminal (or a command prompt window).
Navigate to the folder that contains the executable.
Run it.
It's not disappearing. It is just running really fast.
Every IDE has a keyboard shortcut that allows you to run code and pause after the execution has finished.
This keyboard shortcut is Ctrl-F5 in Visual Studio.
I have no idea what IDE you're running, but that is your basic problem.
The other thing you can do is to test your code in ideone : ideone.com/hb4Cel (it's the same code. There is no point pasting it here)
A dirty workaround is to add something like this
cin >> name;
at the end, just before return 0;. It forces the window to wait for input (i.e. hitting return) before returning (which closes the program).
This isn't necessarily good design, but if all you want to do is run some tests then it'll do the trick.
Basically when you enter your name it displays your last line and exits after return 0.
Here are the following things to avoid that
1- use command line to run the application
Start->accessories->command prompt
Go to folder in which your application is using cd command
c:>cd c:\path\foldername
Now run the application by typing the program name e.g
c:\path\foldername>my_application.exe
It will display your last line.
2- Now if your are using microsoft visual c++ press ctrl+F5 to run your program
3- This is not recommended but you an use it as long as your are debugging then remove it from the code afterwards. Include conio.h header file and add getch(); line before return statement. It would hold the screen for you till you press a key.

How do I input data using the eclipse console? (c++)

I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.