cout behaving unexpectedly for specific strings - c++

This question is an extension to this one.
Eclipse Debug run and console run of the same program give different outputs
To give a bit of background...
While parsing the output of the NTP application I discovered something rather odd in my string outputs. The environment that I am using is eclipse with CDT in Debug mode, redirecting console output to a terminal (/dev/pts/0) with fish running as shell.
Where is what I found...
Strings with starting characters or charachter within like *, +, &, ^. such as when I try to cout like this.
cout << "+123" << endl; //does not get printed
cout << "*123" << endl; //does not get printed
cout << "&123" << endl; //Concatenates with string below (without the line feed)
cout << "|123" << endl; //^
cout << "^123" << endl; //Does not get printed
Also
cout << "abcdef" << endl << endl;
does not have the desired effect of two linefeeds; only one gets printed.
Whereas when I run the progam through the terminal...
~$ ./Project/Debug/Project
Everything is normal and works as expected.
Is this expected behaviour, something I am not aware of?
Or is this a bug, corruption or something if such nature?
Or am I doing something wrong?
P.S: I might have left some details out by accident, please read the question in the link provided about to catch up.

Related

ShellExecute - ERROR code 5

I am using Notepad++ with TDM-GCC. My computer is 2Gb RAM Windows 10 32 bit 3.30 GHz. When I execute my simple program, it shows error.
Access is denied.
An attempt was made to execute the below command.
Command: D:\Deane\Github\CPP_Projects\AnalysisName\main.bat
Arguments:
Error Code: 5
Image of the error
I follow this: ShellExecuteEx function always returning error code 5 (C++)
Program's code (if necessary):
/* AnalysisName Program - written by Vo Tran Nha Linh */
#include <iostream> // Input and Output library.
using namespace std;
int main()
{
string name;
cout << "Hello friend! It's nice to meet you, what is your name?" << endl; // Ask the name.
cin >> name; // Input name.
cout << "Hello " << name << ". Your name is interesting." << endl; // Have a greeting.
cout << "Your name has " << name.length() << "letters." << endl; // Show the name's length.
cout << "It starts with " << name.front() << "letter." << endl; // Show the first letter of the name.
cout << "It ends with " << name.back() << "letter." << endl; // Show the last letter of the name.
return 0;
}
But it doesn't active, please give me a help. Thank you very much!
My problem solved!
I miss Visual C++ Redistributable 2008 and 2010.
Moderators please close my topic. Thank you!
Navigate to C:\Program Files (x86)\Notepad++ Right mouse click the Notpad++.exe file click properties & under the compatability Tab UN-TICK run the program as administrator box DONE.
Refer to this link.
this solved it for me:
right click on the file that won't run (in my case a .cmd file)
check the 'Unblock' checkbox next to the remark "This file came from another computer and might be blocked to help protect this computer"

How do I make my previous messages disappear in Visual Studio 2013 console applications? (C++)

