how can i export results of my calculation fom my c++ programme

Hey everybody,
I need some help here. I have to create a project to plot Cartesian graphs. Until now i am using c++ to calculate the Cartesian coordinates, but i need further development. I enter a loop where my x coordinate every time adds 0.1, and the corresponding y coordinate for the given equation is created.
I have an equation :-
100y+22.09x=2209
and, my code goes as given below. Sample output, of first 6 x and y coordinates is also given (here also i had to type all the values)
But i need to get this information out of c++. Until now, i have to copy all the coordinates one by one. i need a tag which could export this info in another form, like word doc, or spreadsheet(x coordinate and y coordinate as 2 separate columns).
Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
  for (i=0;i<10;i=i+0.1)
{
cout<<"\n\nX coordinate = "<<i;
y2=((a*b)-(b*i*i))/a;
e=sqrt(y2);
cout<<"\nY coordinate = "<<e;
getch();
}
getch();
}


x coordinate = 0
y coordinate = 4.7

x coordinate = 0.1
y coordinate = 4.699765

x coordinate = 0.2
y coordinate = 4.69906

x coordinate = 0.3
y coordinate = 4.697885

x coordinate = 0.4
y coordinate = 4.696238

x coordinate = 0.5
y coordinate = 4.694121
closed account (o3hC5Di1)
Hi there,

You could write your coordinates in CSV format, that would probably be the simplest way to make the information available as a spreadsheet.

http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules_and_examples

1
2
3
4
std::ifstream ifs("coordinates.csv");

//in your loop
ifs << x << "," << y << "\r\n";


Hope that helps.

All the best,
NwN
NwN,
I am not very Good at c++. I did not fully understand ur answer. I need more explained answer.
so, according to you, in my code, i should write like this:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
double i,a,b,e,y2;
//the values of a and be a are are already inputed by the user in the earlier phase of my code
  for (i=0;i<10;i=i+0.1)
{
cout<<"\n\nX coordinate = "<<i;
y2=((a*b)-(b*i*i))/a;
e=sqrt(y2);
cout<<"\nY coordinate = "<<e;
getch();
}
std::ifstream ifs("coordinates.csv");
ifs << x << "," << y << "\r\n";
getch();
}

Would this create this "coordinates,csv" in its main folder, every time i run the software and it calculates the required result?
And also please tell if any libraries are required to mention
I am asking cause i didnt understand the wiki u have mentioned

Very Much Thanks
Last edited on
Also, you shouldn't use FP numbers in a loop. They are not exact. Instead calculate the number of times the loop needs to run, and convert that to an int. Should be easy (EndValue - StartValue) / StepValue, so for you this (10.0 - 0.0) / 0.1 = 100.0. Use static_cast<int> to do the casting.

So use ints as variable types in the for loop, but double types inside the loop.

Also, it is always int main() never void main()
Apparent typo in NwN's code: s/if/of/

Read about std::ofstream http://www.cplusplus.com/reference/fstream/ofstream/

Alternatively, replace the "ifs" with "std::cout", e.g. remove lines 8 and 11, and replace 12 with NwN's 4.
Thanks
Topic archived. No new replies allowed.