Concatenate integer and space/newline for cout in C++


 
Thread Tools Search this Thread
Top Forums Programming Concatenate integer and space/newline for cout in C++
# 1  
Old 01-25-2018
Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator.
In C I could do it with:
Code:
printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n");

but could not figure out the equivalent in C++:
Code:
cout << ((j+1)%20)? string(tmp_int+"\t"):string(tmp_int+"\n");
cout << ((j+1)%20)? to_string(tmp_int+"\t"):to_string(tmp_int+"\n");

Can anyone correct my code for the right trick?
Thanks!
# 2  
Old 01-25-2018
Plus does not mean append, that's only a feature of std::string. What you are doing is adding a number to a number.

I don't think you need std::string at all. Just let cout print them:
Code:
cout << tmp_int << ((j+1)%20)?"\t":"\n";

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-25-2018
Quote:
Originally Posted by yifangt
I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator.
In C I could do it with:
Code:
printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n");

but could not figure out the equivalent in C++:
Code:
cout << ((j+1)%20)? string(tmp_int+"\t"):string(tmp_int+"\n");
cout << ((j+1)%20)? to_string(tmp_int+"\t"):to_string(tmp_int+"\n");

Can anyone correct my code for the right trick?
Thanks!
In addition to what Corona688 has already said; if you were trying to append a <newline> to the string returned by string() or to_string() using std::string that would be coded as:
Code:
to_string(tmp_int)+"\n";

not:
Code:
to_string(tmp_int+"\n");

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-25-2018
Thanks!
I'm surprised the problem seems to be with the higher precedence of the operator "<<" in this case
Code:
cout  << tmp_int << ((j+1)%20) ? "\t":"\n";

which needs to be bracketed as
Code:
cout  << tmp_int << (((j+1)%20) ? "\t":"\n");

Did I miss anything?
# 5  
Old 01-26-2018
No idea what's going on there. If it mistook it for some other operator there'd be a syntax error since ? and : can't do much else, but it's definitely not working. Perhaps it's being taken to be the wrong type.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace space by newline error

Hello I have had a requirement where I need to move data to a new line based on a text .So basically as soon as it encounters :61: it should move to a new line Source Data : :61:D100,74NCH1 :61:D797,50NCH2 :61:D89,38NCHK2 :61:D99,38NCHK12 :61:D79,38NCHK22 :61:D29,38NCHK5 Target Data... (11 Replies)
Discussion started by: kamijia83
11 Replies

2. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

3. Shell Programming and Scripting

Newline to space code removes 0 from the first line

I am having a peculiar problem. First I run the code below to append 0 at the start of each line in some hundreds of files that I have in a directory. These files have each word in a newline. for f in *.dat; do echo "0" > tmpfile cat $f >> tmpfile mv tmpfile $f done Then I run this... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

4. Shell Programming and Scripting

concatenate is ignoring space

Hi All,I am facing the below problem I have set a variable: a=`cat a.txt| grep "mad" | cut -c 30-50`the output is coming echo $a1 10 Mad300 3215however the actual ouput is 1.10 Mad300 3215There are 4spaces between 300 and 3215 so if i do: echo "$a" I am getting correct output: 1.10... (3 Replies)
Discussion started by: mad_man12
3 Replies

5. UNIX for Dummies Questions & Answers

Remove newline & space

Can someone help me on this. I have a file that has a long line just like below. The long line keeps on being truncated to the next line (new line + space) for some reason. Basically, I just need to remove this problem. Hope somebody can help! Thanks! INPUT FILE: structuralObjectClass:... (4 Replies)
Discussion started by: Orbix
4 Replies

6. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

7. Shell Programming and Scripting

Awk adding a space between integer and the rest of the field

Hi, I have a problem where some of the records I need to process have the first address field as something like "10Walpole Street" where obviously I want it to be "10 Walpole Street". I know I need to somehow separate out the integer and probably form a new string variable, but I just don't... (5 Replies)
Discussion started by: jonathanm
5 Replies

8. Shell Programming and Scripting

Concatenate the string with the newline character

Hi , i want to Concatenate a string and use the following code str="i" str="$str am \n" str="$str a \n" str="$str boy \n" echo $str I want to ouput this i am a boy However it outputs i am \n a \n boy \n (3 Replies)
Discussion started by: youareapkman
3 Replies

9. UNIX for Advanced & Expert Users

newline character, space and tab after a string

no problem (6 Replies)
Discussion started by: angelina
6 Replies

10. Solaris

Dvd files concatenate error: Not enough space

Hi, I downloaded the latest sparc ver of sol10, tried to concatenate the files and encounter error: Not enough space on the disk. Tried from both sol10 x86 and winxp. command used: UNIX: cat file1 file2 file3 file4 file5 > file.iso DOS: copy /b file1 + file2 + file3 + file4 + file5 file.iso... (3 Replies)
Discussion started by: maag
3 Replies
Login or Register to Ask a Question