So if I write a piece of code like this:
string name, feeling;
cout << What is your name?" << endl;
cin >> name;
cout << "Hello, " << name << "!"<<endl;
cout << "So how are you feeling today?" << endl;
cin >> feeling;
I get the output:
What is your name?
James (input from user)
Hello, James!
So how are you feeling today?`
But I want it to remove the first message and the input, so the user will get just this on the console window:
Hello, James!
So how are you feeling today?
As long as you stay on the same line, it's usually pretty easy to use a combination of \b (back-space) and/or \r (carriage return without new-line) and some spaces to go back to the beginning of a line and write over what's displayed there.
If you need to do (much) more than that, you can use the Windows console functions such as SetConsoleCursorPosition and FillConsoleOutputCharacter to write data where you want it, overwrite existing data, etc.
If you care about portability to Linux and such, or already know how to program with curses, you might prefer to use PDCurses instead. This is basically a re-implementation of the ncurses programming interface on top of the Windows console functions.
If you work on windows environment, try this
#include <iostream>
int main()
{
std::cout << "This is the first line";
system("cls");
std::cout << "This is the line on clear console" << std::endl;
return 0;
}

Why am I getting a segfault on cin?

I am writing a fairly simple exercise (homework), and most of it works, however it sometimes segfaults on cin. Here is the relevant code.
int main()
{
std::string str = "";
std::cout << "Please select the desired operation:\n";
std::cout << "(A): Generate Decompositions\n";
std::cout << "(B): Generate Acceptable Compositions from S1.txt and S2.txt\n";
std::cout << "cout"; //debug statement
std::cin >> str;
std::cout << "cin"; //debug statement
std::cout << str;
char resp = str.at(0);
std::cout << "resp"; //debug statement
...
}
I get a segfault on std::cin >> str (I know this because of what "debug statements" are output). But the weird thing is, I only get it when I input 'b'. If I input 'a', or any word starting with 'a', it works fine. If I enter any letter other than a or b, or anything starting with any other letter than a or b, it exits (as it's supposed to). But if I type in 'b', or any word starting with 'b', it Segfaults. Every single time. Why?
I know this because of what "debug statements" are output"
The code that you posted looks fine.
Since your output statements do not have << endl at the end, some of the output may still be buffered at the time of segfault. Writing out endl blocks until the output is flushed, so adding << endl is likely to help you get closer to the actual location of the crash.

Variable not accessible within an if statement

I have a variable which holds a score for a game.
My variable is accessible and correct outside of an if statement but not inside as shown below
score is declared at the top of the main.cpp and calculated in the display function which also contains the code below
cout << score << endl; //works
if(!justFinished){
cout << score << endl; // doesn't work prints a large negative number
endTime = time(NULL);
ifstream highscoreFile;
highscoreFile.open("highscores.txt");
if(highscoreFile.good()){
highscoreFile.close();
}else{
std::ofstream outfile ("highscores.txt");
cout << score << endl;
outfile << score << std::endl;
outfile.close();
}
justFinished = true;
}
cout << score << endl;//works
EDIT:
have realised my problem, when I was printing out it looped through many times so I did not see that all of them for the first loop didn't work, hence I thought the others were working when in fact they were not for the first iteration.
This is not a problem relating to the scope of the variable.
It could be several things:
Memory corruption somewhere
Multi threading relating problem
Something else...
Are you sure you are looking at the same iteration where the score value works before and after but not inside? Maybe put some more detailed logging instead of simply outputting the score itself.
Try printing cout << score << "#" << &score << endl; each place you currently print score. This will let you check if you're actually looking at the same variable.
If the addresses are different, you're accidentally shadowing your score variable somewhere - gcc has -Wshadow to catch that.
If the addresses are the same then the variable is getting corrupted somehow. Most debuggers have a memory breakpoint feature, so you can set the debugger to break when the memory in score changes and find the culprit.
With the amount of code you have attached there is nothing to indicate there is a problem in the code. As Brian says it is something else
Can you try this in your debugger and see what happens ? The idea is to simplify the problem as much as possible and try to get the minimal amount of code that replicates the problem.
What does this do ?
cout << score << endl; //works
if(!justFinished)
{
cout << score << endl; // doesn't work prints a large negative number
}
cout << score << endl; //works

Stroustrup swan book Vector Problem

I'm using Stroustrup's swan book. I have run into a problem getting output from a
vector. I followed the text example from sec. 4.6.3 on page 121. I
managed to get the source compiled and am able to execute it. After
typing in a list of whitespace separated words, the program hangs and
does not list the elements of the vector as it should. I realize not
every element will be outputted if it is repeated, but i receive no
output at all. I have compiled and run this using the g++ 4.3.2
compiler on Linux and using the Visual C++ express 2008 compiler on
windows. Both produce the same result. Thank you for taking time to
read this. Here is my source:
#include "Supporting_files/std_lib_facilities.h"
int main()
{
vector<string> words;
string temp;
cout << "Enter a list of words: ";
while(cin>>temp)
words.push_back(temp);
cout << "Number of words: " << words.size() << endl;
sort(words.begin(),words.end());
for(int i=0;i<words.size();++i)
if(i==0||words[i-1]!=words[i])
cout << words[i] << "\n";
}
while(cin>>temp) only ends when it hits an end of file. Use control-D to send an end of file into the terminal.