C++ output formatting error - c++

I was working on output formatting on C++ when I came across this weird error. Here is a sample of my code used to print info contained in an array:
int store;
while(table[i]!=NULL)
{
store=table[i]->ReturnID();
output<<"ID: "<<setw(9)<<store<<"\t"; // Export to a file channel
store=table[i]->ReturnTotalNumber();
output<<" Total Number: "<<setw(5)<<store<<endl;
i++;
}
Although the code compiles and works as intended here is a part of my output.txt file:
ID: 243 Total Number: 0
ID: 312 Total Number: 0
ID: 458 Total Number: 0
ID: 700 Total Number: 0
ID: 738 Total Number: 0
.....
The second line of the txt file always seems to be off balance. The rest are OK no matter how many they are. I have also noticed that changing the first setw(9) from 9 to any other random number fixes that bug but I can't explain it. Anyone have any explanation for this? My compiler is GCC-G++ version 4.4.1 and my IDE is Code::Blocks 10.05.

Based on the strings you're outputting, it appears that the tab has somehow gone astray. Your task therefore is to find out whether it's not being output by the program or whether it's not being rendered by whatever tool you're using to look at the output.
I would send the output to a file then use whatever hex dump utility you have to examine said file to see if the tab is actually there. Under Linux (or Windows with CygWin), you could use:
od -xcb fileNameYouCapturedTo
If you can establish it's not being output, raise a bug on glibc here. If it is being output then you have two possibilities.
First, you can file a bug against whatever tool you're using to view the output, whether that be an editor or the terminal program you're using (since it doesn't appear to be treating tabs correctly).
Or you could ditch the tabs altogether and just output a single space. Given that you're columns are already lined up because of the setw(), there's little need for tabs here.

Related

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");

How to Display a Smiley Face in C++ Console

When I use to have Windows 7, I use to be able to use certain codes for my C++ programs to create acsii symbols. For example, if I did "cout << (char)1;" my program would display a white smiley face in the console box. Now since I updated to Windows 10, those codes do not work anymore.
I've done research and figured out the unicode from Windows 10 has changed. In the link below, I use to be able to use symbol/code 1 through 31 but now they appear not to work on Windows 10.
Link: https://www.alt-codes.net/
My question is, is there anyway to be able to get codes 1-31 back, possibly by downloading the Windows 7 unicode or is there a new symbol to paste the smiley face from the link? And if that's the case, how would I show it from doing this: "cout << (char) ??? ";
When I use cout < (char)1, it displays displays a box with a question mark inside of it.

In Xcode 8.3 update, C/C++ programming printf does not give output without newline

After last week's updated to Xcode 8.3, in a C/C++ program the output from a printf statement no longer appears on the screen without a newline. Thus I can't prompt for the user to enter a number, and have them type in that number on the same line following the input prompt.
Neither flushing the output buffer [fflush(stdout) or cout << endl] nor setting the output buffer to NULL [setbuf(stdout, NULL)] addresses this problem, but rather is a question specifically about Xcode 8.3 seemingly being broken.
With the scanf commented out, the output of the program below is:
Enter a value for x: Value of x is: 0
With the scanf in place, the output from the first printf never shows up. If you go ahead and type in a value and press enter, only then does it show up. Output is:
3
Enter a value for x: Value of x is: 3
Full test program is here:
#include <iostream>
using namespace std;
int main() {
int x=0;
printf("Enter a value for x: ");
//scanf("%d", &x);
printf("Value of x is: %d\n", x);
return 0;
}
My work-around has been to revert back to Xcode 8.2.1, downloaded from developer.apple.com/xcode/downloads/
8.3.2 was announced last night and addresses this supposedly:
It is standard behavior in C for stdout to be flushed when an input function is called, such as scanf(), regardless of whether a newline was output prior to the call or not. This ensures that all appropriate output is displayed before the input operation takes place. Therefore, the update might have broken something in Xcode. Although I'm not currently sure what the exact nature of the problem is, a (temporary) workaround is to run your application on the command line. This has worked for my projects. It also reveals that this problem is with the Xcode output window, and not the compiler or something else.
In response to tell's comment: no, flushing stdout does not correct the issue within Xcode. This implies even more strongly to me that the issue is definitely in the Xcode interface itself. When running the application from the command line, calls to fflush() work as expected.
Also, printing to stderr makes no difference from within Xcode. Basically, stdout should be flushed in this case without appealing to stderr or any other gimmicks because the OP is calling scanf(). It works perfectly from the command line... just not in the Xcode output window.
And please note this this question is not a duplicate: it has nothing to do with anyone's misunderstanding of how C input and output work, and everything to do with the fact that a recent update to Xcode broke something.
EDIT:
Thanks, joe_04_04. The update certainly seems to have fixed the problem.

decoding the runtime error (SIGFPE)

This is a problem on spoj.com (http://www.spoj.com/problems/PRIC/) .We have to check whether numbers of the sequence : ai=( a(i-1)+1234567890 ) mod 2^31 are prime or not, 1st number is 1.
My code is given below (Please try to ignore the clumsiness.) based on sieve of eratosthenes .
The PROBLEM : We have to print "prime(1) or not(0)" for sequence upto i=33,333,333 , My code works perfectly fine for i( c3 in the code) values upto 8000 or so and after that (e.g c3>19000 ) it starts giving The SIGFPE error . Now i googled about the error , it has something to do with division/ mod by 0. But why is it that code works for c3 values upto 9000 but not beyond that?
Depending on your compiler and development environment, you should read up on the concept of a Debugger. This answer has a guide to use gdb. If you are using Visual Studio, Code::Blocks or any other IDE, look up the debugging features. For instance how to set a breakpoint or step into/out of/over a function call for instance, watching or changing variables etc. (I'm mentioning these things to give you vital hints for Google search-words, wink wink nudge nudge).
EDIT:
Copy-pasted the code and saved it, compiled with gcc -g for debug symbols and -lm to link the math library, I ran it through gdb and it gave me this output:
Program received signal SIGFPE, Arithmetic exception.
0x0000000000400707 in sieve (prime=0x6626a0) at t.c:43
43 if (a%prime2[j]==0){
This tells you to look at line 43, at the if statement that uses a modulo operation. This seems to be the place you are doing the modulo zero.
Do note that line 43 in the document I got when I copy-pasted your code from Stackoverflow, may not be line 43 in your document.
EDIT2:
Hey my answer was unaccepted! - why was that :) ?

Pre-assign parameter in script

Actually I have trouble naming the title of this post. Because I don't know how to summarize my meaning in a professional way. But I'll explain my question as below:
I'm running a program written by C++, command is:
./VariationHunter_SC
Then it'll let you type in many parameters:
Please enter the minimum paired-end insert size:
Please enter the maximum paired-end insert size:
Please enter the pre-processing mapping prune probability:
Please enter the name of the input file:
Please enter the minimum support for a cluster:
Obviously I need to type in such parameters one by one to run the program; But I have thousands of such jobs, and need to pre-assign such parameters in script, and submit script to computer.
So how can I do that?
thx
Edit
so how can I make parameter-list?
Just like below?:
140
160
0
mrfast.vh
1
Seems the program cannot recognize these numbers, and distribute numbers..
This depends on how the program actually reads the data that you type in - it's likely that its reading stdin, so you could use separate files with the parameters and pass them in via redirection: ./VariationHunter_SC < parameter-file
It's also possible that the program will accept parameters on the command line, but there's no way of really knowing that (or how) except by whatever documentation the program might come with (or by reading the source code, if it's available and there is no other accurate docs).
Simply use the piping character to pipe the contents of a file to your program
example, in a windows command shell:
echo "asdf" | pause
This will pass "asdf" to the pause program. As a result, pause will print a "Press any key to continue" message, then imediately continue because it will receive the "asdf" string as a response.
So, overall, write or use a program that outputs the contents of your file. Call it, then pipe its output to the program that needs the input.
The unix cat command is such a command that writes the contents of a file to output, or to the input of another executable if you are piping the output.