#include <iostream>
int main()
{
std::cout << "Hello";
}
I compiled it using g++ Hello.cpp
I received the following output when I ran the compiled a.out file using ./a.out
Why do I keep getting a % sign at the end of the output?
./a.out
Hello%
The % you see there might actually be your shell prompt, and not part of your program output. You're not writing a new line after your output, so the shell prompt appears at the very end of the output of the last command.
Possible solutions:
Append a newline to the end of the output with + "\n".
Add a std::endl to the end of your output.
Related
While trying to help a friend with a problem with his code, I encountered a very weird bug when compiling the following code with GCC.
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream classes("classes.txt");
std::string line;
std::string txt = ".txt";
while (std::getline(classes, line)) {
std::cout << "[-]: " << line << "," << txt << std::endl;
}
return 0;
}
classes.txt contains the following:
CSC1
CSC2
CSC46
CSC151
MTH121
When compiled with Clang or MSVC, the output is as follows:
[-]: CSC1,.txt
[-]: CSC2,.txt
[-]: CSC46,.txt
[-]: CSC151,.txt
[-]: MTH121,.txt
But, when compiled with GCC, this is what the code outputs:
,.txtCSC1
,.txtCSC2
,.txtCSC46
,.txtCSC151
[-]: MTH121,.txt
I cannot make sense of whats happening here. Can anyone explain this?
Image with GCC version and output:
No, this is not a compiler bug. You are running into line-ending differences between operating systems. My magic ball tells me that if you run dos2unix classes.txt, the problem will go away. Similarly, cat -v classes.txt should output something similar to the following:
CSC1^M
CSC2^M
CSC46^M
CSC151^M
MTH121^M
Here, the ^M denotes \r\n. This is known as a CRLF or "carriage return line feed". On Linux when the carriage return is encountered, it instructs the terminal to go back to the beginning of the line. This results in .txt overwriting whatever you had output previously.
N.B if you are running Clang on an Apple system, which I'm guessing you are, certain versions of Mac use \r, but not \r\n or \n..
[#tl;dr] I have Visual Studio Ultimate 2013 and Eclipse Neon v2(C++),
and I need to redirect the output of my program using DOS format, but
I have no idea how.
Im on Windows btw.
OK.. so I have this class assignment where I have to write a program like this:
#include <iostream>
#include <string>
using namespace std;
/* Check if the Character is lower case or not */
bool checkLowerCase(char c) {
if (c >= 'a' && c <= 'z') return true;
return false;
}
/*
* If there is any lower case letter, it will replace it will a upper case.
* example:
* "Tauros" will become "TAUROS"
* "auHU" will become "AUHU"
*/
string fixer(string s)
{
char right = ('a' - 'A');
string a = s;
for (unsigned int i = 0; i < a.length(); i++)
{
if (checkLowerCase(a[i])) {
a[i] = a[i] - right;
}
}
return a;
}
int main(void)
{
while (1) //infinite loop
{
string line;
getline(cin, line);
if (!cin) { //Professor wants us to only check end of input like this
return 0;
}
line = fixer(line);
cout << line << endl;
}
}
The input was:
aaaaaa
bbbbbb
cccccc
The output was:
aaaaaa
AAAAAA
bbbbbb
BBBBBB
cccccc
CCCCCC
Thanks for reading this far. Ok, so here's my problem.
The output is all messed up, so I need to redirect the output somewhere else (at least for testing).
I know how to do that using , , holding each line in a Array of String, reallocating if needed and then print what is on the array, but, unfortunately, my lecturer demanded us only to include and
Ohh, I dont know if it will matter, but we may not use char*, only the class string.
My lecturer told us that we have to use DOS format. But I have no clue how to do that. If someone can tell me how to do either redirect the input or the output is finee...
I have in my PC both Eclipse C++ (working glitchy) and Visual Studio Ultimate 2013 (working fine).
[Edit] Im on Windows.
AGAIN: I may only include and
For more information, here's his slide on DOS format.
*For testing purposes one redirect to/from a file
*DOS formatting will have unexpected consequences
– The end-of-line is the CR-NL combination
– A line read from the file will end with CR
– The CR character is the command to erase the
previous line!
./main < infile.txt Input is from infile.txt
./main > outfile.txt Output is to outfile.txt
./main < infile.txt > outfile.txt Both input and output are redirected
OK, so... I found instructions that helped me here:
https://msdn.microsoft.com/en-us/library/ms235639.aspx
I opened my project folder in the VisualStudio, created 2 files.
input.txt
output.txt
I opened the Developer Command Prompt for VS2013 that I had in my PC.
cd ["C://my_project_path"] //go to my project folder
cl /EHsc file.cpp
/*
cl /EHsc will compile my program (I think it only compiles one file at a time.
I have yet to test it).
*/
file < input.txt > output.txt // this command will run my program
So when I run my program like this... Instead of waiting for an input from keyboard, my program will read input.txt as the standard input. And when I try to print something to the output, it will write on the output.txt instead.
[EDIT] at the end of the link I pasted, there are explanations on how to compile multiple files and what exacly does the /EHsc do exacly
i write the simple hello program.
#include<iostream>
using namespace std;
int main()
{
cout<<"hello";
}
and I compile the program as g++ hello.cpp and run it as./a.out but could not get output as hello
The sample provide should work and should produce output. I do not see a reason not to print output. You could try flushing the std output stream and see whether it makes any difference. If you are running the program on a Linux platform, then you can debug using strace and there should be a write system call at the end.
strace a.out
Example:
....
write(1, "hello", 5hello) = 5
exit_group(0) = ?
You have output.
You just didn't add a newline character. It worked, but you probably saw something like as follows where your normal prompt was:
hello$
If you change std::cout << "hello"; to std::cout << "hello\n";, it'll "work" as expected.
I'm trying to write a simple Bash script to compile my C++ code, in this case it's a very simple program that just reads input into a vector and then prints the content of the vector.
C++ code:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
string s;
while (cin >> s)
v.push_back(s);
for (int i = 0; i != v.size(); ++i)
cout << v[i] << endl;
}
Bash script run.sh:
#! /bin/bash
g++ main.cpp > output.txt
So that compiles my C++ code and creates a.out and output.txt (which is empty because there is no input). I tried a few variations using "input.txt <" with no luck. I'm not sure how to pipe my input file (just short list of a few random words) to cin of my c++ program.
You have to first compile the program to create an executable. Then, you run the executable. Unlike a scripting language's interpreter, g++ does not interpret the source file, but compiles the source to create binary images.
#! /bin/bash
g++ main.cpp
./a.out < "input.txt" > "output.txt"
g++ main.cpp compiles it, the compiled program is then called 'a.out' (g++'s default output name). But why are you getting the output of the compiler?
I think what you want to do is something like this:
#! /bin/bash
# Compile to a.out
g++ main.cpp -o a.out
# Then run the program with input.txt redirected
# to stdin and the stdout redirected to output.txt
./a.out < input.txt > output.txt
Also as Lee Avital suggested to properly pipe an input from the file:
cat input.txt | ./a.out > output.txt
The first just redirects, not technically piping. You may like to read David Oneill's explanation here: https://askubuntu.com/questions/172982/what-is-the-difference-between-redirection-and-pipe
I have a simple C++ program that reads stdin using scanf and returns results to stdout using printf:
#include <iostream>
using namespace std;
int main()
{
int n, x;
int f=0, s=0, t=0;
scanf("%d",&n); scanf("%d",&x);
for(int index=0; index<n; index++)
{
scanf("%d",&f);
scanf("%d",&s);
scanf("%d",&t);
if(x < f)
{
printf("first\n");
}
else if(x<s)
{
printf("second\n");
}
else if(x<t)
{
printf("third\n");
}
else
{
printf("empty\n");
}
}
return 0;
}
I am compiling with g++ and running under linux. I execute the program using a text file as input, and pipe the output to another text file as follows:
program < in.txt > out.txt
The problem is that out.txt looks like this:
result1_
result2_
result3_
...
Where '_' is an extra space at the end of each line. I am viewing out.txt in gedit.
How can I produce output without the additional space?
My input file looks like this:
2 123
123 123 123
123 234 212
Edit: I was able to find a workaround for this issue: printf("\rfoo");
Thanks for your input!
Try removing the '\n' from your printf() statements, and run the code again. If the output file looks like one long word (no spaces), then you know that the only thing being inserted after the text is that '\n'.
I assume that the editor you are using to read the out.txt file just makes it look like there is an extra space after the output.
If you are still unsure, you can write a quick program to read in out.txt and determine the ASCII code of each character.
The end of line chars are:
System Hex Value Type
Mac 0D 13 CR
DOS 0D 0A 13 10 CR LF
Unix 0A 10 LF
For a end of line on each system you can:
printf("%c", 13);
printf("%c%c", 13, 10);
printf("%c", 10);
You can use this like
printf("empty");
printf("%c", 10);
Wikipedia Newline article here.
Okay, it's a little hard to figure this out, as the example program has numerous errors:
g++ -o example example.cc
example.cc: In function 'int main()':
example.cc:19: error: 'k' was not declared in this scope
example.cc:22: error: 'o' was not declared in this scope
example.cc:24: error: 'd' was not declared in this scope
make: *** [example] Error 1
But it's not going to be your input file; your scanf will be loading whatever you're typing into ints. This example, though:
/* scan -- try scanf */
#include <stdio.h>
int main(){
int n ;
(void) scanf("%d",&n);
printf("%d\n", n);
return 0;
}
produced this result:
bash $ ./scan | od -c
42
0000000 4 2 \n
0000003
on Mac OS/X. Get us a copy of the code you're actually running, and the results of od -c.
More information is needed here, as timhon asked, which environment are you working under? Linux, Windows, Mac? Also, what text editor are you using which displays these extra spaces?
My guess is that your space isn't really a space. Run
od -hc out.txt
to double check that it is really a space.
First, the code sample you've given doesn't compile as o and d are not defined...
Second, you've probably got whitespace at the end of the line you're reading in from the input file. Try opening it in vi to see. Otherwise, you can call a trim function on each line prior to output and be done with it.
Good luck!
Make sure you're looking at the output of the program you expect; this has a syntax error (no ";" after int n).
I feel like it's not even close to this, but if you run this on Windows, you'll get \r\n as line terminators, and, maybe, under *nix, under a non-Windows-aware text editor, you'll get \r as a common blank space, since \r is not printable.
Long shot, the best way to test this is using an hexadecimal editor and see the file yourself.