Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 putting comma in cout (C++)

views
     
TSdelsoo
post Nov 23 2016, 03:23 PM, updated 8y ago

Look at all my stars!!
*******
Senior Member
3,187 posts

Joined: Nov 2013
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
double x1,x2,x3,y1,y2,y3;
cout<< "Enter the coordinate x1,y1,x2,y2,x3,y3"<<endl;
cin>>x1>>y1>>x2>>y2>>x3>>y3;

double x = ((x1+x2+x3)/3) ;
double y = ((y1+y2+y3)/3) ;

cout<< "The centroid(G) is = " <<x,<<y;
system("Pause");
return(0);
}

I want to design a program using C++ code to show centroid = (x,y ) with comma in between x and y , but i tried the above, but compilation error occured.
Eventless
post Nov 23 2016, 06:22 PM

Look at all my stars!!
*******
Senior Member
2,641 posts

Joined: Jan 2003
QUOTE(delsoo @ Nov 23 2016, 03:23 PM)
CODE

   cout<< "The centroid(G) is =  " <<x,<<y;

I want to design a program using C++ code to show centroid = (x,y ) with comma in between x and y , but i tried the above, but compilation error occured.
*
CODE

   cout<< "The centroid(G) is =  (" <<x<<","<<y<<")";

Would the above code work? , by itself means it is part of the code rather that part of a string that you want to join to the rest of the output. That is why you are getting a compilation error.
TSdelsoo
post Nov 24 2016, 12:11 PM

Look at all my stars!!
*******
Senior Member
3,187 posts

Joined: Nov 2013
QUOTE(Eventless @ Nov 23 2016, 06:22 PM)
CODE

   cout<< "The centroid(G) is =  (" <<x<<","<<y<<")";

Would the above code work? , by itself means it is part of the code rather that part of a string that you want to join to the rest of the output. That is why you are getting a compilation error.
*
can ypu explain why the open bracket is inside the " ?
Eventless
post Nov 24 2016, 05:21 PM

Look at all my stars!!
*******
Senior Member
2,641 posts

Joined: Jan 2003
QUOTE(delsoo @ Nov 23 2016, 03:23 PM)
I want to design a program using C++ code to show centroid = (x,y ) with comma in between x and y , but i tried the above, but compilation error occured.
*
QUOTE(delsoo @ Nov 24 2016, 12:11 PM)
can ypu explain why the open bracket is inside the " ?
*
Didn't you mention that you wanted the output to be in this form?
CODE
centroid = (x,y)

Just like the comma, the brackets need to be within quotes to be treated as part of a string.
DanikaMuehl
post Dec 1 2016, 07:54 PM

New Member
*
Junior Member
6 posts

Joined: Dec 2016


You should use escape sequences for displaying the comma between variables otherwise it is used to differentiates these variables. You can also use printf statements.
yan99033
post Dec 7 2016, 10:40 AM

Getting Started
**
Junior Member
116 posts

Joined: Apr 2007
QUOTE(delsoo @ Nov 23 2016, 03:23 PM)
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    double x1,x2,x3,y1,y2,y3;
    cout<< "Enter the coordinate x1,y1,x2,y2,x3,y3"<<endl;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;

    double  x = ((x1+x2+x3)/3) ;
    double y = ((y1+y2+y3)/3) ;

    cout<< "The centroid(G) is =  " <<x,<<y;
    system("Pause");
    return(0);
}

I want to design a program using C++ code to show centroid = (x,y ) with comma in between x and y , but i tried the above, but compilation error occured.
*
It is because you cannot mix different data type.
x is double
',' is char

Therefore, (double)(char) gives you error message.

Try this (as DanikaMuehl suggested above),
cout << "The centroid(G) is " << x << ", " << y << endl //separate the comma from the x

or use printf with placeholders (if you prefer C style, but remember to #include <stdio.h>)

printf("The centroid(G) is %f , %f", x, y)

This post has been edited by yan99033: Dec 7 2016, 10:43 AM
SaberCortez
post Dec 7 2016, 10:44 AM

Enthusiast
*****
Senior Member
710 posts

Joined: Nov 2012



QUOTE(delsoo @ Nov 23 2016, 04:23 PM)
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    double x1,x2,x3,y1,y2,y3;
    cout<< "Enter the coordinate x1,y1,x2,y2,x3,y3"<<endl;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;

    double  x = ((x1+x2+x3)/3) ;
    double y = ((y1+y2+y3)/3) ;

    cout<< "The centroid(G) is =  " <<x,<<y;
    system("Pause");
    return(0);
}

I want to design a program using C++ code to show centroid = (x,y ) with comma in between x and y , but i tried the above, but compilation error occured.
*
Macaim ini la abang:


» Click to show Spoiler - click again to hide... «


This post has been edited by SaberCortez: Dec 7 2016, 10:46 AM

 

Change to:
| Lo-Fi Version
0.0127sec    0.30    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 07:35 AM