Stroustrup swan book Vector Problem - c++

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.

Related

strange thing in sequential file access

Recently, I am trying to write codes to get trained in sequential file access. I learned it well, but the issue is kinda stressing me out. I have a code that work 100%, and its task is "make a code that prints array elements inside a file", the text file name is "numric".
#include <iostream>
#include <fstream>
using namespace std;
int main(){
int a[3]={5000,6000,7000};
ofstream outfile("numric.txt");
if (outfile.is_open()){
outfile << "employee payroll:" << endl;
for (int i=0;i<3;i++)
outfile << a[i] << endl;
outfile.close();
} else
cout << "failed!" << endl;
return 0;
}
I implemented the code in two different program (VS Code, dev++) and it works fine. It found the file, but when I open the text file, there isn't any text inside it. The code should do its work by finding some text inside it if I opened it.
Note:sometimes when the code works, the antivirus pops a message saying it found an item that doesn't look safe in the program and deletes it.

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"

C++ (g++) special characters encoding ('\a', '\b', etc.)

I am using g++ version 4:4.8.2-1ubuntu6 with Eclipse 3.8 on Linux Mint.
Following example from my C++ book does not work as expected:
//bondini.cpp -- using escape sequences
#include <iostream>
int main()
{
using namespace std;
cout << "\aOperation \"HyperHype\" is activated\n";
cout << "enter sercret code:________\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered: " << code << "...\n";
cout << "\aCode OK! Commencing Z3!\n";
return 0;
}
I get following result when running the program:
In Eclipse and directory I am using UTF-8 encoding. Why does not '\a' play sound as it should and '\b' does not move the cursor one space back, while '\n' works properly.
edit: As I understand it, it is the compiler that makes the mess of it. --> I was wrong, in terminal it works fine, but eclipse 'terminal' does not work.
Wherever you are sending your output. What the destination does with it is entirely in its own hands. So while eclipse might not support these special characters your terminal should.

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;
}

cout behaving unexpectedly for specific strings

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.